LESSON 03

Numbers & Mathematics

Now we do maths. Don't worry, Python does the hard part.

USES: print( ), variables, comments.

01

Python is a calculator

You might think you already know maths. Fine. We will go through it from scratch anyway, because Python has its own symbols for it. First rule: numbers do NOT need quotation marks. Quotes are only for text. Write 7 + 3 and Python simply calculates it.

BLUEPRINT · TAP OR HOVER EACH PIECE

Blueprint 1 of 2: a calculation. Tap each piece.

02

Two kinds of numbers

Python has two main kinds of numbers. Integers (int) are whole numbers like 5 or 7. Floats are numbers with a decimal point, like 5.5 or 6.73. Quotes change everything: 5 is a number, but "5" is text that only looks like one. You will learn how to switch between them later.

INPUTprint(5) print(6.73) print("5")
OUTPUT5 6.73 5

03

Add, subtract, multiply

+ adds, - subtracts, and * multiplies. Multiply is a star, not an x, because x is a letter and could be a variable name.

INPUTprint(8 + 2) print(8 - 2) print(8 * 2)
OUTPUT10 6 16

04

Division

/ divides. In modern Python it always gives a float, even when the answer is whole. So 10 / 2 is 5.0, not 5. That is normal, not a bug. One more thing: dividing by zero is impossible, even for Python. It stops with an error. That is fine, errors are not scary.

INPUTprint(10 / 2) print(7 / 2)
OUTPUT5.0 3.5

05

Sharing cookies: // and %

There are 7 cookies and 2 people. Each person gets 3 cookies, and 1 is left over. // gives the 3 (how many whole cookies each). % gives the 1 (what is left over). The % symbol is called modulo, and it has nothing to do with percentages here.

INPUTprint(7 // 2) print(7 % 2)
OUTPUT3 1

BLUEPRINT · TAP OR HOVER EACH PIECE

Blueprint 2 of 2: the leftover. Tap each piece.

06

Powers

Two stars mean 'to the power of'. 2 ** 3 is 2 * 2 * 2, which is 8.

INPUTprint(2 ** 3) print(5 ** 2)
OUTPUT8 25

07

Order of operations

Python follows the same order as school maths: powers first, then multiply and divide, then add and subtract. Brackets let you choose your own order, and Python always does what is inside them first.

INPUTprint(2 + 3 * 4) print((2 + 3) * 4)
OUTPUT14 20

08

Quick quiz: which is bigger?

Which one gives the bigger answer? Decide first, then check.

09

Maths with variables

Variables can hold numbers, and you can calculate with them. The line age = age + 1 looks strange at first, because nothing can equal itself plus one. Remember: = does not mean equals, it means 'put into the box'. Python works out the right side first (old age plus 1), then puts the result back in the box.

INPUT# Birthday! age = 20 age = age + 1 print(age)
OUTPUT21
Report a mistake or suggest a lesson