Python operators guide arithmetic comparison logical examples

Python operators — complete guide with the mistakes everyone makes

If you’re already comfortable with variables and data types, the next natural step is Python operators. They’re the tools that let you perform calculations, compare values and make decisions in your code.

In this article we cover every type of Python operators you need in your first year, with the three mistakes I saw most often in class explained in detail.

What are Python operators?

An operator is a symbol that tells Python what operation to perform with one or more values. For example the + in 3 + 5 is an operator that tells Python to add those two values together.

Python has several types of operators. We’ll look at them from most to least used.

Arithmetic operators in Python — the everyday ones

These are the ones you’ll use in almost every program:

# Addition
print(5 + 3)    # → 8

# Subtraction
print(5 - 3)    # → 2

# Multiplication
print(5 * 3)    # → 15

# Normal division
print(5 / 2)    # → 2.5  (always returns float)

# Floor division
print(5 // 2)   # → 2    (rounds down to integer)

# Modulo (remainder)
print(5 % 2)    # → 1

# Power
print(5 ** 2)   # → 25

Classic mistake 1 — confusing / with //

This is the error I saw most often in FP1 exercises. Normal division / in Python always returns a decimal number (float), even when the result is exact:

print(10 / 2)   # → 5.0  NOT 5, it's 5.0
print(10 // 2)  # → 5    this is a proper integer

When to use each one? Use / when you need decimals, calculating averages or percentages. Use // when you need a whole number, counting how many rows fit on a screen or dividing people into equal groups.

Classic mistake 2 — % is not percentage

When you see % in Python it doesn’t mean percentage. It means modulo — the remainder of an integer division. It’s one of the most useful operators but also the most confusing at first:

print(10 % 3)   # → 1   because 10 = 3×3 + 1
print(15 % 5)   # → 0   because 15 = 5×3 + 0
print(7 % 2)    # → 1   because 7 = 2×3 + 1

What’s it useful for in practice? Checking if a number is even or odd:

number = 7
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

If the remainder of dividing by 2 is 0, the number is even. If it’s 1, it’s odd. You’ll see this in a huge number of FP1 exercises.

It’s also useful for checking if a number is divisible by another:

print(15 % 3)   # → 0  15 is divisible by 3
print(16 % 3)   # → 1  16 is not divisible by 3

Comparison operators — return True or False

These operators compare two values and return a boolean — True if the comparison holds, False if it doesn’t:

print(5 > 3)    # → True
print(5 < 3)    # → False
print(5 >= 5)   # → True
print(5 <= 4)   # → False
print(5 == 5)   # → True   (equal)
print(5 != 3)   # → True   (not equal)

Classic mistake 3 — confusing = with ==

This is probably the most frequent mistake in first year and the one that wastes the most time when debugging:

# = is assignment — stores a value in a variable
x = 5           # x now equals 5

# == is comparison — checks if two values are equal
print(x == 5)   # → True
print(x == 3)   # → False

The typical problem is using = when you meant to compare:

# WRONG — this gives a syntax error in Python
if x = 5:
    print("It's five")

# RIGHT
if x == 5:
    print("It's five")

Python gives a syntax error if you try to use = inside an if, so at least this mistake gets caught quickly. But in other contexts it can go unnoticed, so get used to always using == to compare and = to assign.

Logical operators — combining conditions

When you need to check multiple conditions at once you use logical operators:

# and — both conditions must be True
age = 20
has_id = True
print(age >= 18 and has_id)   # → True

# or — at least one condition must be True
is_student = True
has_discount = False
print(is_student or has_discount)   # → True

# not — inverts the boolean value
passed = False
print(not passed)   # → True

In practice you’ll use them constantly in the if statements of your programs:

grade = 6.5
attendance = 80

if grade >= 5 and attendance >= 75:
    print("Passed")
else:
    print("Failed")

Compound assignment operators — the shortcut that saves time

Python has a compact version of arithmetic operators combined with assignment. They’re very useful inside loops:

x = 10

x += 5    # equivalent to x = x + 5  → x is now 15
x -= 3    # equivalent to x = x - 3  → x is now 12
x *= 2    # equivalent to x = x * 2  → x is now 24
x /= 4    # equivalent to x = x / 4  → x is now 6.0
x //= 2   # equivalent to x = x // 2 → x is now 3.0
x **= 2   # equivalent to x = x ** 2 → x is now 9.0
x %= 4    # equivalent to x = x % 4  → x is now 1.0

By far the most used is += — you’ll see it in every accumulator and counter in FP1.

Operator precedence — order matters

Python follows a priority order for evaluating operations, just like in mathematics. From highest to lowest priority:

# 1. Parentheses           ( )
# 2. Power                 **
# 3. Multiplication, division, modulo  * / // %
# 4. Addition and subtraction          + -
# 5. Comparison            > < >= <= == !=
# 6. not
# 7. and
# 8. or

In practice this means these two expressions give different results:

print(2 + 3 * 4)      # → 14  (3*4=12 first, then 2+12)
print((2 + 3) * 4)    # → 20  (2+3=5 first, then 5*4)

Golden rule: when in doubt, use parentheses. They make the code more readable and prevent precedence errors. Better to have one parenthesis too many than one too few.

Visualise it with Python Tutor

To understand how Python evaluates an expression step by step, paste this into pythontutor.com and run it line by line:

a = 10
b = 3

addition = a + b
subtraction = a - b
division = a / b
floor_division = a // b
modulo = a % b
power = a ** b

print(addition, subtraction, division, floor_division, modulo, power)

You’ll see how Python creates each variable with its value in memory. Especially useful for seeing the difference between / and //, one creates a float, the other an integer.

Quick summary

# ARITHMETIC OPERATORS
5 + 3    # → 8    addition
5 - 3    # → 2    subtraction
5 * 3    # → 15   multiplication
5 / 2    # → 2.5  division (always float)
5 // 2   # → 2    floor division (integer)
5 % 2    # → 1    modulo (remainder — not percentage)
5 ** 2   # → 25   power

# COMPARISON OPERATORS — return True or False
>  <  >=  <=  ==  !=

# LOGICAL OPERATORS
and   # both conditions True
or    # at least one True
not   # inverts the boolean

# COMPOUND ASSIGNMENT
x += 5   x -= 3   x *= 2   x /= 4   x //= 2   x **= 2   x %= 4

# THE THREE KEY RULES
# 1. / always returns float — use // for integer division
# 2. % is remainder — not percentage
# 3. = assigns, == compares — never mix them up

# PRECEDENCE (high to low)
# () → ** → * / // % → + - → > < >= <= == != → not → and → or
# When in doubt — use parentheses

In the next article we practice all these operators with real programs in VS Code.

Similar Posts

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *