PY-101 ยท Operators
Arithmetic operators
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Arithmetic operators do maths on numbers: + - * and / work as you expect, // divides and drops the remainder, % gives the remainder, and ** raises to a power. / always returns a float. Python follows the usual order: brackets, powers, then * / // %, then + and -.
- Use the seven arithmetic operators
- Know the difference between / and //
- Use % to find remainders
- Control order with brackets
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
a = 17b = 5print(a + b)print(a - b)print(a * b)print(a / b)print(a // b)print(a % b)print(2 ** 10)print(2 + 3 * 4)print((2 + 3) * 4)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
22
12
85
3.4
3
2
1024
14
20 ๐ฏ A film runs for 135 minutes. Use // and % to print it as: 2 h 15 min
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
hours = minutes // 60 and rest = minutes % 60. Then print(hours, "h", rest, "min")
- The problemA film is 135 minutes long and the cinema app shows 2 h 15 min. How does it split 135 into 2 and 15? With two operators most beginners have never met: // and %.
- The Python idea
a + b a - b a * b a / bArithmetic operators do maths on numbers: + - * and / work as you expect, // divides and drops the remainder, % gives the remainder, and ** raises to a power. - In real lifePaging through search results: page_count = (total + per_page - 1) // per_page. With 101 results and 10 per page you get 11 pages.
Start with a question
A film is 135 minutes long and the cinema app shows 2 h 15 min. How does it split 135 into 2 and 15? With two operators most beginners have never met: // and %.
Prices, timers, pagination and game scores are all arithmetic. // and % in particular show up everywhere: splitting minutes into hours, checking even numbers, wrapping around a grid.
Explain it like I'm 10
Python is a calculator that never slips. // asks "how many whole times does it fit?" and % asks "what is left over?"
The technical explanation
17 / 5 is 3.4. / always gives a float, even 10 / 2 gives 5.0.
17 // 5 is 3: how many whole times 5 fits in 17. 17 % 5 is 2: what is left over.
n % 2 == 0 is the standard test for an even number.
Python works out 2 + 3 * 4 as 14 because * comes before +. Brackets make the order clear: (2 + 3) * 4 is 20.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Add | 17 + 5 | 22 |
- | Subtract | 17 - 5 | 12 |
* | Multiply | 17 * 5 | 85 |
/ | Divide | 17 / 5 | 3.4 |
// | Floor divide | 17 // 5 | 3 |
% | Modulus | 17 % 5 | 2 |
** | Power | 2 ** 10 | 1024 |
Syntax
a + b a - b a * b a / b
a // b a % b a ** b Real-world scenario
Paging through search results: page_count = (total + per_page - 1) // per_page. With 101 results and 10 per page you get 11 pages.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Comparison and logical operators โQuestions people ask
What is the difference between / and // in Python?
/ is true division and always returns a float (7 / 2 is 3.5). // is floor division and returns the whole-number part, rounded down (7 // 2 is 3).
What does % mean in Python?
For numbers, % is the modulus operator: it returns the remainder after division. 10 % 3 is 1. It is often used to test for even numbers with n % 2 == 0.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the difference between / and // in Python?
/ is true division and always returns a float (7 / 2 is 3.5). // is floor division and returns the whole-number part, rounded down (7 // 2 is 3).
beginner What does % mean in Python?
For numbers, % is the modulus operator: it returns the remainder after division. 10 % 3 is 1. It is often used to test for even numbers with n % 2 == 0.
intermediate What is 20 // 6?
3. 6 fits into 20 three whole times, so 20 // 6 is 3.
intermediate What is 20 % 6?
2. 6 * 3 = 18, and 20 - 18 leaves a remainder of 2.
Quick revision
Remember
- Use the seven arithmetic operators
- Know the difference between / and //
- Use % to find remainders
- Control order with brackets
/always gives a float, even10 / 2is5.0. Use//when you need a whole number, like a page count.
Syntax
a + b a - b a * b a / b
a // b a % b a ** b Official documentation
- Tutorial: numbers (docs.python.org)
- Operator precedence (docs.python.org)