Arithmetic Operators
Learn how Python does math — from basic addition to the surprisingly handy modulo operator — and understand the difference between regular and floor division.
Computers are incredible calculators. Before we teach Python to make decisions or repeat tasks, we need to teach it to do math. Python supports all the arithmetic you learned in school — plus a few operators you may not have seen before that turn out to be incredibly useful.
In this lesson we will walk through every arithmetic operator, run real examples, and pick up a few insights that will save you from common beginner stumbles.
See it in action
— step through the idea, then dive into the details below.Python Does Math Too
Python has seven arithmetic operators — the four you know from school, plus three powerful extras. Master these and you can make Python calculate almost anything.
#The Basics: + - * /
The four everyday operators work exactly as you would expect. You can use them with whole numbers (int) or decimal numbers (float).
print(10 + 3) # addition
print(10 - 3) # subtraction
print(10 * 3) # multiplication
print(10 / 3) # divisionDivision always returns a float
Even 10 / 2 returns 5.0, not 5. The / operator always produces a float in Python 3. This is intentional — Python wants to be honest that division can produce a fraction.
#Floor Division: //
Sometimes you only care about the whole number part of a division result — no decimals, no rounding, just chop everything after the decimal point. That is what // (floor division) does.
Think of it like this: if you have 10 cookies and 3 friends, how many whole cookies does each person get? The answer is 3 — you ignore the leftover.
print(10 / 3) # regular division -> float
print(10 // 3) # floor division -> int (whole part only)Floor division rounds DOWN, not toward zero
With negative numbers, // floors toward negative infinity, not toward zero.
-7 // 2 gives -4, not -3.
This surprises many beginners. For positive numbers it behaves exactly like truncating, but keep this in mind when working with negatives.
print(-7 // 2) # floors toward negative infinity
print(7 // 2) # floors toward zero (same as truncation here)#Modulo: %
The % operator gives you the remainder after division. Going back to the cookie example: 10 cookies shared among 3 friends — each gets 3, and there is 1 left over. That leftover is what % returns.
print(10 % 3) # 10 divided by 3 is 3 remainder 1
print(15 % 5) # 15 divided by 5 is 3 remainder 0
print(7 % 2) # 7 divided by 2 is 3 remainder 1Modulo is like a clock
Imagine a 12-hour clock. After 12, the hours wrap back to 1. Modulo works the same way — it wraps numbers around. 14 % 12 gives 2, just like 14:00 is 2 o'clock on a 12-hour clock.
This wrapping property makes % useful for things like cycling through a list or alternating between two states.
One of the most common uses of % is checking whether a number is even or odd. An even number has no remainder when divided by 2; an odd number has a remainder of 1.
number = 42
print(number % 2) # 0 means even
number = 7
print(number % 2) # 1 means odd#Exponentiation: **
The ** operator raises a number to a power. 2 ** 8 means "2 to the power of 8", which is 2 multiplied by itself 8 times.
print(2 ** 8) # 2 to the power of 8
print(3 ** 3) # 3 cubed
print(9 ** 0.5) # square root of 9 (power of 0.5)Square root trick
You do not need a special function for square roots. Because the square root of a number is the same as raising it to the power of 0.5, you can write x ** 0.5 and Python handles it perfectly.
#Mixing int and float
What happens when you mix a whole number and a decimal in the same expression? Python promotes the result to float to avoid losing any precision. Think of it as Python being careful — it would rather give you too much information than too little.
print(type(3 + 2)) # int + int -> int
print(type(3 + 2.0)) # int + float -> float
print(type(10 // 2)) # floor div -> int
print(type(10 // 2.0)) # floor div with float -> float#Order of Operations
Python follows the same order of operations you learned in math class — often remembered as PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). When in doubt, use parentheses to make your intention clear.
print(2 + 3 * 4) # multiplication first -> 14
print((2 + 3) * 4) # parentheses first -> 20
print(2 ** 3 ** 2) # ** is right-to-left: 2 ** (3**2) = 2**9 = 512** is right-associative
2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512, NOT (2 ** 3) ** 2 = 64. Exponentiation chains from right to left. When chaining **, always use parentheses to be explicit.
What does `17 % 5` evaluate to?
Key takeaways
- `/` always returns a `float`; use `//` when you only want the whole-number part of a division.
- The `%` (modulo) operator returns the remainder — great for even/odd checks and wrapping values.
- `**` raises a number to a power; `x ** 0.5` is a handy way to compute a square root.
- Mixing an `int` and a `float` in any expression promotes the result to `float`.
- Parentheses override all other precedence rules — use them freely to make your math intentions clear.
What does this code print?
print(10 / 2)
print(10 // 3)
print(10 % 3)This code should print whether 42 is even, but it has a bug. What is wrong?
number = 42
print(number / 2)Complete the code so it prints the square root of 25 (which is 5.0).
result = 25 0.5 print(result)
Put these lines in the right order so the code converts 90 seconds into minutes and leftover seconds, then prints both.
leftover = total % 60
total = 90
minutes = total // 60
print(minutes, "minute(s)", leftover, "second(s)")
Write a small Python program that takes the number seconds = 3661 and prints: 1. How many complete hours are in that many seconds. 2. How many leftover minutes after removing the hours. 3. How many leftover seconds after removing the hours and minutes.
Expected output: `` 1 hour(s) 1 minute(s) 1 second(s) ``
Hint: there are 3600 seconds in an hour and 60 seconds in a minute.
Try it live — edit the code and hit Run to execute real Python: