Operators in Python
What are Operators?
Operators are special symbols used to perform operations on values and variables.
The values on which the operator works are called operands.
a = 10
b = 5
print(a + b) # + is operator, a and b are operands
15
Types of Operators in Python
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Assignment Operators
- Membership Operators
- Identity Operators
Arithmetic Operators
These operators are used to perform mathematical operations.
a = 15
b = 4
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
Addition: 19 Subtraction: 11 Multiplication: 60 Division: 3.75 Floor Division: 3 Modulus: 3 Exponent: 50625
Explanation
+→ Addition-→ Subtraction*→ Multiplication/→ Division (always returns float)//→ Floor Division (gives only whole number)%→ Modulus (gives remainder)**→ Exponent (power)
Comparison (Relational) Operators
These operators compare two values and return True or False.
x = 10
y = 20
print(x == y) # Equal to
print(x != y) # Not equal to
print(x > y) # Greater than
print(x < y) # Less than
print(x >= 10) # Greater than or equal to
print(x <= 5) # Less than or equal to
False True False True True False
Comparison operators are heavily used in if conditions and loops.
Logical Operators
Logical operators are used to combine conditional statements.
a = True
b = False
print(a and b) # True only if both are True
print(a or b) # True if at least one is True
print(not a) # Reverses the result
False True False
Practical Example
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
else:
print("Entry denied")
Entry allowed
Assignment Operators
Assignment operators are used to assign values to variables.
x = 10
print(x)
x += 5 # x = x + 5
print(x)
x -= 3 # x = x - 3
print(x)
x *= 2 # x = x * 2
print(x)
x /= 4 # x = x / 4
print(x)
x //= 2 # x = x // 2
print(x)
x **= 3 # x = x ** 3
print(x)
10 15 12 24 6.0 3.0 27.0
Membership Operators
Membership operators check whether a value exists in a sequence (string, list, tuple, set, etc.).
text = "Python"
print("P" in text)
print("z" in text)
print("z" not in text)
numbers = [10, 20, 30, 40]
print(20 in numbers)
print(50 not in numbers)
True False True True True
in → Returns True if value is present
not in → Returns True if value is not present
Identity Operators
Identity operators check whether two variables refer to the same object in memory.
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True → same object
print(a is c) # False → different objects
print(a == c) # True → same values
print(a is not c) # True
True False True True
is checks identity (same object), while == checks equality (same value).
Operator Precedence
When multiple operators are used in a single expression, Python follows a specific order (precedence).
print(10 + 5 * 2) # Multiplication first
print((10 + 5) * 2) # Parentheses first
print(10 + 5 * 2 ** 3) # Exponent → Multiply → Add
20 30 50
Common Precedence Order (High to Low)
()→ Parentheses**→ Exponent*,/,//,%→ Multiplication & Division+,-→ Addition & Subtraction- Comparison operators (
==,>,<etc.) - Logical operators (
not,and,or)
Summary – Operators in Python
- Arithmetic →
+-*///%** - Comparison →
==!=><>=<= - Logical →
andornot - Assignment →
=+=-=*=/=etc. - Membership →
innot in - Identity →
isis not
Next Topic → Strings in Python
Operators in Python
Learn Arithmetic, Comparison, Logical, Assignment, Membership and Identity operators.
Select any topic from the left sidebar to begin.
Start Learning →
