Python Operators
Operators are special symbols that perform operations on variables and values. Python supports 7 categories of operators — arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators.
1. Arithmetic Operators
Used for mathematical calculations.
a = 10
b = 3
print(a + b) # 13 (addition)
print(a - b) # 7 (subtraction)
print(a * b) # 30 (multiplication)
print(a / b) # 3.333... (division — always returns float)
print(a // b) # 3 (floor division — removes decimal)
print(a % b) # 1 (modulus — remainder)
print(a ** b) # 1000 (exponent — 10 to the power 3)
2. Comparison Operators
Compare two values and return True or False.
x = 5
y = 10
print(x == y) # False (equal)
print(x != y) # True (not equal)
print(x < y) # True (less than)
print(x > y) # False (greater than)
print(x <= y) # True (less than or equal)
print(x >= y) # False (greater than or equal)
3. Logical Operators
Combine multiple conditions. and, or, not.
age = 22
has_id = True
print(age >= 18 and has_id) # True (both must be True)
print(age < 18 or has_id) # True (at least one True)
print(not has_id) # False (reverses)
4. Assignment Operators
Shorthand ways to update a variable.
x = 10
x += 5 # x = x + 5 → 15
x -= 3 # x = x - 3 → 12
x *= 2 # x = x * 2 → 24
x /= 4 # x = x / 4 → 6.0
x //= 2 # x = x // 2 → 3.0
x %= 2 # x = x % 2 → 1.0
x **= 3 # x = x ** 3 → 1.0
print(x)
5. Bitwise Operators
Operate on binary representations of numbers. Rarely used but asked in interviews.
a = 5 # binary: 101
b = 3 # binary: 011
print(a & b) # 1 (AND)
print(a | b) # 7 (OR)
print(a ^ b) # 6 (XOR)
print(~a) # -6 (NOT)
print(a << 1) # 10 (left shift)
print(a >> 1) # 2 (right shift)
6. Membership Operators
Check if a value exists in a sequence. in, not in.
name = "Ankit"
fruits = ["apple", "banana", "mango"]
print("A" in name) # True
print("z" not in name) # True
print("apple" in fruits) # True
print("grape" in fruits) # False
7. Identity Operators
Check if two variables point to the same object in memory. is, is not.
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (same object)
print(a is c) # False (different object)
print(a == c) # True (same values)
print(a is not c) # True
== vs is: Use == to compare values, is to compare identity (memory location). They behave differently for lists and other mutable objects.
Operator Precedence
Python follows PEMDAS-like rules. From highest to lowest:
()— Parentheses**— Exponent* / // %— Multiply, Divide, Modulus+ -— Add, Subtract< > <= >= == !=— Comparisonnot,and,or— Logical
Write a program that takes two numbers and prints: their sum, difference, product, division, floor division, and remainder — each on a separate line with a label. Then check if the first number is greater than the second using a comparison operator.
Quick Revision
- Arithmetic:
+ - * / // % ** - Comparison:
== != < > <= >= - Logical:
and,or,not - Assignment:
+= -= *= /= //= %= **= - Bitwise:
& | ^ ~ << >> - Membership:
in,not in - Identity:
is,is not //is floor division,%is modulus.
Interview Questions
Q: Difference between / and // in Python?
A: / is true division — always returns a float. // is floor division — returns the largest integer less than or equal to the result. Example: 10 / 3 = 3.333, 10 // 3 = 3.
Q: What is the difference between == and is?
A: == compares values (are they equal?), is compares identity (are they the same object in memory?). For integers, both often behave the same, but for lists they differ.
Q: What does 5 % 2 return?
A: 1. The modulus operator returns the remainder of the division.
Q: Explain the ** operator.
A: ** is the exponentiation operator. 2 ** 3 means 2 to the power 3, which equals 8.
Want to Master Python?
Join our Python course in Varanasi — practical classes and career support at our Sarnath centre.