Operators
What is an Operator?
An operator is a symbol that performs an operation (like +, -, *, etc.).
Types of Operators in Python
[Link] Operators
Operator Meaning Example
+ Addition 5+3=8
- Subtraction 5-2=3
* Multiplication 5 * 2 = 10
/ Division 10 / 2 = 5.0
// Floor division 7 // 2 = 3
% Modulus 7%2=1
** Exponent 2 ** 3 = 8
Example
a = 10
b=5
print(a + b)
print(a -b)
print(a * b)
print(a / b)
print(a % b)
print(a ** b)
print(a // b)
Task
What is the result of:
125 + 87 = ?
What is the result of:
450 - 298 = ?
What is the result of:
23 × 15 = ?
What is the result of:
144 ÷ 12 = ?
What is the remainder when:
55 ÷ 6 = ? (Find 55 % 6)
6) Adrianna has 100 pieces of gum to share with her friends. When she went to the park,
she shared 10 pieces of strawberry gum. When she left the park, Adrianna shared another
10 pieces of bubble gum. How many pieces of gum does Adrianna have now?
2) Assignment Operators
Assignment operators are used to assign values to variables.
They can also be used to update the value of a variable directly using arithmetic operations.
List of Assignment Operators
Operator Meaning Example Same As
= Assign a=5 a=5
+= Add and assign a += 3 a=a+3
-= Subtract and assign a -= 2 a=a–2
*= Multiply and assign a *= 4 a=a*4
/= Divide and assign a /= 2 a=a/2
%= Modulus and assign a %= 3 a=a%3
//= Floor divide and assign a //= 2 a = a // 2
**= Exponent and assign a **= 3 a = a ** 3
&= Bitwise AND and assign a &= 2 a=a&2
` =` Bitwise OR and assign `a
^= Bitwise XOR and assign a ^= 1 a=a^1
>>= Bitwise right shift and assign a >>= 1 a = a >> 1
<<= Bitwise left shift and assign a <<= 2 a = a << 2
Example Program for Assignment Operators:
a = 10
print(f"Initial value of a: {a}")
a += 5
print(f"After a += 5 : {a}")
a -= 3
print(f"After a -= 3 : {a}")
a *= 2
print(f"After a *= 2 : {a}")
a /= 4
print(f"After a /= 4 : {a}")
a %= 4
print(f"After a %= 4 : {a}")
a **= 2
print(f"After a **= 2 : {a}")
Practice Questions
1)Given: a = 20
Operation: a += 10
What is the value of a?
2) Given: b = 50
Operation: b -= 15
What is the value of b?
3) Given: c = 6
Operation: c *= 3
What is the value of c?
4)Comparison Operators
Operator Description Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5>3 True
< Less than 3<5 True
>= Greater than or equal 5 >= 5 True
<= Less than or equal 3 <= 5 True
Example Program:
a = 10
b = 20
print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a < b) # True
print(a >= b) # False
print(a <= b) # True
Practice Questions
1)Given: x = 15, y = 10 Is x > y?
2) Given: c = 50, d = 25 Is c != d?
3) Given: a = 7, b = 7 Is a <= b?
5. Bitwise Operators
Bitwise operators work at the binary level. They compare or manipulate individual bits of
integers.
Operator Description Example Result
& AND 5&3 1
` ` OR `5
^ XOR 5^3 6
~ NOT (complement) ~5 -6
<< Left shift 5 << 1 10
>> Right shift 5 >> 1 2
Example Program:
a = 5 # Binary: 0101
b = 3 # Binary: 0011
print("Bitwise AND (a & b):", a & b) # 1
print("Bitwise OR (a | b):", a | b) #7
print("Bitwise XOR (a ^ b):", a ^ b) # 6
print("Bitwise NOT (~a):", ~a) # -6
print("Left Shift (a << 1):", a << 1) # 10
print("Right Shift (a >> 1):", a >> 1) # 2
Bitwise Operators - Practice Questions
1)Given: x = 6, y = 3 , What is x & y?
2) Given: p = 7, q = 2 ,What is p ^ q?
3) Given: t = 9 ,What is t << 2?
6. Membership Operators
Membership operators are used to test whether a value exists in a sequence (like a string, list,
tuple, set, or dictionary).
Operator Description Example Result
In Present in sequence 'a' in 'cat' True
not in Not present 'd' not in 'cat' True
Example Program:
text = "Hello World"
print('H' in text) # True
print('z' not in text) # True
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits) # True
print('grape' not in fruits) # True
nums = (1, 2, 3, 4)
print(3 in nums) # True
print(5 not in nums) # True
Membership Operators - Practice Questions
1)Given: colors = ['red', 'green', 'blue'] Is 'green' in colors?
2) Given: word = "python" Is 'y' not in word?
3) Given: names = ['Ram', 'Sita', 'Laxman'] Is 'Hanuman' not in names?
Identity Operators in Python
Identity operators are used to compare the memory location of two objects, not their values.
List of Identity Operators:
Operator Meaning Example Result
Is Returns True if both variables refer to the same object a is b True
is not Returns True if both variables do not refer to the same object a is not b True
Example Program:
x = [1, 2, 3]
y=x
z = [1, 2, 3]
print(x is y) # True (y and x are the same object)
print(x is z) # False (x and z have same value but different memory location)
print(x == z) # True (x and z have the same content)
print(x is not z) # True (different objects)
Identity Operators - Practice Questions
1)a = [10, 20, 30]
b=a
Is a is b?
2) p = (4, 5)
q = (4, 5)
Is p is not q?
3) str1 = "Python"
str2 = "Python"
Is str1 is str2?
4) list1 = [5, 6, 7]
list2 = list1
Is list1 is not list2?
What is an Expression in Python?
An expression is a combination of:
Variables
Values (constants)
Operators
Function calls
👉 When evaluated, an expression produces a result (a value).
Example of Expressions:
a = 10 + 5 # 10 + 5 is an expression (result is 15)
b=a*2 # a * 2 is an expression (result is 30)
c = b > 20 # b > 20 is an expression (result is True or False)