Find a course

Module 2: Working With Numbers and TextLesson 3 of 1211 min

Numbers and Operators

Use Python's arithmetic operators, including integer division, remainder and powers, apply operator precedence and handle float rounding surprises.

Step 1 of 14Learn

Python as a calculator

A bakery packs eggs into cartons of 12. Today it has 150 eggs. How many full cartons can it fill, and how many eggs are left over? A calculator says 12.5, which answers neither question. Python has operators for exactly this.

OperatorMeaningExampleResult
+addition7 + 29
-subtraction7 - 25
*multiplication7 * 214
/division7 / 23.5
//floor (integer) division7 // 23
%remainder (modulo)7 % 21
**power7 ** 249

Two rules about types:

  • / always returns a float, even when the division is exact: 10 / 2 is 5.0.
  • Mixing an int and a float gives a float: 3 + 1.5 is 4.5. Using only ints with +, -, *, //, % or ** (with a non-negative power) gives an int.

Python's integers have no size limit: 2 ** 100 prints all 31 digits exactly. Floats, as you'll see shortly, are a different story.

For the bakery: 150 // 12 is 12 full cartons and 150 % 12 is 6 eggs left over.

Step 2 of 14Learn

Floor division and remainder in practice

// and % are a pair. For positive numbers, a // b is how many whole times b fits into a, and a % b is what's left. They always fit together like this:

a == (a // b) * b + (a % b)
17 == (17 // 5) * 5 + (17 % 5)   # 17 == 3 * 5 + 2

They solve a surprising range of everyday problems:

  • Even or odd: n % 2 is 0 for even numbers and 1 for odd ones.
  • Converting units: 200 minutes is 200 // 60 = 3 hours and 200 % 60 = 20 minutes.
  • Every nth item: i % 3 == 0 is true for every third item in a loop.
  • Last digit: 1987 % 10 is 7; 1987 // 10 drops the last digit, leaving 198.
  • Wrapping around: hours on a 24-hour clock, (22 + 5) % 24, give 3, so 5 hours after 22:00 is 03:00.

One detail matters with negative numbers: // rounds down toward negative infinity, not toward zero. So -7 // 2 is -4, not -3, and -7 % 2 is 1. That keeps the identity above true. For everyday positive values, you won't notice.

Step 3 of 14Practice · Code challenge · Python

Code challenge · Python

The function header is written for you; you only replace pass with your code. The return keyword hands a result back (you'll study functions in detail later).

Complete leftover(items, box_size) so it returns how many items are left over after filling as many full boxes of box_size as possible. For example, 150 eggs in cartons of 12 leave 6.

Real Python, running in a sandbox in your browser.
  • leftover(150, 12)
  • leftover(17, 5)
  • leftover(24, 6)
  • leftover(4, 10)

Step 4 of 14Try the calculator

Floor division and remainder explorer

Change the numbers to see how // and % split a division into a whole part and a remainder.

a / b
12.5
a // b (whole times b fits)
12
a % b (remainder)
6
Check: (a // b) * b + a % b
150

Step 5 of 14Practice · Work it out

A video is 200 minutes long. What does 200 % 60 evaluate to?

Step 6 of 14Learn

Order of operations

Python follows the familiar math precedence, from highest to lowest:

  1. Parentheses ( )
  2. Exponent **
  3. Unary minus, as in -x
  4. *, /, //, % (left to right)
  5. +, - (left to right)

So 2 + 3 * 4 is 14, and (2 + 3) * 4 is 20. 10 - 4 - 3 runs left to right: 3.

Two details surprise people. First, ** binds tighter than a leading minus, so -3 ** 2 is -9 (it means -(3 ** 2)). If you want 9, write (-3) ** 2. Excel users beware: Excel does the opposite. Second, ** groups from the right: 2 ** 3 ** 2 is 2 ** 9 = 512, not 8 ** 2 = 64.

Readable code doesn't rely on anyone remembering these rules. Add parentheses whenever a formula has more than one kind of operator. The average of three scores should be (a + b + c) / 3; without the parentheses only c is divided by 3, the same bug that catches spreadsheet users.

Python also has helpful built-in functions for numbers: abs(-5) is 5, max(3, 9, 4) is 9, min(...) finds the smallest, and round(3.14159, 2) is 3.14.

Step 7 of 14Practice · Diagram

Operator precedence (top runs first)

In 2 + 3 * 4 ** 2, which operator does Python apply first?

Tap the shape that answers the question, then press Check.

Operator precedence (top runs first)Pyramid diagram: + -, * / // %, Unary minus: -x, Exponent: **, Parentheses ( ).+ -* / // %Unary minus: -xExponent: **Parentheses ( )
+ -
* / // %
Unary minus: -x
Exponent: **
Parentheses ( )

Step 8 of 14Practice · Choose one

What is the value of 2 + 3 * 4 ** 2?

Step 9 of 14Learn

Why 0.1 + 0.2 is not exactly 0.3

Type this into Python:

>>> 0.1 + 0.2
0.30000000000000004

This isn't a bug in Python. Computers store floats in binary, and just as 1/3 can't be written exactly in decimal (0.3333…), 0.1 can't be written exactly in binary. Every language using standard floats behaves this way. The tiny errors are usually harmless, but they have consequences:

  • Don't compare floats with ==. 0.1 + 0.2 == 0.3 is False. Compare with a tolerance instead: abs(x - 0.3) < 1e-9, or use math.isclose(x, 0.3).
  • Round for display: round(0.1 + 0.2, 2) is 0.3, and an f-string like f"{total:.2f}" shows two decimals.
  • For money, many programs count in whole cents as integers, or use Python's decimal module, which stores decimal digits exactly.

Two more conversion facts. int() drops the decimal part instead of rounding: int(7.9) is 7 and int(-7.9) is -7. And round() uses "round half to even" (banker's rounding): round(2.5) is 2 and round(3.5) is 4. That avoids a systematic upward bias when rounding many values, but it catches people who expect 2.5 to become 3.

Step 10 of 14Flashcards

The arithmetic operators

Tap a card to flip it.

Step 11 of 14Note

Step 12 of 14Practice · True or false

In Python, the expression 0.1 + 0.2 == 0.3 evaluates to True.

Step 13 of 14Practice · Match the pairs

Match each expression to its result.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. 7 / 2
  2. 7 // 2
  3. 7 % 2
  4. 2 ** 3
  5. int(7.9)

Step 14 of 14Practice · Fill in the blanks

Fill in the blanks

To test whether a whole number n is even, write the condition n % 2 == . For an odd number, n % 2 is .