LAB # 02 Variables and Operators Objective
LAB # 02 Variables and Operators Objective
LAB # 02
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)
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
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
pi=3.142
r=50
area=pi*r*r
print("area of the circle:%2f"%area)
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.
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: