CSE 1278
Computer Programming and Algorithms Sessional
Lab Session-2
AUST CSE
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
Python Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication, etc.
For Example,
Assume, variable a holds 10 and variable b holds 20.
Operator Name Example
+ Addition a + b = 30
- Subtraction a – b = -10
* Multiplication a * b = 200
/ Division b/a=2
% Modulus b%a=0
** Exponent a**b =10**20
// Floor Division 9//2 = 4
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))
Python Comparison Operators
Comparison operators compare the values on either side of them and decide the
relation among them. They are also called Relational operators.
Assume, variable a holds 10 and variable b holds 20.
Operator Name Example
== Equal (a == b) is not true
!= Not equal (a != b) is true
> Greater than (a > b) is not true
Page 3 of 11
AUST CSE
< Less than (a < b) is true
>= Greater than or equal to (a >= b) is not true
<= Less than or equal to (a <= b) is true
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")
a,b=b,a #values of a and b swapped. a becomes 10, b becomes 21
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 Assignment Operators
Assignment operators are used to assign values to variables. For Example,
Operator Example Equivalence
= a = 10 a = 10
+= a += 30 a = a + 30
-= a -= 15 a = a - 15
*= a *= 10 a = a * 10
/= a /= 5 a=a/5
%= a %= 5 a=a%5
**= a **= 4 a = a ** 4
//= a //= 5 a = a // 5
&= a &= 5 a=a&5
|= a |= 5 a=a|5
^= a ^= 5 a=a^5
>>= a >>= 5 a = a >> 5
<<= a <<= 5 a = a << 5
:= print(x := 3) x=3
print(x)
Python Logical Operators
Python logical operators are used to combine two or more conditions and check the
final result.
Assume, variable a holds 10 and variable b holds 20.
Operator Name Example
and AND a>5 and b<25
or OR a>5 or b<25
not NOT not(a>5 or b<25)
var = 5
print(var > 3 and var < 10)
print(var > 3 or var < 4)
print(not (var > 3 and var < 10))
Page 5 of 11
AUST CSE
Python Membership Operators
Python's membership operators test for membership in a sequence, such as strings,
lists, or tuples.
Operator Description Example
in returns True if it finds a variable in the a in b
specified sequence, false otherwise
not in returns True if it does not find a a not in b
variable in the specified sequence and
false otherwise
a = 10
b = 20
list = [1, 2, 3, 4, 5]
print ("a:", a, "b:", b, "list:", list)
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
Python Identity Operators
Identity operators compare the memory locations of two objects.
Operator Description Example
is returns True if both variables are the a is b
same object and false otherwise
is not returns True if both variables are not a is not b
the same object and false otherwise
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)
Python Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation. These operators
are used to compare binary numbers.
Operator Name Example
& AND a&b
| OR a|b
^ XOR a^b
~ NOT ~a
<< Zero fill left shift a << 3
>> Signed right shift a >> 3
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
Here, if the condition of the if statement is:
• True - the body of the if statement executes.
• False - the body of the if statement is skipped from execution.
Page 8 of 11
AUST CSE
number = 10
# check if number is greater than 0
if number > 0:
print('Number is positive')
print('This statement always executes')
Python if...else Statement
An if statement can have an optional else clause. The else statement executes if the
condition in the if statement evaluates to False.
if condition:
# body of if statement
else:
# body of else statement
Here, if the condition inside the if statement evaluates to
• True - the body of if executes, and the body of else is skipped.
• False - the body of else executes, and the body of if is skipped.
Page 9 of 11
AUST CSE
number = 10
if number > 0:
print('Positive number')
else:
print('Negative number')
print('This statement always executes')
Python if…elif…else Statement
The if...else statement is used to execute a block of code among two alternatives.
However, if we need to make a choice between more than two alternatives, we use
the if...elif...else statement.
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
Page 10 of 11
AUST CSE
number = 0
if number > 0:
print('Positive number')
elif number <0:
print('Negative number')
else:
print('Zero')
print('This statement is always executed')
Python Nested if Statements
It is possible to include an if statement inside another if statement. For example,
number = 5
# outer if statement
if number >= 0:
# inner if statement
if number == 0:
print('Number is 0')
# inner else statement
else:
print('Number is positive')
# outer else statement
else:
print('Number is negative')
Page 11 of 11