PF LAB 02 (Variables and Operators)
PF LAB 02 (Variables and Operators)
LAB # 02
OBJECTIVE
Implement different type of data types, variables and operators used in Python.
THEORY
Variable
Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
Example
x= 5
y= "John"
print(x)
print (y)
Output:
>>> %Run task1.py
5
John
Variables do not need to be declared with any particular type and can even change type after
they have been set.
1
Programming Fundamentals (CS-116L) SSUET/QR/114
Example:
x= 4
x= "Sally"
print(x)
Output:
>>> %Run task2.py
Sally
Example:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Output:
>>> %Run task3.py
Orange
Banana
Cherry
Example:
x= "awesome"
print("Python is " , x)
Output:
>>> %Run task4.py
Python is awesome
Python Keywords
Keywords are the words whose meaning have already been explained to the Python
compiler. The keywords cannot be used as variable names,function name or any
identifier because if we do so we are trying to assign a new meaning to the keyword,
which is not allowed by the computer. Keywords are also called ‘Reserved words’.
Some keywords are as follows:
2
Programming Fundamentals (CS-116L) SSUET/QR/114
Data Types
Data types specify how we enter data into our programs and what type of data we enter.
Python Data Types are used to define the type of a variable.
Python has five standard data types −
Numbers (int, float)
String
List
Tuple
Dictionary
You can get the data type of any object by using the “type( )” function.
Operators
Operators are special symbols in Python that carry out arithmetic or logical
computation.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
3
Programming Fundamentals (CS-116L) SSUET/QR/114
EXERCISE
A. Point out the errors, if any,and also paste the output in the following Python
statements.
1. x=5:
print(x)
Error
2. 1TEXT = "SSUET"
NUMBER = 1
print(NUMBER+ TEXT)
Error
Statement: #
Correct code:
TEXT = “SSUET”
NUMBER = 1
4
Programming Fundamentals (CS-116L) SSUET/QR/114
Print(NUMBER, TEXT)
Output
3. a = b = 3 = 4
Error
Statement: #
Correct code:
a , b = 3 , 4
print(a)
print(b)
Output
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;
Statement:
2 % 2: This is the modulus operator, which gives the remainder when 2 is divided by
2, Since 2 is evenly divisible by 2, the result is 0.
2 * 2: This is simple multiplication, which equals 4.
2 / 2: This is division, which equals 1.
Output:
2. b = 3 / 2 + 5 * 4 / 3 ;
print(b)
5
Programming Fundamentals (CS-116L) SSUET/QR/114
Statement:
3. c = b = a = 3 + 4 ;
Output:
Code:
# Calculate Area of Circle
r= 50*50
pi=3.142
print("Area of Circle")
A= r*pi
print(A)
Output:
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
Code:
# Performing the four operations
a=50 + 4
6
Programming Fundamentals (CS-116L) SSUET/QR/114
b=50 - 4
c=50 * 4
d=50 / 4
print(a)
print(b)
print(c)
print(d)
Output:
3. Write a Python program to convert height (in feet and inches) to centimeters.
Convert height of 5 feet 2 inches to centimeters.
Code:
Output:
Code:
Output:
8
Programming Fundamentals (CS-116L) SSUET/QR/114
Output
3 a = b = 3 = 4
Error
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;
Statement:
2 % 2: This is the modulus operator, which gives the remainder when 2 is divided by
2, Since 2 is evenly divisible by 2, the result is 0.
2 * 2: This is simple multiplication, which equals 4.
2 / 2: This is division, which equals 1.
Output:
2. b = 3 / 2 + 5 * 4 / 3 ;
pprint(b)
9
Programming Fundamentals (CS-116L) SSUET/QR/114
Statement:#
Divide 3/2 & 4/3
Multiply 5 by 1.33
Add both the value
Output:
3. c = b = a = 3 + 4 ;
print(c)
Output:
10