Numbers
|
This section documents the current Python release line as published at the official Python documentation, which is the reference these pages are written and verified against. No specific patch version is pinned. This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. This section’s bibliography lists the reference material consulted while preparing these pages. |
Python has four built-in numeric types — int, float, complex, and bool — plus two standard-library
types, decimal.Decimal and fractions.Fraction, for arithmetic that must avoid binary floating-point error.
int, float, complex, and Numeric Literals
int has arbitrary precision — it grows to fit the value, with no fixed bit width and no silent overflow.
float is a 64-bit IEEE 754 double, which means most
decimal fractions have no exact binary representation. complex stores a pair of floats as real and
imag. All three are documented under
Numeric Types — int, float,
complex; the tutorial’s Numbers section is a
gentler introduction.
big = 2 ** 100 # int: no overflow, arbitrarily large
pi_ish = 3.14159 # float
z = 2 + 3j # complex: real=2.0, imag=3.0
million = 1_000_000 # underscores as visual separators
hex_val = 0x1F # 31, hexadecimal
oct_val = 0o17 # 15, octal
bin_val = 0b1010 # 10, binary
>>> 2 ** 100
1267650600228229401496703205376
>>> 0.1 + 0.2
0.30000000000000004
>>> (2 + 3j).imag
3.0
That last result is not a bug: 0.1 and 0.2 cannot be represented exactly in binary, so their sum carries a
tiny error. See Floating Point Arithmetic: Issues and
Limitations for why, and the decimal/fractions section below for exact alternatives.
Operators
Arithmetic operators are +, -, , /, //, %, and . / is *true division and always returns a
float; // is floor division and rounds toward negative infinity; % is the matching modulo; is
exponentiation.
7 / 2 # 3.5 (true division: always float)
7 // 2 # 3 (floor division)
-7 // 2 # -4 (rounds toward -infinity, not toward zero)
7 % 2 # 1
-7 % 2 # 1 (result takes the sign of the divisor)
2 ** 10 # 1024
Comparisons chain: a < b < c means a < b and b < c, evaluating b only once:
x = 5
print(0 < x < 10) # True
print(10 < x < 20) # False
Augmented assignment combines an operator with =:
total = 10
total += 5 # 15
total -= 3 # 12
total *= 2 # 24
total //= 5 # 4
total **= 2 # 16
Exact Arithmetic: decimal.Decimal and fractions.Fraction
decimal.Decimal represents numbers in base 10, avoiding the binary rounding error shown above — useful for
money and other values with an exact decimal representation. See
the decimal module.
from decimal import Decimal
price = Decimal("0.1") + Decimal("0.2")
print(price) # 0.3 (exact)
fractions.Fraction keeps an exact numerator/denominator pair, so ratios never lose precision either. See
the fractions module.
from fractions import Fraction
f = Fraction(1, 3) + Fraction(1, 6)
print(f) # 1/2
print(float(f)) # 0.5
bool is an int subclass: True and False behave as 1 and 0 in arithmetic.
print(True + True) # 2
print(isinstance(True, int)) # True
print(True * 10) # 10
Built-in Numeric Tools
abs() returns the magnitude, round() rounds to the given number of decimal places, and pow() is
exponentiation with an optional modulus. Full reference:
Numeric Types — int, float,
complex.
abs(-7) # 7
abs(-3.5) # 3.5
round(2.5) # 2 (banker's rounding: ties go to the nearest even int)
round(3.5) # 4
round(3.14159, 2) # 3.14
pow(2, 10) # 1024
pow(2, 10, 1000) # 24 (2 ** 10 % 1000, computed efficiently)
round()’s tie-breaking rule — round half to even, or "banker’s rounding" — avoids the statistical bias
of always rounding `.5 up; round(0.5) is 0, and round(1.5) is 2.
The math module supplies everything beyond these basics — square roots, trigonometry, logarithms,
constants like math.pi:
import math
math.sqrt(2) # 1.4142135623730951
math.floor(3.7) # 3
math.pi # 3.141592653589793
math (plus random and statistics) is covered in depth in Standard Library Tour.
See Also
-
Variables and Dynamic Typing — how names bind to numeric values and how Python tracks their types at runtime.
-
Standard Library Tour — the
math,random, andstatisticsmodules in depth.