Python Numbers & Math Module
Python has three built-in numeric types: integers, floating-point numbers, and complex numbers. For advanced mathematical operations, Python provides the built-in math module (along with the cmath module for complex numbers).
Built-in Numeric Types
Integers (int)
Whole numbers (positive, negative, or zero) of unlimited precision. Python automatically allocates as much memory as needed.
x = 42
y = -1000
z = 10000000000000000000000000000000000000000 # Unlimited length
# Alternate number bases
binary_num = 0b1010 # 10 in decimal
octal_num = 0o12 # 10 in decimal
hex_num = 0x0A # 10 in decimal
Floating-Point Numbers (float)
Real numbers written with a decimal point or in scientific (exponential) notation. Floating-point numbers follow the IEEE 754 double-precision standard (~53 bits of precision / ~15–17 decimal digits).
a = 3.14159
b = -0.001
c = 2.5e4 # Scientific notation: 2.5 * 10^4 = 25000.0
d = 1e-3 # Scientific notation: 1 * 10^-3 = 0.001
# Precision caveat
print(0.1 + 0.2) # Output: 0.30000000000000004
Note: For exact financial or currency calculations where float rounding errors are unacceptable, use Python's built-in
decimalmodule instead offloat.
Complex Numbers (complex)
Numbers with a real part and an imaginary part, represented using j (or J) for the imaginary unit ($j = \sqrt{-1}$).
z1 = 3 + 4j
z2 = complex(2, -1) # 2 - 1j
print(z1.real) # Output: 3.0
print(z1.imag) # Output: 4.0
print(z1.conjugate()) # Output: (3-4j)
Type Checking & Type Casting
Use type() to check a variable's data type, and explicit casting functions to convert between numeric types.
a = 5 # int
b = 2.8 # float
c = 1j # complex
# Checking types
print(type(a)) #
# Casting
x = float(a) # 5.0
y = int(b) # 2 (truncates decimal part, does not round)
z = complex(a) # (5+0j)
# Note: You cannot convert a complex number directly to an int or float.
Built-in Numeric Functions
Python includes several global functions for working with numbers without importing any module:
| Function | Description | Example | Output |
|---|---|---|---|
abs(x) |
Absolute value | abs(-7.5) |
7.5 |
pow(x, y, z) |
Calculates $x^y$ (or $(x^y) \pmod z$) | pow(2, 3) |
8 |
round(x, n) |
Rounds $x$ to $n$ decimal digits (uses round-half-to-even) | round(3.576, 2) |
3.58 |
min(...) / max(...) |
Finds the minimum / maximum value | max(3, 9, 2) |
9 |
divmod(x, y) |
Returns tuple (x // y, x % y) |
divmod(10, 3) |
(3, 1) |
sum(iterable) |
Sums elements of an iterable | sum([1, 2, 3]) |
6 |
The Python math Module
The standard library's math module provides C-level implementations of mathematical operations for real numbers (int and float).
import math
Essential Constants
math.pi # 3.141592653589793 (π)
math.e # 2.718281828459045 (Euler's number)
math.tau # 6.283185307179586 (2 * π)
math.inf # Floating-point positive infinity
math.nan # Floating-point "Not a Number" (NaN)
Rounding & Truncation
math.ceil(4.1) # Output: 5 (Rounds UP to nearest integer)
math.floor(4.9) # Output: 4 (Rounds DOWN to nearest integer)
math.trunc(4.9) # Output: 4 (Truncates fractional part towards zero)
Powers, Roots & Logarithms
math.sqrt(16) # Output: 4.0 (Square root)
math.isqrt(16) # Output: 4 (Integer square root)
math.pow(2, 3) # Output: 8.0 (Always returns a float)
math.exp(2) # e^2
math.log(10) # Natural logarithm (base e)
math.log(100, 10) # Logarithm base 10 (Output: 2.0)
math.log2(8) # Logarithm base 2 (Output: 3.0)
math.log10(1000) # Logarithm base 10 (Output: 3.0)
Trigonometric & Angular Functions
All trigonometric inputs and outputs use radians.
# Angle conversion
rad = math.radians(180) # Convert degrees to radians -> 3.14159...
deg = math.degrees(math.pi) # Convert radians to degrees -> 180.0
# Trigonometry
math.sin(rad) # sine
math.cos(rad) # cosine
math.tan(rad) # tangent
# Inverse trigonometry
math.asin(1) # arcsine
math.acos(1) # arccosine
math.atan2(y, x) # arctangent of y/x in radians (handles quadrant signs correctly)
Number-Theory & Combinatorics
math.factorial(5) # 5! = 120
math.gcd(24, 36) # Greatest Common Divisor -> 12
math.lcm(12, 18) # Least Common Multiple -> 36
math.comb(5, 2) # Combinations: n! / (k! * (n - k)!) -> 10
math.perm(5, 2) # Permutations: n! / (n - k)! -> 20
math.prod([2, 3, 4]) # Product of sequence -> 24
Checking & Handling Floats
math.isfinite(100.0) # True
math.isinf(math.inf) # True
math.isnan(math.nan) # True
# Safe float comparison (handles floating-point inaccuracy)
math.isclose(0.1 + 0.2, 0.3) # Output: True
5. Summary Cheat Sheet
int: Arbitrary precision whole numbers ($1, -100$).float: Double-precision floating-point numbers ($3.14, 1.0e-3$).complex: Numbers with real and imaginary parts ($3 + 4j$).- Use built-in functions (
abs,round,pow) for basic math. import mathfor trigonometric, logarithmic, rounding (ceil,floor), and statistical/combinatorial functions (factorial,gcd,comb).- Use
cmathinstead ofmathwhen working with mathematical operations on complex numbers (e.g.,cmath.sqrt(-1)).