Introduction
Every value in Python has a type, and understanding those types is the first step toward writing correct programs. This lesson covers the numeric, boolean, and None types you will use in virtually every Python script.
Key Concepts
- Dynamic typing: Python infers a variable's type at runtime -- you never write
int x = 42. - int: Unlimited-precision integers.
- float: 64-bit IEEE 754 double-precision numbers.
- complex: Built-in complex number support with a
jsuffix. - bool:
TrueorFalse, actually a subclass ofint. - None: Python's null value, representing the absence of data.
Real World Context
Misunderstanding Python's type system causes subtle bugs in production. For example, comparing True == 1 evaluates to True because bool is a subclass of int. Knowing how truthiness works is essential for writing correct conditionals and filters in data pipelines, web handlers, and CLI tools.
Deep Dive
Integers (int)
Unlimited precision integers -- Python handles arbitrarily large numbers automatically.
pythonx = 42 big = 10 ** 100 # No overflow! hex_val = 0xFF # 255 in hexadecimal binary = 0b1010 # 10 in binary
Floating Point (float)
64-bit IEEE 754 double precision numbers.
pythonpi = 3.14159 scientific = 1.5e-10 # 0.00000000015
Complex Numbers (complex)
Built-in support for complex arithmetic.
pythonz = 3 + 4j print(z.real, z.imag) # 3.0 4.0
Boolean Type (bool)
Booleans are True or False (note the capitalization). They are actually a subclass of int.
pythonis_valid = True print(True + True) # 2 (booleans are integers!)
None Type
None represents the absence of a value. It is Python's null equivalent.
pythonresult = None if result is None: print("No result yet")
Type Checking
Use type() to check an object's type, and isinstance() for type checking that respects inheritance.
pythontype(42) # <class 'int'> isinstance(42, int) # True isinstance(True, int) # True (bool is subclass of int)
Common Pitfalls
- Floating-point precision errors --
0.1 + 0.2does not equal0.3due to IEEE 754 representation. Usemath.isclose()or thedecimalmodule when exact decimal arithmetic matters. - Confusing
==withisfor None checks -- Always useis Noneinstead of== None. Theisoperator checks identity, which is the correct way to test for None. - Assuming
boolbehaves differently fromint-- BecauseTrue == 1andFalse == 0, arithmetic on booleans can produce surprising results if you forget this relationship.
Best Practices
- Use
isinstance()overtype()for type checks --isinstancerespects inheritance and works with abstract base classes, making your code more flexible. - Use
isfor singleton comparisons -- Compare againstNone,True, andFalsewithis, not==.
Summary
- Python is dynamically typed; you never declare types explicitly.
- Integers have unlimited precision, floats follow IEEE 754, and complex numbers are built in.
boolis a subclass ofint;TrueandFalsebehave as 1 and 0 in arithmetic.Nonerepresents the absence of a value and should be checked withis None.- Use
isinstance()for type checking andmath.isclose()for float comparisons.
Code Examples
# Numeric operations
x = 10
y = 3
print(x / y) # 3.333... (true division)
print(x // y) # 3 (floor division)
print(x % y) # 1 (modulo)
print(x ** y) # 1000 (power)
# Type conversion
print(int(3.7)) # 3
print(float(42)) # 42.0
print(bool(0)) # False
print(bool(1)) # True