// python · beginner

Python Operators and Maths: //, %, ** and Order of Operations

Learn every Python arithmetic operator, the difference between / and //, how % (modulo) works, powers, rounding, the order of operations, and the math module.

Computers are very good at maths, and Python makes calculations easy to write. But a few operators work differently from a calculator, and they catch out nearly every beginner: there are two kinds of division, % has nothing to do with percentages, and ^ doesn’t mean “to the power of”. This guide clears all of that up.

The arithmetic operators

Operator Name Example Result
+ addition 7 + 2 9
- subtraction 7 - 2 5
* multiplication 7 * 2 14
/ division 7 / 2 3.5
// floor (whole-number) division 7 // 2 3
% modulo (remainder) 7 % 2 1
** power 7 ** 2 49

You can try any of these straight away in the Python shell (type python in a terminal; see your first program).

Two kinds of division

/ always gives a float (a decimal number), even when the answer is whole:

python
print(10 / 2)    # 5.0
print(7 / 2)     # 3.5

// divides and then rounds down to a whole number:

python
print(7 // 2)    # 3
print(10 // 3)   # 3
print(-7 // 2)   # -4  (rounds down, so further from zero)

That last one surprises people: “rounding down” means towards negative infinity, so -3.5 becomes -4, not -3.

Modulo: the remainder

% gives the remainder after division. It’s far more useful than it first looks:

python
print(17 % 5)    # 2   (17 = 3 × 5 + 2)
print(10 % 2)    # 0   (no remainder: 10 is even)
print(11 % 2)    # 1   (remainder 1: 11 is odd)

Common uses:

python
n = 42
if n % 2 == 0:
    print("even")

minutes = 135
print(f"{minutes // 60}h {minutes % 60}m")   # 2h 15m

Using // and % together, as in the time example, splits a number into “how many whole groups” and “what’s left over”.

Powers

Use ** for powers. Don’t use ^, which means something completely different in Python (a bitwise XOR):

python
print(2 ** 10)   # 1024
print(9 ** 0.5)  # 3.0  (a power of 0.5 is a square root)
print(2 ^ 3)     # 1   (not 8!)

Order of operations

Python follows the usual maths rules, often remembered as BIDMAS or PEMDAS:

  1. Brackets ( )
  2. Indices (powers) **
  3. Division and Multiplication / // % *, left to right
  4. Addition and Subtraction + -, left to right
python
print(2 + 3 * 4)      # 14, not 20
print((2 + 3) * 4)    # 20
print(10 - 4 - 3)     # 3  (left to right)
print(2 ** 3 ** 2)    # 512 (powers go right to left: 3 ** 2 first)

When in doubt, add brackets. They cost nothing and make your intention obvious to anyone reading the code.

Shortcut assignment operators

These update a variable using its current value:

python
score = 10
score += 5    # score = score + 5  -> 15
score -= 3    # -> 12
score *= 2    # -> 24
score //= 5   # -> 4
print(score)

Integers, floats and rounding

Mixing an int and a float gives a float:

python
print(3 + 2.0)   # 5.0

Floats are stored in binary, so some decimals can’t be represented exactly:

python
print(0.1 + 0.2)          # 0.30000000000000004
print(round(0.1 + 0.2, 2))  # 0.3

That isn’t a Python bug; almost every programming language does it. For display, round or format the number. f-strings are perfect for this (f"{value:.2f}", see the variables guide).

round() has one more surprise: when a number is exactly halfway, it rounds to the nearest even number:

python
print(round(2.5))   # 2
print(round(3.5))   # 4
print(round(2.567, 2))  # 2.57

This is called banker’s rounding, and it avoids always rounding upwards. If you need a whole number in a particular direction, use the math module.

The math module

For anything beyond basic arithmetic, import math (see modules and pip):

python
import math

print(math.sqrt(81))     # 9.0
print(math.floor(4.7))   # 4  (always down)
print(math.ceil(4.2))    # 5  (always up)
print(math.pi)           # 3.141592653589793
print(abs(-12))          # 12 (abs is built in, no import needed)

math.ceil is great for “how many do I need?” questions:

python
import math

pupils = 62
seats_per_bus = 15
print(math.ceil(pupils / seats_per_bus))   # 5 buses

Mini project: a pizza-sharing calculator

python
import math

people = int(input("How many people? "))
slices_each = int(input("Slices per person? "))
slices_per_pizza = 8

total_slices = people * slices_each
pizzas = math.ceil(total_slices / slices_per_pizza)
spare = pizzas * slices_per_pizza - total_slices

print(f"Order {pizzas} pizzas.")
print(f"There will be {spare} spare slices.")

For 7 people eating 3 slices each, that’s 21 slices: 3 pizzas, with 3 spare. Challenge: ask for the price of one pizza and print the cost per person to two decimal places.

Key points

  • / always gives a float; // divides and rounds down.
  • % gives the remainder, handy for even/odd checks and splitting units.
  • Use ** for powers, never ^.
  • Brackets first, then powers, then * / // %, then + -.
  • Floats can be slightly inexact; round or format them for display.
  • math.floor, math.ceil and math.sqrt cover most other needs.

Next: make decisions with these numbers in If Statements and Loops in Python.