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.
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.
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.
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.
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.
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.
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.