Python Reference ยท Numbers and Binary Data
Python numeric types and conversion
How Python's numeric types work: int, float, complex and bool, mixed arithmetic and promotion rules, conversion functions, rounding and the pitfalls to avoid.
Python has three built-in number types: int (whole numbers of any size), float (64-bit binary floating point) and complex (a pair of floats). bool is a subclass of int, so True behaves like 1 and False like 0.
Beyond the built-ins, the standard library adds decimal.Decimal for exact decimal arithmetic and fractions.Fraction for exact ratios. This page explains how the types mix and how to convert between them.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
How the types mix
In arithmetic Python converts the "narrower" operand to the "wider" one, following the order bool โ int โ float โ complex. So 1 + 2.0 is a float, and 1 + 2j is a complex.
- Division
/always returns a float, even for6 / 3. Floor division//keeps the type:intfor two ints,floatif either operand is a float. - Integers never overflow. Very large ints just use more memory. Converting a huge int to float raises
OverflowError. - Floats are approximate.
0.1 + 0.2is not exactly0.3. Compare withmath.isclose(), or useDecimalorFractionwhen you need exactness. - Equal numbers hash equally.
1,1.0andTrueare equal and have the same hash, so they collide as dict keys or set elements.
Conversion functions
| Call | Result |
|---|---|
int(x) |
Truncates a float toward zero. Parses strings (surrounding spaces and _ between digits allowed). int(s, base) parses other bases; base 0 reads prefixes like 0x. |
float(x) |
Parses decimal text including "inf", "nan" and exponents. |
complex(real, imag) or complex("1+2j") |
The string must not contain spaces around +. |
bool(x) |
False for zero, empty containers, None; otherwise True. |
round(x, n) |
Rounds half to even, so round(2.5) is 2. |
math.floor, math.ceil, math.trunc |
Round toward negative infinity, positive infinity and zero. |
Usage notes
int("3.5")fails. Convert through a float first:int(float("3.5")).round()uses banker's rounding and works on the stored binary value, soround(2.675, 2)gives2.67.boolis anint, sosum([True, False, True])is2. That is useful for counting, but confusing inisinstance(x, int)checks.- Complex numbers cannot be ordered:
1j < 2jraisesTypeError. Equality works. - Division by zero raises
ZeroDivisionErrorfor ints and floats alike; floats can overflow toinfin some operations like1e308 * 10. - Underscores in literals (
1_000_000) are ignored and only help readability.