Numbers and maths
You have added a couple of numbers already. Python can do a great deal more than that, and two of its operators do things you will not have seen in a maths class — which is exactly where beginners get stuck.
Two kinds of number
Python has whole numbers and decimals, and it treats them as different types:
print(type(7))
print(type(7.0))
<class 'int'> <class 'float'>
An int is a whole number. A float is a number with a decimal point.
7 and 7.0 are the same value to you, but not the same type to Python. The
name "float" is short for floating point, which will matter at the end of this
lesson.
The four you already know
print(9 + 4)
print(9 - 4)
print(9 * 4)
print(9 / 4)
13 5 36 2.25
Notice the last one. Division always produces a float, even when the numbers divide evenly:
print(8 / 2)
4.0
Not 4. If you need a whole number back, that is the next operator.
Integer division and remainder
// divides and throws away anything after the decimal point. % gives you
what was left over:
print(17 // 5)
print(17 % 5)
3 2
Five goes into seventeen three times, with two left over. Together these two turn one number into a sensible pair of numbers, which is how you convert a total into units:
total_seconds = 200
minutes = total_seconds // 60
seconds = total_seconds % 60
print(minutes, "minutes and", seconds, "seconds")
3 minutes and 20 seconds
% has a second everyday use: testing whether a number divides evenly. If the
remainder is zero, it does.
print(10 % 2)
print(7 % 2)
0 1
Every even number gives 0, every odd number gives 1. That is the standard
way to check for even and odd, and you will use it constantly once you are
writing loops.
Powers, and rounding
** raises a number to a power:
print(2 ** 10)
print(9 ** 0.5)
1024 3.0
Raising to the power of 0.5 is a square root, which is occasionally handy.
round() shortens a decimal to as many places as you ask for:
price = 349.567
print(round(price))
print(round(price, 2))
350 349.57
With no second argument you get a whole number back. With one, you get that many decimal places.
Order matters, and brackets win
Python follows the same precedence rules you learned in maths: powers first, then multiply and divide, then add and subtract.
print(2 + 3 * 4)
print((2 + 3) * 4)
14 20
The first is 3 * 4 then + 2. The second does the brackets first. When a
calculation gets long enough that you have to stop and think, add brackets even
where they are not strictly needed — you are writing for the person who reads it
next, and that is usually you.
The one that looks like a bug
Try this, and read the output carefully:
print(0.1 + 0.2)
0.30000000000000004
That is not a mistake in Python, and it is not a mistake you made. Every mainstream programming language does this — JavaScript, Java, C, all of them.
Here is why. A computer stores numbers in binary, as sums of halves, quarters,
eighths and so on. Some decimals fit that scheme exactly: 0.5 is one half,
0.25 is a quarter. But 0.1 cannot be written as a finite sum of halves, in
the same way 1/3 cannot be written exactly as a decimal — you get 0.3333…
forever and have to stop somewhere. The computer stops somewhere too, and the
tiny leftover shows up when you add.
So this fails, which surprises nearly everyone:
print(0.1 + 0.2 == 0.3)
False
Two practical rules. Round for display, when a human is going to read it:
print(round(0.1 + 0.2, 2))
0.3
And never compare floats with ==. Check that the difference is small
enough instead:
answer = 0.1 + 0.2
print(abs(answer - 0.3) < 0.0001)
True
abs() gives a number's distance from zero, so it ignores whether the
difference came out positive or negative.
Whole numbers do not have this problem at all — int arithmetic in Python is
exact, no matter how large:
print(2 ** 100)
1267650600228229401496703205376
That is a 31-digit number, calculated exactly. Most languages would have given up long before this. It is decimals, not size, that Python handles approximately.
Next you will meet the other types Python uses and how to move between them,
which is the types lesson. If you want to see
where % really earns its place, it turns up again in
the loops lesson.
Your turn
Print how many whole times 7 goes into 45 on the first line, then what is left over on the second line. Use the operators, not the answers.
The variable number is set. Print Even if it divides by 2 with nothing left over, otherwise print Odd. With number = 13 it should print Odd.
total_seconds holds 500. Print it as minutes and seconds in exactly this form: 8 minutes and 20 seconds
0.1 + 0.2 is not exactly 0.3, so == will say False. Print True by checking that the difference from 0.3 is smaller than 0.0001 instead.
Prefer learning with a teacher?
WasiLearn Academy runs small-group classes covering this material, online and in Karachi.
See the classes