Python Numbers
What you will learn
- The two main number types:
intandfloat - Ways to write numbers: underscores and scientific notation
- That an
inthas no fixed size limit - The limits of a
float, and the special valuesinfandnan - Which type an operation gives back
1. int and float
Python has two main kinds of numbers:
| Type | What it is | Examples |
|---|---|---|
int |
a whole number, positive, negative or zero | 10, -3, 0 |
float |
a number with a decimal point (a "floating-point" number) | 3.5, -0.25, 2.0 |
A number written with a decimal point is a float, even if the decimal part is zero: 2.0 is a float, 2 is an int. They can be equal in value but they are different types.
print(type(3), type(3.0), 3 == 3.0)
<class 'int'> <class 'float'> True
Quick check: what type is `5.0`?
float. The decimal point makes it a float.
2. Writing numbers
Underscores for readability
You can put underscores inside a long number. Python ignores them:
print(1_000_000)
1000000
Scientific notation
For very big or very small numbers, write e followed by a power of ten. 1e3 means 1 × 10³:
print(1e3, 2.5e-3, 1.5e10, type(1e3))
1000.0 0.0025 15000000000.0 <class 'float'>
A number written with e is always a float. Python also prints very large or very small floats this way:
print(1e-7, 1e16)
1e-07 1e+16
(Here 1e-07 means 0.0000001.)
3. An int can be as large as you need
Many languages have a largest whole number. Python's int does not: it grows as needed, limited only by your computer's memory.
print(2 ** 100)
print(123456789 * 987654321)
print(len(str(2 ** 1000)))
1267650600228229401496703205376
121932631112635269
302
So 2 ** 1000 is a perfectly good number with 302 digits. You never have to worry about whole numbers "overflowing".
4. A float has limits
A float uses a fixed amount of space (64 bits, the standard "double precision" format), so it has a largest size and a limited number of precise digits (about 15 to 17):
import sys
print(sys.float_info.max)
print(sys.float_info.epsilon)
1.7976931348623157e+308
2.220446049250313e-16
sys.float_info.maxis the largest float.epsilonis the gap between 1.0 and the next float: it shows the limit of precision.
Because of that limit, big floats lose their last digits:
print(2.0 ** 53 + 1 == 2.0 ** 53)
print(123456789012345678.0)
True
1.2345678901234568e+17
Adding 1 to 2.0 ** 53 changed nothing, because a float has no room for it. With int, the result would be exact. The next lessons come back to this (lesson 3.4).
Beyond the largest float: inf and nan
A float calculation that is too large can give infinity:
print(1e308 * 10, -1e308 * 10)
print(float("inf"), float("nan"))
inf -inf
inf nan
inf(and-inf) mean "larger than any number".nanmeans "not a number": the result of a calculation with no sensible answer, such asinf - inf.
nan is odd: it is not even equal to itself. Use math.isnan to test for it (lesson 3.6):
import math
x = float("nan")
print(x == x, math.isnan(x))
False True
Some operations raise an error instead of giving inf: 10.0 ** 400 raises OverflowError: (34, 'Result too large').
5. Which type does an operation give?
| Operation | Result type | Example |
|---|---|---|
int + int, int - int, int * int |
int |
2 + 3 is 5 |
any operation with a float |
float |
2 + 3.0 is 5.0 |
/ (division) |
always float |
6 / 3 is 2.0 |
// (floor division) |
int if both are int, else float |
6 // 3 is 2 |
** with a negative int power |
float |
2 ** -1 is 0.5 |
print(type(2 + 3), type(2 + 3.0), type(6 / 3), type(6 // 3), type(2 ** -1))
<class 'int'> <class 'float'> <class 'float'> <class 'int'> <class 'float'>
The rule of thumb: a float anywhere makes the answer a float, and / always gives a float. Lesson 3.2 covers the operators.
6. Checking for a whole-number float
float has a method is_integer() that checks whether the number has no decimal part:
print(7.5.is_integer(), (7.0).is_integer())
False True
Worked example: how big is "a lot"?
How many seconds are there in 100 years of 365 days?
seconds = 100 * 365 * 24 * 60 * 60
print(seconds)
print(type(seconds))
3153600000
<class 'int'>
| Step | Meaning |
|---|---|
100 * 365 |
days |
* 24 |
hours |
* 60 * 60 |
minutes, then seconds |
All the numbers are int, so the answer is an exact int, however large. If you had written 365.0, the answer would be a float: 3153600000.0.
Common mistakes
- Thinking
2.0and2are the same type. They are equal in value, but one is afloatand one anint. - Worrying that an
intcan overflow. It cannot; floats are the ones with limits. - Expecting a float to hold every digit. After about 15 to 17 digits it rounds.
- Testing
x == float("nan"). It is alwaysFalse; usemath.isnan(x). - Putting a comma in a number.
1,000is not a number in Python (it makes a pair of values); use1_000or1000.
Key takeaways
intis a whole number of any size;floathas a decimal point and about 15 to 17 precise digits.- Write
1_000_000for readability and1e3(a float) for powers of ten. - A
floatanywhere in a calculation makes the result afloat;/always gives afloat. - Floats have limits and special values:
inf,-infandnan. nanis not equal to anything, including itself.
Practice
1. What are the types of 7, 7.0 and "7"?
Show answer
int, float and str.
2. What does print(3 * 2.0) show, and what is its type?
Show answer
6.0, a float. A float in the calculation makes the result a float.
3. Rewrite 5000000000 so that it is easier to read, in two ways.
Show answer
5_000_000_000 (underscores, still an int) or 5e9 (scientific notation, which gives the float 5000000000.0).
4. Can 2 ** 200 cause an overflow error? Why or why not?
Show answer
No. An int has no fixed size limit, so Python gives the exact number.
5. What does float("nan") == float("nan") give? How do you test for nan properly?
Show answer
False, because nan is not equal to anything. Use math.isnan(x).
6. What is the type of 10 / 5? Of 10 // 5?
Show answer
float (2.0) and int (2).