Numbers
Last updated
Python has two everyday kinds of number: whole numbers and decimals. In this lesson you'll do arithmetic with both, see the two kinds of division, find out why whole numbers never overflow but decimals are sometimes slightly off, and read the error you get when you add text to a number.
By the end of this lesson you can
- Tell
intandfloatapart, and know which one a sum gives - Use
/,//,%and** - Explain why
0.1 + 0.2isn't exactly0.3, and round a result - Read a
TypeErrorfrom mixing text and numbers
Whole numbers and decimals
A number without a decimal point, like 412, is an int, short for integer, a whole number. A number with a point, like 7.99, is a float, short for floating-point number, Python's name for a decimal.
Arithmetic works as you'd expect: +, - and * for multiply. If both numbers are int, the answer is an int. If either is a float, the answer is a float (line 6).
Two kinds of division
Python has two ways to divide. / always gives a float, even when the answer is whole: 12 / 4 is 3.0, not 3. // gives the whole number of times one number fits into the other and drops the rest, so 10 // 4 is 2.
Use // when only whole numbers make sense, like how many full weeks fit in a number of days.
What's left over, and powers
% gives what's left over after //. 17 days is 2 full weeks (17 // 7) with 3 days left over (17 % 7). Together they split an amount into big and small units, which you'll use for minutes and seconds, or for euros and cents.
** raises to a power: 2 ** 10 is 2 multiplied by itself 10 times.
Change days to 30 and work out the answer before you run it.
Whole numbers never overflow
In many languages a whole number has a fixed size, and going past the top is an error or wraps round to the bottom. (In Simple Rust's lesson 1.4, a u8 can't hold 256.) Python's int grows as big as it needs to, so 2 ** 100 is worked out exactly.
Try 2 ** 1000. It's a 302-digit number, and Python prints every digit.
Decimals are close, not exact
A float is stored in binary, and most decimals, like 0.1, can't be written exactly in binary, the way 1/3 can't be written exactly as a decimal. So Python stores the nearest value it can. Usually you never notice, but sometimes the tiny difference shows (line 1).
This isn't a Python bug: almost every language stores decimals this way. round(value, places) rounds to a number of decimal places, which is usually what you want to show a person (lines 2 and 3).
Line 4 holds a surprise: round(2.5) is 2, but round(3.5) is 4. When a number is exactly halfway, Python rounds to the even neighbour. This is called banker's rounding, and it stops rounding from pushing totals upwards. For money, the course will count in whole cents, so round never has to guess.
Adding text to a number
"5" in quotes is text, not a number, even though it looks like one. This program tries to add 3 to it. Press Run and read the error from the bottom up.
Python won't guess whether you meant to add numbers (8) or join text ("53"), so it stops with a TypeError: the wrong type of value for +. Fix it two ways and run each:
- Remove the quotes on line 1, so
copiesis the number5. The answer is8. - Or keep the quotes and change line 2 to
print(int(copies) + 3).int()turns text written with digits into a whole number. You'll use it a lot in the next lesson.
Check what you learned
Four questions. Pick or type an answer, then check it.
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- The Python Tutorial: Using Python as a Calculator, Numbers: /, //, % and **
- Built-in Types: Numeric Types (int, float, complex): whole numbers have no size limit
- The Python Tutorial: Floating-Point Arithmetic: Issues and Limitations: why 0.1 + 0.2 isn't 0.3
- Built-in Functions: round(): rounding halves to the even number
- Built-in Exceptions: TypeError
- The Rust Programming Language: Integer Overflow: a fixed-size whole number that goes past the top