Python Operators
Master Python arithmetic, comparison, logical, and assignment operators with examples and output.
Arithmetic Operators
Arithmetic operators perform maths. Python has all the standard ones plus two extras worth knowing:
** for exponentiation and // for floor division (divides then rounds down).
The % (modulus) operator gives the remainder after division — very useful for checking
if a number is even (n % 2 == 0) or cycling through a range.
a, b = 10, 3 print(a + b) # 13 addition print(a - b) # 7 subtraction print(a * b) # 30 multiplication print(a / b) # 3.33 true division (always float) print(a // b) # 3 floor division print(a % b) # 1 remainder print(a ** b) # 1000 10 to the power 3
13 7 30 3.3333333333333335 3 1 1000
Comparison Operators
Comparison operators always return True or False. They are the foundation
of every decision in your code. The most important thing to remember is the difference between
= (assignment — sets a value) and == (equality check — asks "are these equal?").
Confusing them is one of the most common beginner mistakes.
x, y = 5, 10 print(x == y) # Equal? False print(x != y) # Not equal? True print(x < y) # Less than? True print(x > y) # Greater than? False print(x <= 5) # Less or equal? True (5 ≤ 5) print(x >= 6) # Greater/equal? False
False True True False True False
Logical Operators
Logical operators combine boolean expressions. and requires both sides to be True.
or requires at least one side to be True. not flips the result.
These let you express complex real-world conditions naturally, like "user is over 18 AND has a valid ID".
age = 20 has_id = True print(age >= 18 and has_id) # True and True = True print(age >= 18 and not has_id) # True and False = False is_weekend = False is_holiday = True print(is_weekend or is_holiday) # False or True = True
True False True
Assignment Operators
Assignment operators are shortcuts that combine arithmetic with assignment.
Instead of writing x = x + 5, write x += 5. This works with all arithmetic
operators and is the standard Python style.
n = 10 n += 5 # n = n + 5 → 15 print(n) n -= 3 # n = n - 3 → 12 print(n) n *= 2 # n = n * 2 → 24 print(n) n //= 5 # n = n // 5 → 4 print(n)
15 12 24 4
++ or -- operators like C or JavaScript. Use x += 1 to increment a variable.🧠 Quick Check
What does 17 % 5 return?