Basics · lesson 4 of 10
Parts in this lesson
Aint floatwhole numbers and decimals
B/ //two kinds of division
C% **what's left over, and powers
Dround()fewer decimal places
Etype()from 1.3

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 int and float apart, and know which one a sum gives
  • Use /, //, % and **
  • Explain why 0.1 + 0.2 isn't exactly 0.3, and round a result
  • Read a TypeError from 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).

Step 1 code main.py
Edit the code, then run it again

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.

Step 2 code main.py
Edit the code, then run it again

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.

Step 3 code main.py
Edit the code, then run it again

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.

Step 4 code main.py
Edit the code, then run it again

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

Step 5 code main.py
Edit the code, then run it again

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.

Step 6 code main.py
Edit the code, then run it again

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 copies is the number 5. The answer is 8.
  • 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.

What does print(9 / 3) print?
A loan is 20 days long. Which line prints how many days are left after the full weeks?
Why does 0.1 + 0.2 print 0.30000000000000004?
This program stops with a TypeError. On which line?
pages = 300
extra = "20"
total = pages + extra
print(total)

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.

All sources for Simple Python