Lab 02
Lab 02
Python Operators
Operators are special symbols that perform operations on variables and values.
These operations can be arithmetic, logical, comparison, assignment, membership,
or identity operations. Understanding operators is crucial for manipulating data
effectively and writing concise code.
Types of Python Operators
• Arithmetic Operators
• Comparison Operators
• Assignment Operators
• Logical Operators
• Membership Operators
• Identity Operators
• Bitwise Operators
Page 2 of 11
AUST CSE
a = 21
b = 10
c=0
c=a+b
print ("a: {} b: {} a+b: {}".format(a,b,c))
c=a-b
print ("a: {} b: {} a-b: {}".format(a,b,c) )
c=a*b
print ("a: {} b: {} a*b: {}".format(a,b,c))
c=a/b
print ("a: {} b: {} a/b: {}".format(a,b,c))
c=a%b
print ("a: {} b: {} a%b: {}".format(a,b,c))
a=2
b=3
c = a**b
print ("a: {} b: {} a**b: {}".format(a,b,c))
a = 10
b=5
c = a//b
print ("a: {} b: {} a//b: {}".format(a,b,c))
Comparison operators compare the values on either side of them and decide the
relation among them. They are also called Relational operators.
Page 3 of 11
AUST CSE
a = 21
b = 10
if ( a == b ):
print ("Line 1 - a is equal to b")
else:
print ("Line 1 - a is not equal to b")
if ( a != b ):
print ("Line 2 - a is not equal to b")
else:
print ("Line 2 - a is equal to b")
if ( a < b ):
print ("Line 3 - a is less than b" )
else:
print ("Line 3 - a is not less than b")
if ( a > b ):
print ("Line 4 - a is greater than b")
else:
print ("Line 4 - a is not greater than b")
if ( a <= b ):
print ("Line 5 - a is either less than or equal to b")
else:
print ("Line 5 - a is neither less than nor equal to b")
if ( b >= a ):
print ("Line 6 - b is either greater than or equal to b")
else:
print ("Line 6 - b is neither greater than nor equal to b")
Page 4 of 11
AUST CSE
Python logical operators are used to combine two or more conditions and check the
final result.
var = 5
Page 5 of 11
AUST CSE
a = 10
b = 20
list = [1, 2, 3, 4, 5]
if ( a in list ):
print ("a is present in the given list")
else:
print ("a is not present in the given list")
if ( b not in list ):
print ("b is not present in the given list")
else:
print ("b is present in the given list")
c=b/a
print ("c:", c, "list:", list)
if ( c in list ):
print ("c is available in the given list")
else:
print ("c is not available in the given list")
Page 6 of 11
AUST CSE
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c=a
print(a is c)
print(a is b)
print(a is not c)
print(a is not b)
Page 7 of 11
AUST CSE
a = 20
b = 10
print ('a=',a,':',bin(a),'b=',b,':',bin(b))
c=0
c = a & b;
print ("result of AND is ", c,':',bin(c))
c = a | b;
print ("result of OR is ", c,':',bin(c))
c = a ^ b;
print ("result of EXOR is ", c,':',bin(c))
c = ~a;
print ("result of COMPLEMENT is ", c,':',bin(c))
c = a << 2;
print ("result of LEFT SHIFT is ", c,':',bin(c))
c = a >> 2;
print ("result of RIGHT SHIFT is ", c,':',bin(c))
Python if Statement
An if statement executes a block of code only if the specified condition is met.
if condition:
# body of if statement
Page 8 of 11
AUST CSE
number = 10
else:
# body of else statement
Page 9 of 11
AUST CSE
number = 10
if number > 0:
print('Positive number')
else:
print('Negative number')
Page 10 of 11
AUST CSE
number = 0
if number > 0:
print('Positive number')
else:
print('Zero')
# outer if statement
if number >= 0:
# inner if statement
if number == 0:
print('Number is 0')
Page 11 of 11