Course topics

By WebNest Studio

Python Tutorial

Numbers and Math in Python

Python has three built-in numeric types — int, float and complex — plus the decimal and fractions modules for exact arithmetic. Python integers have unlimited size, floats follow the IEEE 754 standard (with its famous rounding surprises), and the math module provides functions from square roots to trigonometry.

This lesson covers numeric types and operations, integer vs float division, rounding, float precision issues and how to handle money correctly.

int, float and complex

int holds whole numbers of any size — 2 ** 200 works without overflow. float holds decimal numbers with about 15–17 significant digits. complex holds numbers with a real and imaginary part. Mixing types promotes to the wider type: int + float gives a float.

Division and Rounding

/ always returns a float (7 / 2 == 3.5); // is floor division (7 // 2 == 3, -7 // 2 == -4); % gives the remainder and divmod() both at once. round() uses "banker's rounding" — halves round to the nearest even number, so round(2.5) == 2. Use math.floor, math.ceil and math.trunc for other rounding.

Float Precision and Money

Floats are stored in binary, and many decimal fractions (like 0.1) cannot be represented exactly, so 0.1 + 0.2 != 0.3. Compare floats with math.isclose(). For money, use decimal.Decimal created from strings, or store amounts as integer paise/cents. fractions.Fraction represents exact rational numbers.

Examples

Numeric types and operations

Python
print(type(10), type(3.5), type(2 + 3j))
print(2 ** 100)
print(7 / 2, 7 // 2, -7 // 2, 7 % 3, divmod(17, 5))
print(10 + 2.5)
print(abs(-8), pow(2, 5), pow(2, 5, 3))
Output
<class 'int'> <class 'float'> <class 'complex'>
1267650600228229401496703205376
3.5 3 -4 1 (3, 2)
12.5
8 32 2

Rounding and the math module

Python
import math

print(round(3.14159, 2), round(2.5), round(3.5))
print(math.floor(4.7), math.ceil(4.2), math.trunc(-4.7))
print(math.sqrt(144), math.pi, math.factorial(5))
print(math.gcd(24, 36), math.lcm(4, 6))
print(round(math.sin(math.radians(30)), 2))
Output
3.14 2 4
4 5 -4
12.0 3.141592653589793 120
12 12
0.5

Float precision and exact money with Decimal

Python
import math
from decimal import Decimal, ROUND_HALF_UP
from fractions import Fraction

print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3, math.isclose(0.1 + 0.2, 0.3))

price = Decimal("19.99")
total = price * 3
print(total)
print((Decimal("2.675")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
print(Fraction(1, 3) + Fraction(1, 6))
Output
0.30000000000000004
False True
59.97
2.68
1/2

Common Mistakes

  • Comparing floats with == instead of math.isclose().
  • Using float for currency calculations.
  • Expecting round(2.5) to be 3 — Python rounds halves to even.
  • Creating Decimal from a float (Decimal(0.1)) instead of a string, carrying the float error along.

Key Points to Remember

  • int has unlimited size; float is IEEE 754; complex uses j.
  • / gives a float, // floors, % gives the remainder.
  • round() uses banker's rounding; math has floor, ceil, trunc and more.
  • Use math.isclose for float comparison and Decimal (from strings) for money.

Practice the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.