Lab 2
Lab 2
LABORATORY EXERCISE 2
VARIABLES AND OPERATORS
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.
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:
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
Python divides the operators in the following groups:
• Arithmeticoperators
• Assignmentoperators
• Comparisonoperators
• Logicaloperators
• Identityoperators
• Membershipoperators
• Bitwiseoperators
EXERCISE
A. Point out the errors, if any, in the following Pythonstatements.
1.x=5:
Print(x)
OUTPUT:
2. 1TEXT = "SSUET"
NUMBER = 1
print(NUMBER+ TEXT)
OUTPUT:
3. a = b = 3 = 4
OUTPUT:
B. Evaluate the operation in each of the following statements, and show the
resultant value after each statement isexecuted.
1. a = 2 % 2 + 2 * 2 - 2 /2;
OUTPUT:
2. b = 3 / 2 + 5 * 4 / 3 ;
3. c = b = a = 3 + 4 ;
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
SOURCE CODE:
OUTCOME:
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 = 60inches
• Add up our inches: 60 + 2 = 62inches
• Convert inches to cm: 62 inches × 2.54 cm/inch = 157.48cm
SOURCE CODE:
OUTPUT:
SOURCE CODE:
OUTPUT: