Python — Operators
Arithmetic operators
a = 10
b = 3
print(a + b) # 13 — addition
print(a - b) # 7 — subtraction
print(a * b) # 30 — multiplication
print(a / b) # 3.3333... — true division (always returns float)
print(a // b) # 3 — floor division (rounded down)
print(a % b) # 1 — modulus (remainder)
print(a ** b) # 1000 — exponentiation (10 to the power 3)
Key differences from other languages:
/always returns a float, even with integers:10 / 2gives5.0, not5//floors the result:7 // 2gives3, not3.5**is the power operator:2 ** 10gives1024
Division quirks
print(7 / 2) # 3.5
print(7 // 2) # 3 (floors toward negative infinity)
print(-7 // 2) # -4 (not -3!)
print(7 % 2) # 1
print(-7 % 2) # 1 (always non-negative when divisor is positive)
Floor division rounds toward negative infinity, not toward zero. This is different from C and Java.
Assignment operators
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 **= 3 # x = x ** 3 → 27.0
x %= 5 # x = x % 5 → 2.0
Python also has augmented assignment for bitwise operations: &=, |=, ^=, >>=, <<=.
Comparison operators
a = 10
b = 20
print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a < b) # True
print(a >= 10) # True
print(a <= 5) # False
Python supports chained comparisons — unusual but useful:
x = 5
print(1 < x < 10) # True — same as (1 < x) and (x < 10)
print(1 < x < 3) # False
print(0 <= x <= 100) # True
Logical operators
a = True
b = False
print(a and b) # False — both must be true
print(a or b) # True — at least one must be true
print(not a) # False — flips the value
Python uses English words instead of symbols (and, or, not instead of &&, ||, !).
Short-circuit evaluation
# and — stops at first False
result = False and expensive_function() # expensive_function never runs
# or — stops at first True
result = True or expensive_function() # expensive_function never runs
Truthy and falsy values
# Falsy: 0, 0.0, "", [], {}, set(), None, False
# Truthy: everything else
if "" or []:
print("This won't print — both are falsy")
if "hello" or [1, 2]:
print("This prints — first is truthy")
Membership operators
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" not in fruits) # True
text = "Hello, World!"
print("World" in text) # True
print("Python" not in text) # True
in checks membership in sequences, strings, and dictionaries.
Identity operators
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True — same content
print(a is b) # False — different objects
print(a is c) # True — same object
print(a is not c) # False
# None should always be compared with is
x = None
print(x is None) # correct
print(x == None) # works but not recommended
== checks value equality. is checks object identity (same memory address). Use is only for None comparisons.
Bitwise operators
a = 12 # 1100 in binary
b = 10 # 1010 in binary
print(a & b) # 8 — AND (1000)
print(a | b) # 14 — OR (1110)
print(a ^ b) # 6 — XOR (0110)
print(~a) # -13 — NOT (inverts all bits)
print(a << 2) # 48 — left shift (multiply by 4)
print(a >> 1) # 6 — right shift (divide by 2)
Operator precedence
# Highest to lowest:
# () — Parentheses
# ** — Exponentiation
# +x, -x, ~x — Unary operators
# *, /, //, % — Multiplicative
# +, - — Additive
# <<, >> — Bitwise shift
# & — Bitwise AND
# ^ — Bitwise XOR
# | — Bitwise OR
# ==, !=, <, <=, >, >= — Comparison
# in, not in, is, is not — Membership/Identity
# not — Logical NOT
# and — Logical AND
# or — Logical OR
When in doubt, use parentheses. They make intent explicit and prevent precedence bugs.
Walrus operator :=
Python 3.8 introduced the walrus operator — assign and use a value in one expression:
import random
# Without walrus
number = random.randint(1, 100)
if number > 50:
print(f"High: {number}")
# With walrus — cleaner
if (number := random.randint(1, 100)) > 50:
print(f"High: {number}")
The walrus operator avoids repeating expensive calculations. Use it when it improves readability.
Mini Practice
- Calculate the remainder of
17 / 5and17 // 5— explain the difference - Use chained comparison to check if a number is between 1 and 100
- Write an expression using
andandorthat checks if a string is non-empty AND starts with "A" - Use
into check if a word exists in a sentence - Evaluate
2 ** 3 ** 2— explain why the answer is 512, not 64
Next: storing collections in lists →
Related Topics
Frequently Asked Questions about Operators
What is Operators in Python?
Operators is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Operators?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.
Why is Operators important in Python?
Operators is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.