Course topics

By WebNest Studio Engineering Team

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.

Why it matters

Calculations involving money, measurements and percentages are in almost every application, and two behaviours surprise newcomers: the difference between / and //, and the small errors of floating-point arithmetic. A billing program that gets either wrong produces incorrect totals.

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.

Exercise

Convert 135 minutes into hours and remaining minutes and print the result. Then add 0.1 and 0.2 exactly, using Decimal, and print the sum.

Expected output: 2 h 15 min then 0.3

Python · starter code
from decimal import Decimal

total_minutes = 135

# TODO: work out the hours and the remaining minutes, and print them as "2 h 15 min"

# TODO: add 0.1 and 0.2 using Decimal and print the result
Show hints
  • divmod(a, b) returns the result of a // b and of a % b together.
  • Create each Decimal from a string, as in Decimal("0.1"), not from a float.
Show solution
Python · solution
from decimal import Decimal

total_minutes = 135

hours, minutes = divmod(total_minutes, 60)
print(f"{hours} h {minutes} min")

print(Decimal("0.1") + Decimal("0.2"))

Quick quiz

1. What is the result of 7 / 2?

  • A. 3
  • B. 3.5
  • C. 4
  • D. 3.0
Show answer

B. The / operator always returns a float. Use // for whole-number division.

2. What is the result of -7 // 2?

  • A. -3
  • B. -3.5
  • C. -4
  • D. 3
Show answer

C. Floor division rounds down towards negative infinity, so -3.5 becomes -4.

3. What does round(2.5) return?

  • A. 3
  • B. 2.0
  • C. 2.5
  • D. 2
Show answer

D. Python rounds a value exactly halfway to the nearest even number.

Interview questions

Why is 0.1 + 0.2 not equal to 0.3 in Python?

Show answer

Floats are stored in binary, and most decimal fractions, including 0.1 and 0.2, have no exact binary representation, so each is stored as a very close approximation. Their sum is 0.30000000000000004. To compare floats use math.isclose(), and for money use decimal.Decimal created from strings, which is exact.

What is the difference between /, // and %?

Show answer

/ is true division and always returns a float. // is floor division: it returns the quotient rounded down to a whole number. % returns the remainder, which has the same sign as the divisor. divmod(a, b) returns the last two together.

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 in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

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

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering