Python Numbers
Learn Python's built-in number types, everyday arithmetic, and complex numbers, from division and remainders to exponentiation and rounding.
Basic
Python has two number types you will use all day:
int, a whole number of any size, and
float, a decimal number. You rarely have to think about which is which, because Python picks from the value you write: no decimal point means
int, one means
float. What deserves your attention is the arithmetic, because two of its rules surprise everyone exactly once.
The operators, including the two surprises
Expression | Result | Notes |
|---|---|---|
7 + 3 | 10 | Addition works on both types. |
7 - 3 | 4 | So does subtraction. |
7 * 3 | 21 | Multiplication, no symbol tricks. |
7 / 2 | 3.5 | Surprise one: / is always true division, always a float. |
7 // 2 | 3 | Floor division discards the remainder. |
7 % 2 | 1 | The remainder itself (modulo). |
2 ** 10 | 1024 | Exponentiation, not ^. |
The first surprise is division:
/ between two integers still produces a float, so even
6 / 3 is
2.0. When you want whole-number division, reach for
//, and pair it with
% when the remainder matters, testing evenness, cycling through positions, splitting a duration into minutes and seconds. The second surprise is exponentiation: Python uses
**, because
^ means something else entirely and errors out loudly if you try it on numbers.
Integers never overflow
Python integers have no fixed size. They grow as large as the machine's memory allows, so
2 ** 200 just works, all 61 digits of it. This is not a party trick but a small design promise: counting things in Python never wraps around the way fixed-size integer languages can. Floats are the ordinary limited ones, with about 15 to 17 significant digits and the usual binary-decimal fuzziness, which is why
0.1 + 0.2 prints as
0.30000000000000004. Every language with floats has that artifact; none of them hide it.
Complex numbers use j for the imaginary part
Python also has the
complex type for numbers with a real part and an imaginary part. Python writes the imaginary unit with a trailing
j rather than
i. So
3 + 4j combines a real part of 3 with an imaginary part of 4. The real and imaginary components of a complex value are exposed as floats.
When the two parts are separate values, use
complex(real, imag). Complex numbers support arithmetic with other real or complex numbers, but they do not have a natural less-than or greater-than ordering; do not try to sort them with
< or
>. The
abs() function returns a complex number’s magnitude and is covered with the other numeric helper built-ins.
Run a complex-number example
Output
Ready to run.
The example prints
3.0,
4.0, then
(5+3j). Notice that the constructor receives real part first and imaginary part second; the sign of the imaginary component is part of its value.
Quick check: choose the components
Quick check
Which expression creates a complex number whose real part is 2 and imaginary part is -5?
z =
print(z.real, z.imag)
Converting and rounding
int() and
float() convert between the types and parse strings, when the string is a clean number:
int("42") works,
int("42.5") raises an error, so a two-step
int(float("42.5")) is the honest route for messy input. Rounding has its own built-in,
round(), which rounds to the nearest value and, unusually, ties to the nearest even number:
round(2.5) is
2, and
round(3.5) is
4. The behavior is deliberate, an accumulator's friend, and worth knowing before it startles you in a report.
Run the arithmetic
The example below is a small price calculation using the whole operator set. Run it, then change the
loaves count or the division lines. Try replacing
// with
/ to see the type change ripple through the output, and try
2 ** 64 where the piasters total is computed, to watch Python calmly print a number that would overflow a 64-bit integer in many other languages.
Output
Ready to run.
What to notice
Every
/result printed with a decimal point, even when the division was exact.Floor division and modulo together describe any split: the equal shares and the leftover.
The f-strings let values speak for themselves;
f"{total}"formats a float without ceremony.No limits were consulted before using
2 ** 64.
Quick check
If you can predict
7 / 2,
7 // 2, and
7 % 2 without running them, and you know why money should be counted in integers, you have Python's numbers. Next, strings holds the other half of everyday data. When you are ready, take the Python Numbers quiz.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.