5.3 Numeric Data Types
Python's four numeric types — int, float, complex, and bool — with type conversion and the core arithmetic operators.
int
Whole numbers of arbitrary precision (Python ints don’t overflow like fixed-width integers in C — they grow as large as memory allows).
>>> type(10)
<class 'int'>
>>> 2 ** 100 # no overflow, however large
1267650600228229401496703205376
float
Decimal (floating-point) numbers, stored using the IEEE 754 double-precision format — the same trade-offs (like imprecise decimal representation) apply as in most other languages.
>>> type(10.5)
<class 'float'>
>>> 0.1 + 0.2 # classic floating-point precision surprise
0.30000000000000004
complex
Numbers with a real and imaginary part, written with a j suffix — used in scientific/engineering computation, rarely in typical DevOps scripting.
>>> type(2 + 3j)
<class 'complex'>
bool
What Is It?
True/False, but technically a SUBCLASS of int (True == 1, False == 0).
Why Does It Matter?
bool values can be used directly in arithmetic, and isinstance(True, int) is True — a common interview gotcha.
>>> type(True)
<class 'bool'>
>>> int(True), int(False)
(1, 0)
>>> isinstance(True, int) # bool IS an int subclass
True
Type Conversion
How Is It Used?
Explicit conversion between numeric types uses the type’s own name as a function:
>>> int("42")
42
>>> float("3.14")
3.14
>>> str(42)
'42'
Arithmetic Examples
>>> 10 / 3 # true division -- always returns a float
3.3333333333333335
>>> 10 // 3 # floor division -- returns an int-like whole result
3
>>> 10 % 3 # modulo -- the remainder
1
>>> 10 ** 2 # exponentiation
100
Quick Interview Answer
“Python has four numeric types:
int(arbitrary precision, no overflow),float(IEEE 754 double-precision, subject to the classic0.1 + 0.2 != 0.3imprecision),complex(real + imaginary,jsuffix), andbool, which is technically a subclass ofint—True == 1,False == 0, andisinstance(True, int)isTrue. Explicit conversion between them just calls the target type as a function:int("42"),float("3.14").”
Common Mistakes
- Comparing floats with
==directly (0.1 + 0.2 == 0.3isFalse) instead of accounting for floating-point imprecision. - Forgetting
boolis anintsubclass, then being surprised thatTrue + True == 2or that a type-check withisinstance(x, int)also matches booleans. - Using
/when floor division//was actually intended, or vice versa.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form