After this lesson you can recognise the two number types you will use every day, write them in code, and know why a float sometimes shows a strange trailing digit.
Two kinds of number
Python has several number types, but two do almost all the work. An int (short for integer) is a whole number: 7, 0, -12, 2024. A float is a number with a decimal point: 3.14, -0.5, 2.0. You do not declare which one you want. Python decides from how you write the value, and the built-in type() function shows its decision.
students = 28
average_mark = 71.5
print(students, type(students))
print(average_mark, type(average_mark))
print(type(5), type(5.0))28 <class 'int'>
71.5 <class 'float'>
<class 'int'> <class 'float'>Notice the last line. 5 and 5.0 mean the same amount to a person, but to Python the first is an int and the second is a float. The decimal point is what matters.
Big numbers and readable numbers
In many languages an integer has a fixed size and overflows when it gets too large. Python integers simply grow as needed, limited only by your computer's memory.
print(2 ** 100)
print(1_000_000 * 3)1267650600228229401496703205376
3000000The ** operator means "to the power of", which the next lesson covers. The second line shows a handy trick: you may put underscores inside a number to group digits. Python ignores them. Use them for anything with more than four digits, because 1_000_000 is much easier to check than 1000000.
You can also write a float using scientific notation. 1.5e3 means 1.5 times ten to the power of 3, which is 1500.0. You will meet this mostly in output from scientific code.
Why floats are slightly off
Here is the most famous surprise in programming.
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)0.30000000000000004
FalseThis is not a Python bug. Computers store floats in binary, and just as one third cannot be written exactly in decimal (0.3333...), the value 0.1 cannot be written exactly in binary. Python stores the closest value it can, and when you add two such approximations the tiny errors show up. Every mainstream language behaves the same way.
The practical rules are simple:
- Never compare two calculated floats with
==. Check whether they are close enough instead. - Do not worry about the error for everyday work. It is far below anything a person would notice.
- For money, where every cent must be right, do not use floats. See below.
If you do need to compare floats, the standard library offers math.isclose:
import math
total = 0.1 + 0.2
print(math.isclose(total, 0.3))TrueRounding for display
The round() function takes a number and, optionally, how many digits to keep after the decimal point.
price = 19.987
print(round(price, 2))
print(round(price))
print(round(2.5), round(3.5))19.99
20
2 4With one argument, round() returns an int. Look at the last line: 2.5 rounds to 2 and 3.5 rounds to 4. Python rounds a value that is exactly halfway to the nearest even number, a rule that avoids always pushing totals upwards. It surprises many learners, so it is worth knowing it is deliberate.
Exact decimals for money
When you add up shop prices, you want 0.10 plus 0.20 to be exactly 0.30. The standard library has a Decimal type for this. You build it from a string, not from a float, so no error sneaks in first.
from decimal import Decimal
subtotal = Decimal("0.10") + Decimal("0.20")
print(subtotal)
print(subtotal == Decimal("0.30"))0.30
TrueDecimals are slower than floats and a little more awkward to write, so use them where exactness matters, such as bills, and use plain floats everywhere else.
Choosing between int and float
A useful rule is to ask what you are measuring. If you are counting things, such as students, books on a shelf or items in a basket, use an int, because you cannot have two and a half students. If you are measuring something that can fall between whole units, such as a height, a temperature or an average mark, use a float. Money is the awkward middle case. Many programs store it as a whole number of the smallest unit, for example cents, so every value stays an int and no rounding error can creep in. Whichever approach you take, decide early and use it consistently, because mixing styles inside one program is where most money bugs begin.
Common mistakes
- Trusting float equality. Writing
if total == 0.3after adding floats can fail. Usemath.iscloseor round first. - Building a
Decimalfrom a float.Decimal(0.1)carries the float's error with it. WriteDecimal("0.1")with quotes. - Putting a comma in a big number.
1,000is not one thousand in Python. It creates a pair of values. Use1000or1_000. - Expecting
round(2.5)to give 3. It gives 2, because halfway values go to the nearest even number. - Leading zeros. Writing
007is an error in Python 3. Write7.
A video version of this topic is in the video course at /learn/python/.
Exercise
A shop sells a notebook for 4.35 and a pen for 1.20. A student buys 3 notebooks and 4 pens. Using only the numbers above, print the total as an exact decimal, then print the same total as a float rounded to 2 places.
Show a possible answer
from decimal import Decimal
exact = Decimal("4.35") * 3 + Decimal("1.20") * 4
print(exact)
approx = 4.35 * 3 + 1.20 * 4
print(round(approx, 2))17.85
17.85Both lines show the same total here, but only the Decimal version is guaranteed to be exact for every combination of prices.
Key points
- An int is a whole number with no decimal point, and a float is a number with a decimal point. type() tells you which one you have.
- Python integers have no fixed size, so very large whole numbers work without any special handling.
- Floats are stored in binary, so some decimals such as 0.1 cannot be stored exactly. Expect tiny errors in the last digits.
- Use round() for display and the decimal module when you need exact money arithmetic.
- Underscores such as 1_000_000 make big numbers easier to read and do not change the value.