Table of Contents
Variables store values. But what do you actually do with those values? You add them, compare them, combine them, check them. That’s where operators come in.
An operator is a symbol that tells Python to perform a specific operation between values or variables. You’ve been using one already — the = sign when assigning variables. That’s an operator too.
Let’s go through the main types.
1. Arithmetic Operators
These are your classic maths operators. Nothing surprising here:
a = 10
b = 3
print(a + b) # 13 — Addition
print(a - b) # 7 — Subtraction
print(a * b) # 30 — Multiplication
print(a / b) # 3.3333... — Division (always gives a float)
print(a // b) # 3 — Floor Division (drops the decimal)
print(a % b) # 1 — Modulus (remainder after division)
print(a ** b) # 1000 — Exponentiation (a to the power of b)
A couple of ones worth highlighting:
The // operator (floor division) gives you the whole number part of a division, discarding anything after the decimal. So 10 // 3 gives you 3, not 3.333.
The % operator (modulus) gives you the remainder. So 10 % 3 gives you 1, because 10 divided by 3 is 3 with 1 left over. This one comes up surprisingly often in logic — for example, checking if a number is even or odd:
number = 8
print(number % 2) # 0 — even number (no remainder)
number = 7
print(number % 2) # 1 — odd number (remainder of 1)
2. Comparison Operators
These compare two values and always return either True or False. They’re the foundation of all decision-making in Python.
a = 10
b = 5
print(a == b) # False — Equal to
print(a != b) # True — Not equal to
print(a > b) # True — Greater than
print(a < b) # False — Less than
print(a >= b) # True — Greater than or equal to
print(a <= b) # False — Less than or equal to
Important one to flag — == (double equals) is comparison. It asks: are these two things equal? Whereas = (single equals) is assignment. It says: set this variable to this value.
Mixing them up is one of the most common beginner mistakes. If you write if a = 5 instead of if a == 5, Python will throw an error. Keep this distinction locked in.
3. Assignment Operators
You already know =. But Python also has shorthand assignment operators that combine an arithmetic operation and assignment into one step:
score = 10
score += 5 # same as score = score + 5 → score is now 15
score -= 3 # same as score = score - 3 → score is now 12
score *= 2 # same as score = score * 2 → score is now 24
score /= 4 # same as score = score / 4 → score is now 6.0
score //= 2 # same as score = score // 2 → score is now 3.0
score **= 3 # same as score = score ** 3 → score is now 27.0
These are just shortcuts. They make your code shorter and cleaner, especially when you’re updating a variable repeatedly — like incrementing a counter or accumulating a total.
4. Logical Operators
Logical operators let you combine multiple conditions together. There are three:
age = 20
has_id = True
# and — both conditions must be True
print(age >= 18 and has_id) # True
# or — at least one condition must be True
print(age >= 18 or has_id) # True
# not — flips True to False and vice versa
print(not has_id) # False
Think of and as strict — everyone needs to qualify. Think of or as lenient — anyone who qualifies gets through. And not simply flips the result.
These become very powerful once you start writing conditions and if-statements — which is coming up soon.
Quick Recap
- Arithmetic operators — +, -, *, /, //, %, ** for maths
- % gives the remainder — useful for checking even/odd
- Comparison operators — ==, !=, >, <, >=, <= always return True or False
- == is comparison, = is assignment — don’t mix them up
- Assignment shortcuts — +=, -=, *= etc. update a variable in one step
- Logical operators — and, or, not combine or flip conditions
Operators are what give your programs their logic and their ability to make decisions. We’ve laid the groundwork here — and once we get into if-statements and loops, you’ll see all of this click into place beautifully. Next up, we talk about Input — making your programs actually interactive.
