Software Development (MCS-203L) SSUET/QR/114
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.
Rules for constructing variable names
A variable can have a short name (like x and y) or a more descriptive name (age,carname,
total volume). Rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
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.
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY
Software Development (MCS-203L) SSUET/QR/114
Example:
x= 4
x= "Sally"
print(x)
Output:
>>> %Run task2.py
Sally
Assign Value to Multiple Variables
Python allows you to assign values to multiple variables in one line
Example:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Output:
>>> %Run task3.py
Orange
Banana
Cherry
To combine both text and a variable, Python uses the + character
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:
false class finally is return none continue for try break
true def for from while and del not with as
elif if or except in raise yield
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY
Software Development (MCS-203L) 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
Python divides the operators in the following groups:
• Arithmeticoperators
• Assignmentoperators
• Comparisonoperators
• Logicaloperators
• Identityoperators
• Membershipoperators
• Bitwiseoperators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Python Relational Operators
Comparison operators are used to compare two values:
Operator Name Example
== Equal x==y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY
Software Development (MCS-203L) SSUET/QR/114
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator Name Example
and Return True if both statements are true x < 5 and x < 10
or Return True if one of the statements is x < 5 or x < 4
true
not Reverse the result, returns False if the not (x<5 and x< 10)
result is true
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:
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY
Software Development (MCS-203L) SSUET/QR/114
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:
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY
Software Development (MCS-203L) SSUET/QR/114
2. b = 3 / 2 + 5 * 4 / 3 ;
3. c = b = a = 3 + 4 ;
C. Write the following Python programs:
1. Write program that calculates area of a circle𝐴=𝜋𝑟 2 . (Consider r =50).
SOURCE CODE:
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:
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY
Software Development (MCS-203L) SSUET/QR/114
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:
4. Write a program to compute distance between two points by creating variables
(Pythagorean Theorem)
Distance =((x2−x1)^2+(y2−y1)^2)^1/2
SOURCE CODE:
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY
Software Development (MCS-203L) SSUET/QR/114
OUTPUT:
NAME: Laraib Shahzad ROLL NO.:20fbmcs-004
SIRSYED UNIVERSTY OF NGEENIRING AND TECHNOLOGY