Introduction
Python is a calculator that never makes arithmetic mistakes, as long as you tell it exactly what to do. This lesson covers the seven arithmetic operators and the order Python uses them in.
Arithmetic is everywhere in programs, and even simple calculations have details that surprise beginners. Knowing exactly what /, // and % do will save you from many wrong answers later.
How it works
The arithmetic operators
a = 17
b = 5
print(a + b, a - b, a * b)
print(a / b)
print(a // b, a % b)
print(2 ** 10)| Operator | Meaning | 17 ? 5 gives |
|---|---|---|
+ | add | 22 |
- | subtract | 12 |
* | multiply | 85 |
/ | divide | 3.4 |
// | divide and round down | 3 |
% | remainder | 2 |
** | power | 1419857 |
Whole numbers are called int; numbers with a decimal point are float. / always gives a float, even when the division is exact: 10 / 2 is 5.0.
Which operator goes first?
x = 2 + 3 * 4
y = (2 + 3) * 4
print(x, y)Python follows the same order as mathematics: ** first, then *, /, // and %, and finally + and -. So 2 + 3 * 4 is 14. Brackets change the order: (2 + 3) * 4 is 20. When in doubt, add brackets; they make the code clearer too.
Syntax
7 / 2 # 3.5
7 // 2 # 3
7 % 2 # 1
2 ** 3 # 8
(1 + 2) * 3
Example
// tells you how many whole times one number fits into another, and % tells you what is left over. Together they solve many everyday problems, such as making change:
taka = 1270
hundreds = taka // 100
rest = taka % 100
print(hundreds, "notes of 100, and", rest, "taka left")Step through it. On line 2 the calculation box shows taka // 100 → 1270 // 100 → 12, and on line 3 it shows taka % 100 → 1270 % 100 → 70.
Line by line
taka = 1270stores the amount.taka // 100asks how many whole hundreds fit, which is 12.taka % 100asks what is left over, which is 70.printshows the two results with some words between them.
Expected result
12 notes of 100, and 70 taka left
In the real world
% tells you whether a number is even, which row of a grid an item is on, and what time it will be in 500 minutes. Banks and shops split amounts into notes and coins with exactly these two operators.
Common mistakes
- Expecting
7 / 2to be3. It is3.5; use7 // 2for the whole-number part. - Writing
2 ^ 3for a power. In Python^means something else; write2 ** 3. //rounds down, so-7 // 2is-4, not-3.- Forgetting that a float can be slightly inexact, so
0.1 + 0.2is not exactly0.3.
Best practices
- Add brackets whenever the order is not obvious.
- Use
//and%together to split a value into units. - Round a float only when you show it.
Summary
+, -, *, /, //, % and ** do arithmetic. / gives a float and // rounds down. % gives the remainder. Python does powers first, then multiplying and dividing, then adding and subtracting, and brackets change that order.
Practice
A day has 86400 seconds. Write a program that takes seconds = 100000 and prints how many whole days, hours, minutes and seconds that is, using // and %.
Quick check
Key takeaways
- / always gives a float, and // gives the whole number of times.
- // and % together split a value into parts.
- Brackets make the order clear.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.