Module lessons (3/4)
Numbers and the math module
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 toolsmath(sqrt,floor,ceil,gcd,isclose). - Prevent binary approximation errors in float (
0.1 + 0.2) comparisons by choosingmath.iscloseordecimal.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
- 1The mental model: integer exactness vs binary approximation
- 2Built-in numeric and
mathmodule - 3Quick check
- 4Test yourself
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.