0% found this document useful (0 votes)
58 views

LAB # 02 Variables and Operators Objective

The document summarizes an objective and exercises from a programming lab on variables and operators in Python. The objective is to implement different data types, variables, and operators. The exercises include (1) identifying errors in Python statements, (2) evaluating operations and printing results, and (3) writing programs to calculate the area of a circle, perform arithmetic operations, convert between units of height, and compute distance between points using the Pythagorean theorem. Sample code and outputs are provided for each part.

Uploaded by

John wick
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

LAB # 02 Variables and Operators Objective

The document summarizes an objective and exercises from a programming lab on variables and operators in Python. The objective is to implement different data types, variables, and operators. The exercises include (1) identifying errors in Python statements, (2) evaluating operations and printing results, and (3) writing programs to calculate the area of a circle, perform arithmetic operations, convert between units of height, and compute distance between points using the Pythagorean theorem. Sample code and outputs are provided for each part.

Uploaded by

John wick
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Fundamentals (SWE-102) SSUET/QR/114

LAB # 02

VARIABLES AND OPERATORS

OBJECTIVE
Implement different type of data types, variables and operators used in Python.

EXERCISE
A. Point out the errors, if any, in the following Python statements.

1. x=5:
print(x)
>>> %Run 'lab2 exercise.py'
File "F:\lab files\lab2 exercise.py", line 1
x=5:
misuse of colon[:]
x=5
print(x)

>> %Run 'lab2 exercise.py'


5

2. 1TEXT = "SSUET" NUMBER = 1 print(NUMBER+ TEXT)


File "F:\lab files\lab2 example.py", line 1
• 1TEXT = "SSUET"
• A variable name cannot start with a number.
• Output
x="SSUET"
no=1
print(x,"+",no)

>>> %Run 'lab2 exercise.py'


SSUET + 1

1
Programming Fundamentals (SWE-102) SSUET/QR/114

3. a = b = 3 = 4
File "F:\lab files\lab2 ex.py", line 1
a=b=3=4
^
SyntaxError: can't assign to literal

Two values can’t assign to a single variable.

B. Evaluate the operation in each of the following statements, and show the
resultant value after each statement is executed.

1. a = 2 % 2 + 2 * 2 - 2 / 2;
a = 2 % 2 + 2 * 2 - 2 / 2;
print(a)

Output:
>>> %Run 'l2 code 1.py'
3.0

2. b = 3 / 2 + 5 * 4 / 3 ;
b=3/2+5*4/3;
print(b)

Output:
>> %Run 'l2 code 2.py'
8.166666666666668

3. c = b = a = 3 + 4 ;
c=b=a=3+4;
print(c)

Output:
2
Programming Fundamentals (SWE-102) SSUET/QR/114

>> %Run 'l2 code 3.py'


7

C. Write the following Python programs:

1. Write a program that calculates area of a circle 𝐴=𝜋𝑟2. (Consider r =


50).

pi=3.142
r=50
area=pi*r*r
print("area of the circle:%2f"%area)

>>> %Run 'lab2 ex.py'


area of the circle:7855.000000

Output:
>>> %Run 'lab2 ex.py'
area of the circle:7855.000000

2. Write a program that performs the following four operations and prints their result on
the screen.
a. 50 + 4
b. 50 – 4
c. 50 * 4
d. 50 / 4

a=50
b=4
3
Programming Fundamentals (SWE-102) SSUET/QR/114

print("50+4=",a+b)
print("50-4=",a-b)
print("50*4=",a*b)
print("50/4=",a/b)

Output:
> %Run 'lab2 ex.py'
**Arithmetic Operators**
50+4= 54
50-4= 46
50*4= 200
50/4= 12.5

3. Write a Python program to convert height (in feet and inches) to centimeters.
Convert height of 5 feet 2 inches to centimeters.

• First, convert 5 feet to inches: 5 feet × 12 inches/foot = 60 inches


• Add up our inches: 60 + 2 = 62 inches
• Convert inches to cm: 62 inches × 2.54 cm/inch = 157.48 cm

print("**Conversion of height to cm**")


f=5
i=2
m=(f*12)+i
print("height in cm=",m*2.54)
Output:

**Conversion of height to cm**


height in cm= 157.48

4. Write a program to compute distance between two points by creating variables


(Pythagorean Theorem)
Distance =((x2−x1)^2+(y2−y1)^2)^1/2

print('**Pythagorean Theorem**')
x1=18
x2=24
y1=20
y2=26
4
Programming Fundamentals (SWE-102) SSUET/QR/114

print('d=',((x2-x1)**2+(y2-y1)**2)**(1/2))
Output:

Run 'lab2 ex.py'


**Pythagorean Theorem**
d= 8.48528137423857

You might also like