Lesson 27 min read

Operators & Expressions

The math and logic behind every decision your code makes

Arithmetic Operators

Python is basically a super-powered calculator. You've got all the usual suspects — plus, minus, multiply, divide — but also some cool extras that most calculators don't have.

  • + Addition, - Subtraction, * Multiplication — the classics.
  • / True division — always gives you a float: 7 / 23.5.
  • // Floor division — divides and rounds down to the nearest whole number: 7 // 23.
  • % Modulo — gives you the remainder after dividing: 7 % 21. Super handy for checking if a number is even or odd!
  • ** Power — exponentiation: 2 ** 101024.

Arithmetic in Action

# Pizza math!
total_slices = 8
people = 3
slices_each = total_slices // people # Floor division
leftovers = total_slices % people # Modulo
print(f"Each person gets {slices_each} slices")
print(f"Leftover slices: {leftovers}")
# Power up!
print(2 ** 10) # 2 to the power of 10
print(9 ** 0.5) # Square root trick!
Output
Each person gets 2 slices
Leftover slices: 2
1024
3.0

Comparison & Logical Operators

Comparison operators let you ask questions about values. The answer is always True or False.

  • == Equal to (two equals signs — one = is assignment!)
  • != Not equal to
  • <, >, <=, >= Less/greater than (or equal)

Logical operators let you combine multiple True/False values:

  • and — Both must be True: True and FalseFalse
  • or — At least one must be True: True or FalseTrue
  • not — Flips the value: not TrueFalse

Making Comparisons

age = 14
has_ticket = True
# Comparison operators
print(age >= 13) # Old enough for the movie?
print(age == 14) # Exactly 14?
print(age != 21) # Not 21?
# Logical operators — combine conditions
can_enter = age >= 13 and has_ticket
print(f"Can enter: {can_enter}")
# Python lets you chain comparisons!
score = 85
print(70 <= score <= 100) # Is score between 70 and 100?
Output
True
True
True
Can enter: True
True

Operator Precedence

Just like in math class, Python follows an order of operations. Here's the priority from highest to lowest:

  1. () — Parentheses first (always wins)
  2. ** — Exponentiation
  3. *, /, //, % — Multiplication family
  4. +, - — Addition family
  5. ==, !=, <, >, etc. — Comparisons
  6. notandor — Logical (not beats and beats or)

When in doubt, use parentheses! They make your code easier to read AND guarantee the right order.

Precedence Surprises

# Without parentheses — can be confusing
result = 2 + 3 * 4
print(result) # 14, not 20! (* before +)
# With parentheses — crystal clear
result = (2 + 3) * 4
print(result) # 20
# Logical precedence trap
x = True or False and False
print(x) # True! (and runs before or)
x = (True or False) and False
print(x) # False (parentheses change everything)
# Augmented assignment — shortcuts!
score = 100
score += 10 # same as score = score + 10
score *= 2 # same as score = score * 2
print(score)
Output
14
20
True
False
220
Note: Think of and as a strict teacher who needs ALL homework done, and or as a chill teacher who's happy if you did ANY of it. And not? That's the kid who does the opposite of whatever you say.

Quick check

What does 17 % 5 return?
Variables & Data TypesStrings & Formatting