0% found this document useful (0 votes)
23 views9 pages

Lab Sheet 02

The document is a lab manual for an introduction to Python lab that provides objectives and examples for students to complete exercises on variables, operators, and data types in Python. The objectives include understanding variables, assigning values, arithmetic operators, logical operators, bitwise operators, and more. The lab manual provides 8 examples of code for students to write using different operators, data types, and performing arithmetic operations in Python.

Uploaded by

IKhlaq Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Lab Sheet 02

The document is a lab manual for an introduction to Python lab that provides objectives and examples for students to complete exercises on variables, operators, and data types in Python. The objectives include understanding variables, assigning values, arithmetic operators, logical operators, bitwise operators, and more. The lab manual provides 8 examples of code for students to write using different operators, data types, and performing arithmetic operations in Python.

Uploaded by

IKhlaq Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Manual

Introduction to python

Instructor
Mujtaba Awan
[email protected]
Student Name

Reg. No

Department Computing & Technology

Batch/ Semester

Assigned Date

Due Date

Week 2
Lab-sheet 2
Introduction to python Lab 2

Lab Objectives:

At the end of this lab students will know about.

 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)

Assigning Values to Variables:


Python variables do not need explicit declaration to reserve memory space. The declaration happens
automatically when you assign a value to a variable. The equal sign (=) is used to assign values to
variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the =
operator is the value stored in the variable.
For example
a= 100 # An integer assignment
b = 1000.0 # A floating point
c = "John" # A string
print (a)
print (b)
print (c)
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously.
For example
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the same
memory location. You can also assign multiple objects to multiple variables.
For example
a,b,c = 1,2,"mrcet“
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one
string object with the value "john" is assigned to the variable c.
Output Variables:
The Python print statement is often used to output variables.
Variables do not need to be declared with any particular type and can even change type after they have
been set.
For example
x=5 # x is of type int
x = "mrcet " # x is now of type str
print(x)
To combine both text and a variable, Python uses the “+” character:
Example
x = "awesome"
print("Python is " + x)

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.

How to write Logical operators code in python:


Example: 1
a=5
b=2
c=3
d = 200

print("******* Logical and *******")


print(a>b and a>c)
print(a>b and a<c)
print(a<b and a>c)
print(a<b and a<c)
print(a>b and c)
print(a>b and c and d)
print(a<b and c)
print(a<b and c and d)

Example: 2
a=5
b=2
c=3
d = 200

print("******* Logical or *******")


print(a>b or a>c)
print(a>b or a<c)
print(a<b or a>c)
print(a<b or a<c)
print(a>b or c)
print(a>b or c or d)
print(a<b or c)
print(a<b or c or d)

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

print('~a = ', ~a)


print('a&b = ', a&b)
print('a|b = ', a|b)
print('a^b = ', a^b)
print('a<<2 = ', a<<2)
print('a>>2 = ', a>>2)

Example: 6
Inoperator

st1 = "Welcome to python"


print("to" in st1)

st2 = "Welcome top python "


print("to" in st2)

st3 = "Welcome to python "


print("subs" in st3)

Example: 7
NotInoperator

st1 = "Welcome to python "


print("subs" not in st1)

st2 = "Welcome to python "


print("to" not in st2)

st3 = "Welcome top python "


print("to" not in st3)

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:

Write a program to perform different arithematic operations on numbers in python.

Source code:

You might also like