Operators are how you do real work in Python — adding numbers, comparing values, building decisions. This chapter covers the seven categories every Python developer uses every day.
Arithmetic operators
The seven arithmetic operators in Python:
a = 17b = 5print(a + b) # 22 additionprint(a - b) # 12 subtractionprint(a * b) # 85 multiplicationprint(a / b) # 3.4 float divisionprint(a // b) # 3 integer division (floor)print(a % b) # 2 modulus (remainder)print(a ** b) # 1419857 exponentiation
Comparison operators
Comparisons always return a bool — either True or False.
x, y = 10, 20print(x == y) # False equalprint(x != y) # True not equalprint(x < y) # Trueprint(x > y) # Falseprint(x <= 10) # Trueprint(y >= 25) # False
Logical operators
Combine conditions with and, or, not:
age = 22has_id = True# you can enter if you're 18+ AND have IDif age >= 18 and has_id:print("Welcome in.")# you skip the queue if you have a member card OR a VIP passmember = Falsevip = Trueif member or vip:print("Skip the queue.")# inversionprint(not member) # True
Assignment operators
Shorthand for "operate then reassign". Two patterns you'll use the most:
total = 0total += 50 # equivalent to total = total + 50total += 30total -= 10total *= 2print(total) # 140
Membership & identity
Two pairs that confuse beginners but are actually simple:
fruits = ["apple", "mango", "banana"]# membership: is value in collection?print("mango" in fruits) # Trueprint("kiwi" not in fruits) # True# identity: do two names point at the SAME object?a = [1, 2, 3]b = ac = [1, 2, 3]print(a is b) # True — same list in memoryprint(a is c) # False — different lists with equal contentsprint(a == c) # True — values are equal
Key takeaways
- 01Arithmetic: +, -, *, /, //, %, ** — note that / is float division, // is integer division.
- 02Comparison operators always return True or False.
- 03Use and / or / not to combine conditions; Python short-circuits left-to-right.
- 04Compound assignment (+=, -=, *=, /=) keeps your code readable.
- 05Use == for "values equal", is for "same object in memory" (commonly is None).
