Skip to main content

Python Numbers

Data Types Focus

Python Numbers

Integers, floats, complex, Decimal, and Fraction give you the range to model everything from counters to currency.

Numeric types

TypeDescriptionExample
intArbitrary precision integer42
floatIEEE 754 double precision3.1415
complexReal + imaginary parts1 + 2j
decimal.DecimalBase-10 with exact precisionDecimal("0.10")
fractions.FractionRational numbersFraction(1, 3)

Integers have unlimited range—Python grows them as needed, so 2 ** 1000 just works with no overflow.

Numeric literals

million = 1_000_000     # underscores group digits for readability
hex_val = 0xFF # 255 in hexadecimal
octal = 0o17 # 15 in octal
binary = 0b1010 # 10 in binary
sci = 1.5e3 # 1500.0 in scientific notation

Arithmetic quick reference

7 / 2        # 3.5   true division always returns a float
7 // 2 # 3 floor division
7 % 2 # 1 remainder (modulo)
2 ** 10 # 1024 exponentiation
divmod(7, 2) # (3, 1) quotient and remainder together

Precision

Floats follow the IEEE 754 standard and cannot represent some decimals exactly:

0.1 + 0.2            # 0.30000000000000004
0.1 + 0.2 == 0.3 # False!

Compare floats with a tolerance instead of ==, and switch types when exactness matters:

import math
math.isclose(0.1 + 0.2, 0.3) # True

from decimal import Decimal
Decimal("0.1") + Decimal("0.2") # Decimal('0.3') — exact, use for money

from fractions import Fraction
Fraction(1, 3) + Fraction(1, 6) # Fraction(1, 2) — exact rationals

Rounding

round(3.14159, 2)    # 3.14 — round to 2 decimals
round(2.5) # 2 — banker's rounding (ties go to even)
round(3.5) # 4
math.floor(3.9) # 3
math.ceil(3.1) # 4
int(3.9) # 3 — truncates toward zero, does not round

Useful modules

Common numeric helpers
FunctionDescription
math.sqrt(x)Square root
Safe float equality within a tolerance
abs(x)Absolute value
pow(x, y, mod)Exponent with optional modulus
statistics.mean(data)Average of a sequence
math.inf / math.nanInfinity and not-a-number constants

Next up in your learning path

Frequently Asked Questions

Why does 0.1 + 0.2 not equal 0.3 in Python?

Floats use binary IEEE 754 representation, and 0.1, 0.2, and 0.3 have no exact binary form. The tiny rounding error makes the sum slightly off. Compare with math.isclose(), or use Decimal for exact base-10 arithmetic.

0.1 + 0.2                        # 0.30000000000000004
0.1 + 0.2 == 0.3 # False
import math
math.isclose(0.1 + 0.2, 0.3) # True

When should I use Decimal instead of float?

Use Decimal whenever exact base-10 precision matters—money, tax, billing—or when you must control rounding. Floats are fine and faster for scientific and general-purpose math where tiny errors are acceptable.

from decimal import Decimal
0.1 + 0.2 # 0.30000000000000004
Decimal("0.1") + Decimal("0.2") # Decimal('0.3')

What is the difference between / and // in Python?

/ is true division and always returns a float. // is floor division and returns the largest integer not greater than the result. With floats, // still floors but returns a float.

7 / 2      # 3.5
7 // 2 # 3
-7 // 2 # -4 (floors toward negative infinity)
7.0 // 2 # 3.0

Is there a maximum integer size in Python?

No. Python integers have arbitrary precision and grow to fit available memory, so huge exponents work without overflow. Only floats have a finite range and can become math.inf.

2 ** 100   # 1267650600228229401496703205376