Last 30 Days
No notifications
Operators are special symbols that perform operations on values (operands). Python supports a rich set of operators for arithmetic, comparison, logic, and more.
| Operator | Name | Example | Result | ||||
+ | Addition | 7 + 3 | 10 | ||||
- | Subtraction | 7 - 3 | 4 | ||||
* | Multiplication | 7 * 3 | 21 | ||||
/ | Division | 7 / 3 | 2.333... | ||||
// | Floor Division | 7 // 3 | 2 | ||||
% | Modulus | 7 % 3 | 1 | ||||
| Exponent | 2 10 | 1024 | Comparison Operators | Operator | Meaning | Example |
== | Equal | 5 == 5 → True | |||||
!= | Not equal | 5 != 3 → True | |||||
< | Less than | 3 < 5 → True | |||||
> | Greater than | 5 > 3 → True | |||||
<= | Less or equal | 5 <= 5 → True | |||||
>= | Greater or equal | 6 >= 5 → True | Logical Operators | Operator | Description | Example | |
and | Both True | True and False → False | |||||
or | At least one True | True or False → True | |||||
not | Negate | not True → False |
x = 10 # Assign
x += 5 # x = x + 5 → 15
x -= 3 # x = x - 3 → 12
x *= 2 # x = x * 2 → 24
x //= 5 # x = x // 5 → 4** → -, +, ~ (unary) → *, /, //, % → +, - → comparisons → not → and → orOperators are the verbs of programming — add, compare, combine, test. Python keeps them close to maths notation but with a few twists worth knowing (especially / vs //, is vs ==, and short-circuit logic).
An operator is just a function in disguise. a + b is sugar for a.__add__(b). That's why "hi" + "!" works (string __add__ does concatenation) and why "a" * 3 repeats. Same symbol, different behaviour per type.
| Family | Operators |
| Arithmetic | + - * / // % ** |
| Comparison | == != < <= > >= |
| Logical | and or not |
| Assignment | = += -= *= /= //= %= **= |
| Identity / Membership | is is not in not in |
Learn these well before bitwise.
7 / 2 # 3.5 — true division (always float)
7 // 2 # 3 — floor division
-7 // 2 # -4 — floors toward negative infinity
7 % 2 # 1 — remainder
2 ** 10 # 1024 — power, beats math.pow on ints/ *always* returns a float in Python 3, even for whole results (4 / 2 is 2.0). Use // when you need an integer.
1. a == None. Works but unidiomatic. Use a is None.
2. Comparing floats with ==. Use math.isclose(a, b) because 0.1 + 0.2 != 0.3.
3. a + b across types. "3" + 4 is a TypeError. Convert first.
4. Forgetting operator precedence. a or b and c is a or (b and c). Add parens when in doubt.
5. Using is for value equality on ints/strings. Works for small ints by accident; breaks for big ones. Use ==.
6. x = y = [] then mutating. Both names share the same list — same trap as default args.
Python lets you chain comparisons like in maths:
1 < x < 10 # 1 < x AND x < 10
a == b == c # all three equal
low <= score < high # half-open rangeEach term is evaluated once, left to right, with short-circuiting. Massively cleaner than 1 < x and x < 10.
and / orand returns the first falsy operand (or the last if all truthy). or returns the first truthy operand (or the last if all falsy). The result is the value, not just True/False:
name = user_input or "guest" # default-fallback idiom
user and user.is_active # avoids AttributeError if user is NoneFor strict booleans, use bool(...) explicitly. Modern Python often prefers x if x else default or the walrus operator for clarity.
count += 1 # count = count + 1
items += [4] # for a list, modifies in place
total *= 1.05 # apply 5% growthFor mutable types (list, set), += may modify in place — watch out when the same list is shared by multiple names.
a = [1, 2]
b = [1, 2]
a == b # True — same value
a is b # False — different objects
c = a
a is c # True — same objectUse is only for *singletons*: None, True, False. Use == for everything else.
in5 in [1, 2, 5] # True — O(n) for lists
"a" in {"a", "b"} # True — O(1) for sets / dict keys
"py" in "python" # True — substring testFor repeated membership checks on big collections, convert to a set first.
From highest to lowest (the bits you'll trip over):
** (power, right-assoc)
+ - (unary)
- / // %
+ - (binary)
<< >> (bit shift)
& ^ | (bitwise)
== != < ... (comparison + identity + membership)
not
and
or
= += ... (assignment, lowest)When in doubt, parens. Readability beats cleverness.
| Op | Meaning | Example | ||
& | AND | 5 & 3 → 1 | ||
| OR | 5 | 3 → 7 | |
^ | XOR | 5 ^ 3 → 6 | ||
~ | NOT | ~5 → -6 | ||
<< | left shift | 1 << 3 → 8 | ||
>> | right shift | 8 >> 2 → 2 | Real-world: bit flags ( |
:= (Python 3.8+)Assigns inside an expression. Useful in while and comprehensions:
while (line := f.readline()):
process(line)[y for x in data if (y := transform(x)) is not None]
Use sparingly — unreadable code is worse than a bit of duplication.
Any class can implement __add__, __lt__, __eq__, __contains__, etc., to customise how operators behave on its instances. That's how NumPy makes a + b element-wise and how pathlib makes path / "file.txt" build subpaths.
1. Print the result of 7 / 2, 7 // 2, 7 % 2, 2 ** 10 — confirm the types match your guess.
2. Use chained comparison to validate a score is between 0 and 100 in one line.
3. Implement a default-fallback with or and another with the walrus operator; compare readability.
4. Demonstrate that a is b is unreliable for big ints by trying a, b = 1000, 1000 then a, b = 1000, 10**3.