Numbers and operators

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)
Open in compiler
OperatorMeaning17 ? 5 gives
+add22
-subtract12
*multiply85
/divide3.4
//divide and round down3
%remainder2
**power1419857

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)
Open in compiler

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")
Open in compiler

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

  1. taka = 1270 stores the amount.
  2. taka // 100 asks how many whole hundreds fit, which is 12.
  3. taka % 100 asks what is left over, which is 70.
  4. print shows 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 / 2 to be 3. It is 3.5; use 7 // 2 for the whole-number part.
  • Writing 2 ^ 3 for a power. In Python ^ means something else; write 2 ** 3.
  • // rounds down, so -7 // 2 is -4, not -3.
  • Forgetting that a float can be slightly inexact, so 0.1 + 0.2 is not exactly 0.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

1. What is 17 // 5?
2. What is 17 % 5?
3. What is 2 + 3 * 4?

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.