1
What are
Operators?
Operators are
special symbols in Python that perform operations on values
(operands).
Types of Operators in Python
Python operators can be divided into 7 main categories:
1. Arithmetic Operators
Used for mathematical operations.
Operator Meaning Example Result
+ Addition 5+3 8
- Subtraction 5-3 2
* Multiplication 5*3 15
/ Division 5/2 2.5
// Floor Division 5 // 2 2
% Modulus (remainder) 5 % 2 1
** Exponentiation 2 ** 3 8
# + addition add to operands
# - substraction to operand
# * multiplication opreand
# / division to operand return to float division
# // floor division to operand remove decimal part
# % modulus to operand retain remainder
2
# ** exponentiation to operand (power of number)
1. Comparison (Relational) Operators
Used to compare values → returns True or False.
# These operators are used to compare two values; they
always return in Boolean value True or False.
# Used commonly in condition, loops and decision making.
Operator Meaning Example Result
== Equal 5 == 5 True
Not
!= 5 != 3 True
Equal
Greater
> 5>3 True
than
< Less than 5 < 3 False
Greater
>= 5 >= 5 True
or equal
Less or
<= 5 <= 3 False
equal
EX:
age = 18
if age >= 18:
print("You are eligible to vote")
else:
print("you are not eligible to
vote")
3
Operator Meaning Example Result
You are eligible to vote
3.Logical Operators
Used to combine conditions.
Operator Meaning Example Result
(5 > 2)
True if
and and (3 < True
both true
4)
True if at
(5 < 2) or
or least one True
(3 < 4)
true
Reverses not(5 >
not False
result 2)
x=8
print (x > 5 and x < 10) # True (both are true)
print (x > 5 or x < 3) # True (one is true)
print (not (x > 5)) # False (x > 5 is true, not makes it false)
EX:
username = "admin"
password = "1234"
4
if username == "admin" and password == "1234":
print("Login successful")
else:
print("Login failed")
user = "admin"
passw = "1234"
if user == "admin" and passw == "admin":
print("Access Granted")
else:
print("Access Denied")
4.Assignment Operators
Used to assign values.
Operator Meaning Example
= Assign a = 10
+= Add & assign a += 5 → a = a + 5
-= Subtract & assign a -= 3
*= Multiply & assign a *= 2
/= Divide & assign a /= 2
%= Modulus & assign a %= 3
5
Operator Meaning Example
**= Power & assign a **= 2
//= Floor divide & assign a //= 2
#additional assignment
x=5 # we provide x variable value 5
x+=3 # x=x+3
print(x)
#subtraction assignment
z=5
z-=3 #z=z-3
print(z)
#multiplication assignment
a=5
a*=6 #a=a*5
print(a)
#division assignment
b=5
b/=3 #b=b/3
print(b)
#modulus assignment
6
c=10
c%=3
print(c)
5.Bitwise Operators
Work at binary level (0s and 1s).
Operator Meaning Example
& AND 5&3→1
` ` OR
^ XOR 5^3→6
~ NOT (invert bits) ~5 → -6
<< Left shift 5 << 1 → 10
>> Right shift 5 >> 1 → 2
a = 10 # Binary: 1010
b = 4 # Binary: 0100
print(a & b)
print (a | b) # 1110 → 14
print (a ^ b) # 1110 → 14
print(~a) # -11 (inverts bits + adds 1 to it – uses 2's
complement)
7
print (a << 1) # 10100 → 20
print (a >> 1) # 0101 → 5
6.Membership Operators
Used to test membership in sequences (list, string, etc.).
Used to test whether a value exists in a sequence like a list,
string, or tuple.
Operator Example Result
'a' in
in True
'apple'
'x' not in
not in True
'apple'
fruits = ["apple", "banana", "cherry"]
print ("banana" in fruits) # True
print ("mango" not in fruits) # True
text = "hello"
print ("h" in text) # True
print ("z" not in text) # True
7.Identity Operators
Used to compare memory location (not
values).
8
Operator Example Result
Operator Example Result
True if
is a is b same
object
True if not
a is not
is not same
b
object
Used to compare the memory locations (i.e.,
object identity) of two objects.
a = [1, 2, 3]
b=a
c = [1, 2, 3]
print (a is b) # True (same object)
print (a is c) # False (same content, different
objects)
print (a == c) # True (values are same)
print (a is not c) # True
Summary:
# Arithmetic → +, -, *, /, %, //, **
# Relational → ==, !=, >, <, >=, <=
# Logical → and, or, not
9
Operator Example Result
# Assignment → =, +=, -=, etc.
# Bitwise → &, |, ^, ~, <<, >>
# Membership → in, not in
# Identity → is, is not