a + b, the + is the operator and a, b are the operands. Python provides seven families of operators — arithmetic, relational, logical, assignment, augmented assignment, identity, and membership — which together give you everything needed to compute, compare, decide and transform data.
- Unary — one operand, e.g.,
-x(negation),not flag. - Binary — two operands, e.g.,
a + b,x < y,name in users.
not and the unary minus.
10.1 The Seven Families of Operators
10.2 Arithmetic Operators
Seven symbols — the usual four from school plus three extras that Python borrows from mathematics.
| Operator | Name | Example (a=13, b=4) | Result |
|---|---|---|---|
+ | Addition | a + b | 17 |
− | Subtraction | a − b | 9 |
* | Multiplication | a * b | 52 |
/ | True division (always returns float) | a / b | 3.25 |
// | Floor / integer division | a // b | 3 |
% | Modulus (remainder) | a % b | 1 |
** | Exponent (power) | a ** b | 28561 |
a, b = 13, 4 print(a + b, a - b, a * b) # 17 9 52 print(a / b) # 3.25 — always a float print(a // b) # 3 — quotient (floor) print(a % b) # 1 — remainder print(a ** b) # 28561 — 13⁴ print(-a) # -13 — unary minus
n % 2 == 0 is True for even n. Divisibility — n % k == 0 is True when n is divisible by k. You will use these all through the loops unit.
/ vs //. In Python 3 the plain / always gives a float — even for whole results. If you want integer quotient, use //.
print(10 / 2) # 5.0 (float) print(10 // 2) # 5 (int) print(-7 // 2) # -4 (floor division rounds TOWARDS NEGATIVE INFINITY)
10.3 Relational (Comparison) Operators
Compare two values and produce a Boolean — either True or False.
| Operator | Reads as | Example | Result |
|---|---|---|---|
== | equal to | 5 == 5 | True |
!= | not equal to | 5 != 3 | True |
< | less than | 4 < 9 | True |
> | greater than | 4 > 9 | False |
<= | less than or equal to | 5 <= 5 | True |
>= | greater than or equal to | 5 >= 6 | False |
marks = 72 print(marks >= 33) # True — pass print(marks == 100) # False print("abc" < "abd") # True — strings compare dictionary-order print([1, 2] == [1, 2]) # True — lists compare element-wise
age = 16 print(13 <= age < 20) # True — means "13 ≤ age AND age < 20"
= with ==.
=is assignment — stores a value.==is comparison — asks "is it equal?".
if x = 10: is a syntax error; you meant if x == 10:.
10.4 Logical Operators
Combine Boolean values. Python uses the English keywords and, or, not — not the C-style &&, ||, !.
| Operator | Returns True when… | Example | Result |
|---|---|---|---|
and | both operands are True | True and False | False |
or | at least one operand is True | True or False | True |
not | the operand is False (flips it) | not True | False |
age = 17 has_id = True # Is this person allowed to vote AND does she carry ID? print(age >= 18 and has_id) # False — age is 17 print(age >= 18 or has_id) # True — one branch holds print(not has_id) # False
False and anything→False(second operand is skipped).True or anything→True(second operand is skipped).
if x != 0 and 10/x > 1: — the division is never attempted when x == 0.
10.5 Assignment Operator — =
A single = binds a name (L-value) to a value (R-value). Python also allows two neat shortcuts:
| Form | Example | Effect |
|---|---|---|
| Simple | x = 10 | Name x now refers to 10. |
| Multiple targets | a = b = c = 0 | All three names refer to the same value 0. |
| Tuple unpacking | x, y = 10, 20 | Name x gets 10; y gets 20 — in one line. |
| Swap | a, b = b, a | Swaps the two values without a temp variable — a Python classic. |
x, y = 10, 20 print(x, y) # 10 20 x, y = y, x print(x, y) # 20 10 — swap in one line
10.6 Augmented Assignment Operators
A compact way of saying "take the current value, apply an arithmetic operation, store back". Every arithmetic operator has an augmented form.
| Augmented | Meaning | Equivalent |
|---|---|---|
x += 5 | Add 5 to x | x = x + 5 |
x -= 5 | Subtract 5 | x = x - 5 |
x *= 5 | Multiply by 5 | x = x * 5 |
x /= 5 | True divide | x = x / 5 |
x //= 5 | Floor divide | x = x // 5 |
x %= 5 | Modulus | x = x % 5 |
x **= 5 | Power | x = x ** 5 |
total = 0 for marks in [85, 72, 91]: total += marks # same as total = total + marks print(total) # 248 count = 1 count *= 2 # 2 count **= 3 # 8 (2 ** 3)
++ or −− operators (C-style increment/decrement). Use x += 1 or x -= 1.
10.7 Identity Operators — is & is not
Identity operators ask: "Are the two names pointing to the same object in memory?" — not whether the values happen to be equal.
| Operator | True when… |
|---|---|
is | both operands are the same object (id(a) == id(b)) |
is not | operands are different objects |
a = [1, 2, 3] b = [1, 2, 3] # equal in value, but a fresh object c = a # c is the same object as a (alias) print(a == b) # True — values match print(a is b) # False — different objects in memory print(a is c) # True — same object print(a is not b) # True
is. Almost always only with the sentinels None, True, False. For ordinary value comparison use ==.
if result is None: # ✓ the idiomatic way ... if x is 1000: # ❌ unreliable — do not rely on int identity ...
10.8 Membership Operators — in & not in
Ask whether a value appears inside a sequence or collection — a string, list, tuple, dictionary (its keys), or any iterable. Returns a Boolean.
| Operator | Meaning |
|---|---|
in | True if the value is found in the sequence |
not in | True if the value is not found |
vowels = "aeiou" print("e" in vowels) # True print("z" not in vowels) # True marks = [85, 72, 91, 68] print(72 in marks) # True print(100 not in marks) # True student = {"name": "Aarav", "roll": 22} print("name" in student) # True — checks KEYS of a dict print("Aarav" in student) # False — not a key
choice = input("y/n? ").lower() if choice in ("y", "yes"): print("Confirmed") elif choice in ("n", "no"): print("Cancelled") else: print("Please type y or n")
10.9 The Whole Menu — One-page Cheat Sheet
| Family | Operators | Returns |
|---|---|---|
| Arithmetic | + − * / // % ** | Number |
| Relational | == != < > <= >= | Boolean |
| Logical | and or not | Boolean |
| Assignment | = | — |
| Augmented | += −= *= /= //= %= **= | — |
| Identity | is is not | Boolean |
| Membership | in not in | Boolean |
10.9.1 Operator Precedence (preview)
When several operators appear in one expression, Python evaluates them in a fixed order. A detailed table is covered in Chapter 11 — Expressions, but here is the rough picture, highest precedence first:
** (exponent)2.
+x, -x (unary)3.
* / // %4.
+ −5.
< <= > >= == != (relational)6.
is is not in not in7.
not8.
and9.
or10.
= += −= … (assignment, lowest)
print(2 + 3 * 4) # 14 (not 20) print((2 + 3) * 4) # 20
📌 Quick Revision — Chapter 10 at a Glance
- Operator = symbol or keyword that acts on one or more operands. Unary = 1 operand; binary = 2 operands.
- 7 families: Arithmetic · Relational · Logical · Assignment · Augmented · Identity · Membership.
- Arithmetic:
+ − * / // % **. Remember/always gives a float;//gives integer quotient. - Relational:
== != < > <= >=→ returns Boolean. Chained comparisons work:0 < x < 10. - Logical:
and · or · not(English keywords, not&&/||). Short-circuit evaluation — second operand skipped when result is already known. - Assignment
=— binds name to value. Multiple targetsa = b = 0; tuple unpackingx, y = 10, 20; swapa, b = b, a. - Augmented
+= −= *= /= //= %= **=— compact form ofx = x op value. - Python has no
++/−−— usex += 1. - Identity
is/is not— asks if two names point to the same memory object (sameid()). Use only forNone,True,False. - Membership
in/not in— asks if a value appears in a sequence / collection. For dicts it tests keys. - Precedence (rough):
**→ unary-→* / // %→+ -→ comparison →not→and→or→ assignment. Use parentheses to be safe.