Skip to main content
Module 4 · Lesson 3 of 415/36 in the course~14 min
Module lessons (3/4)

Numbers and the math module

Lesson preview5 hands-on exercises readyPython Course · Module 4 — Strings and numbers

int, float, true vs floor division, modulo %, abs/round/min/max, math.sqrt, and math.pi.

What you'll learn in this lesson

  • Distinguish arbitrary precision integers (int) from 64-bit IEEE-754 floats (float).
  • Master built-ins (abs, min, max, round, sum) and standard module tools math (sqrt, floor, ceil, gcd, isclose).
  • Prevent binary approximation errors in float (0.1 + 0.2) comparisons by choosing math.isclose or decimal.Decimal.

Overview

Python offers two main numeric types for everyday use: int (arbitrary precision integers, which never overflow even with 10 ** 100) and float (IEEE-754 double precision floating point). Knowing where floats introduce tiny binary representation errors immediately protects you from financial and comparison bugs.

A int represents every lossless digit as long as there is RAM. In contrast, seemingly simple decimal numbers like 0.1 in base 2 are periodic: this is why 0.1 + 0.2 == 0.3 returns False! Compare floats always within a tolerance (math.isclose) or use integers in cents / decimal.Decimal for accounting.

Sections covered in this lesson

  1. 1The mental model: integer exactness vs binary approximation
  2. 2Built-in numeric and math module
  3. 3Quick check
  4. 4Test yourself
Pro Lesson · Module 4 — Strings and numbers

Unlock "Numbers and the math module" and the full Python Course curriculum

int, float, true vs floor division, modulo %, abs/round/min/max, math.sqrt, and math.pi.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Python Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.