Understanding Numbers in Python
Learn Python's three numeric types — int, float, and complex — with arithmetic examples, type conversion, precision gotchas, and useful math functions.
Python has three built-in numeric types: integers (int), floating-point numbers (float), and complex numbers (complex). This page covers how each type works, how to perform arithmetic with them, common pitfalls, type conversion, and which math functions the standard library provides.
Integer Numbers
An integer is a whole number — positive, negative, or zero — with no decimal point. In Python, integers have unlimited precision: there is no fixed maximum size the way there is in C or Java. Python will happily work with numbers that have hundreds of digits.
x = 10
y = -5
z = 0
# Python integers have no fixed size limit
big = 2 ** 100
print(big) # 1267650600228229401496703205376Integer Arithmetic
Notice that / always returns a float even when the result is a whole number (6 / 2 gives 3.0). Use // when you need an integer result.
Integer Literals: Binary, Octal, and Hexadecimal
Python accepts integer literals in four bases. All four create the same int object — the prefix just tells Python how to interpret the digits.
decimal = 255 # base 10 — no prefix
binary = 0b11111111 # base 2 — prefix 0b
octal = 0o377 # base 8 — prefix 0o
hexadecimal = 0xFF # base 16 — prefix 0x
print(decimal, binary, octal, hexadecimal)
# 255 255 255 255
# Convert an int back to a string in a given base
print(hex(255)) # '0xff'
print(bin(255)) # '0b11111111'
print(oct(255)) # '0o377'Checking the Type
Use type() to confirm a value's type, or isinstance() to check membership:
print(type(42)) # <class 'int'>
print(isinstance(42, int)) # TrueFloat Numbers
A float is a number with a decimal point (or an exponent). Python floats are 64-bit IEEE 754 double-precision values, which gives roughly 15–17 significant decimal digits of precision.
x = 10.5
y = -5.2
z = 0.0
e = 1.5e3 # scientific notation — same as 1500.0Float Arithmetic
Floating-Point Precision
Because floats are stored in binary, some decimal fractions cannot be represented exactly. This is a property of IEEE 754 arithmetic, not a Python bug:
print(0.1 + 0.2) # 0.30000000000000004
print(0.1 + 0.2 == 0.3) # FalseWhen exact decimal arithmetic matters (for example, in financial calculations), use the decimal module from the standard library instead of float.
Rounding and Useful Float Operations
import math
print(round(3.14159, 2)) # 3.14 — round to 2 decimal places
print(math.floor(3.7)) # 3 — largest integer <= value
print(math.ceil(3.2)) # 4 — smallest integer >= value
print(math.sqrt(16)) # 4.0 — square root
print(abs(-7.5)) # 7.5 — absolute valueComplex Numbers
A complex number has a real part and an imaginary part. In Python (following engineering convention) the imaginary unit is written j or J, not i.
x = 10 + 5j
y = -5 + 3j
z = 0 + 0j # equivalent to complex(0, 0)
w = complex(2, -3) # constructor: real=2, imag=-3Accessing Real and Imaginary Parts
z = 3 + 4j
print(z.real) # 3.0
print(z.imag) # 4.0
print(abs(z)) # 5.0 — magnitude: sqrt(3^2 + 4^2)Complex Number Arithmetic
Complex numbers cannot be compared with < or > because there is no natural ordering on the complex plane. Only == and != are supported.
Type Conversion
Python does not silently promote types in assignments, but arithmetic between different numeric types follows well-defined rules:
| Expression | Result type |
|---|---|
int + int | int |
int + float | float |
float + complex | complex |
int + complex | complex |
You can convert between types explicitly with the built-in constructors:
# int → float
print(float(42)) # 42.0
# float → int (truncates toward zero, no rounding)
print(int(3.9)) # 3
print(int(-3.9)) # -3
# str → int or float
print(int("100")) # 100
print(float("3.14")) # 3.14
# int → complex
print(complex(5)) # (5+0j)Note that converting a float to int truncates — it does not round. Use round() first if you need rounding behaviour.
The math Module
The math module provides additional mathematical functions for real numbers.
import math
print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.log(math.e)) # 1.0 — natural log
print(math.log10(1000)) # 3.0
print(math.pow(2, 10)) # 1024.0 — float result (use ** for int result)
print(math.factorial(5)) # 120
print(math.gcd(12, 8)) # 4For operations on complex numbers, use cmath instead of math:
import cmath
z = 1 + 1j
print(cmath.phase(z)) # 0.7853981633974483 — angle in radians (π/4)
print(cmath.polar(z)) # (1.4142135623730951, 0.7853981633974483) — (r, θ)When to Use Each Type
| Use case | Recommended type |
|---|---|
| Counting, indexing, bit operations | int |
| Measurements, scientific computing | float |
| Signal processing, electrical engineering | complex |
| Financial calculations requiring exactness | decimal.Decimal |
For related topics, see the Python Variables chapter for how numbers are stored in variables, the Python Operators chapter for the full set of numeric operators, and Python Casting for type conversion details.