Part 1 · Section 04
Numeric Types
Numbers look simple until you ask how a computer actually stores them. Python offers several numeric types, and each one makes a different trade-off between exactness, range, speed and memory. Knowing those trade-offs explains why 0.1 + 0.1 + 0.1 == 0.3 is False, why round(2.5) is 2, why -7 // 2 is -4, and when you should reach for Decimal or Fraction instead of float.
The section ends with booleans, which turn out to be integers in disguise, and with the rules for truthiness, short-circuiting and comparisons that every if statement relies on.
In this section you’ll learn:
- how integers are stored, why Python’s
intnever overflows, and what big integers cost - integer arithmetic: true division, floor division, modulo and the identity that links them
- building integers from strings in any base, and converting integers to other bases yourself
- exact rational arithmetic with
fractions.Fraction - how a
floatis laid out in 64 bits, and why most decimal fractions can’t be stored exactly - comparing floats safely with
math.isclose, and turning floats into integers (trunc,floor,ceil,round) round()and banker’s rounding, and how to round “the school way” when you need todecimal.Decimal: exact decimal arithmetic, precision and rounding contexts, math functions and performance costs- complex numbers and the
cmathmodule boolas a subclass ofint, truth values of every object, operator precedence, short-circuit evaluation and whatand/orreally return- identity, membership, equality and ordering comparisons, including chained comparisons
Lectures
- 01Integers - Data Type✓
- 02Integers - Operations✓
- 03Integers - Constructors and Bases✓
- 04Rational Numbers✓
- 05Floats - Internal Representation✓
- 06Floats - Equality Testing✓
- 07Floats - Coercing to Integers✓
- 08Floats - Rounding✓
- 09Decimals✓
- 10Decimals - Constructors and Contexts✓
- 11Decimals - Math Operations✓
- 12Decimals - Performance Considerations✓
- 13Complex Numbers✓
- 14Booleans✓
- 15Booleans - Truth Values✓
- 16Booleans - Precedence and Short-Circuiting✓
- 17Booleans - Boolean Operators✓
- 18Comparison Operators✓