Lab Sheet 02
Lab Sheet 02
Introduction to python
Instructor
Mujtaba Awan
[email protected]
Student Name
Reg. No
Batch/ Semester
Assigned Date
Due Date
Week 2
Lab-sheet 2
Introduction to python Lab 2
Lab Objectives:
Variables:
Assigning Values to Variables:
Multiple Assignment:
Output Variables:
Expressions
Arithmetic Operators
Relational Operators / Comparison Operators
Logical Operators
Assignment Operators
Bitwise Operators
Membership Operators
Identity Operators
Variables:
Variables are nothing but reserved memory locations to store values. This means that when
you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can
be stored in the reserved memory. Therefore, by assigning different data types to variables,
you can store integers, decimals, or characters in these variables.
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 2
x = "Python is "
y = "awesome"
z=x+y
print(z)
Expressions:
An expression is a combination of values, variables, and operators. An expression is
evaluated using assignment operator.
Logical Operator:
Logical operators are used to connect more relational operations to form a complex expression called
logical expression. A value obtained by evaluating a logical expression is always logical, i.e. either True
or False.
LAB EXERCISES
The best way to teach programming is by example, and the only way to learn programming is by doing.
So, please do it yourself and don’t copy paste from others.
Example: 2
a=5
b=2
c=3
d = 200
Example: 3
a=5
b=2
c=3
d = 200
#Logical not
print("******* Logical not *******")
print(not(a<b))
print(not(a>b))
Example: 4
# Assignment operators
a = 10
b = 20
m = 15
y=a+b
print(y)
m+=10
print(m)
Example: 5
Bitwise operator
a = 10 # 0000 1010
b = 15 # 0000 1111
Example: 6
Inoperator
Example: 7
NotInoperator
Example: 8
Isoperator
a = 10
b = 10
print(a is b)
a = 10
b = '10'
print(a is b)
Example: 8
isnotoperator
a = 10
b = 10
print(a is not b)
a = 10
b = '10'
print(a is not b)
Tasks
Write a program to demonstrate different number datatypes in python.
Source code:
Source code: