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

PF LAB 02 (Variables and Operators)

Programming Fundamental in python

Uploaded by

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

PF LAB 02 (Variables and Operators)

Programming Fundamental in python

Uploaded by

Syed Farrukh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming Fundamentals (CS-116L) SSUET/QR/114

LAB # 02

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 a 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.

1
Programming Fundamentals (CS-116L) 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

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.

Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

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

3
Programming Fundamentals (CS-116L) SSUET/QR/114

Python Logical Operators


Logical operators are used to combine conditional statements:
Operator Name Example
and Return True if both statements are x < 10 and x > 5
true
or Return True if one of the statements x < 5 or x < 4
is 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,and also paste the output in the following Python
statements.

1. x=5:
print(x)
Error

Statement: #Error in above code


Correct code:
X=5
Print(x)
Output

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 ;

Statement: Variables ‘a’,’b’ and ‘c’ have the same value 7


a=7
b = a (which means b is also assigned 7)
c = b (which means c is also assigned 7)
3+4 is a simple addition which is equal to 7

Output:

C. Write the following Python programs:

1. Write a program that calculates area of a circle A=π r 2 . (Consider r = 50).

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.

 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

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
7
Programming Fundamentals (CS-116L) SSUET/QR/114

Code:

Output:

8
Programming Fundamentals (CS-116L) SSUET/QR/114

Output

3 a = b = 3 = 4
Error

Statement: # There must be (,) in between two variable


or integer
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 ;
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)

Statement: # Variables ‘a’,’b’ and ‘c’ have the same value 7


a=7
b = a (which means b is also assigned 7)
c = b (which means c is also assigned 7)
3+4 is a simple addition which is equal to 7

Output:

10

You might also like