Primitive Data Types
In Python, every value is an Object. Even a simple number like 5 has its own methods and internal state. The “Type” of an object determines what it represents, how much memory it consumes, and what operations (like addition or slicing) are valid for it.
In this lesson, we explore the scalar (primitive) types that serve as the building blocks for all complex Python programs.
1. Integers (int)
Section titled “1. Integers (int)”Integers represent whole numbers, positive or negative, without a fractional component.
Arbitrary Precision
Section titled “Arbitrary Precision”Unlike languages like C or Java, where an int is typically limited to 32 or 64 bits, Python integers have arbitrary precision. This means an integer can be as large as your computer’s RAM allows.
# A standard integerx = 100
# A massive integer (2 to the power of 512)huge_num = 2 ** 512print(huge_num)Under the Hood: Memory Layout
Section titled “Under the Hood: Memory Layout”How does Python store a number that is thousands of digits long?
A Python int is a C struct that contains:
ob_refcnt: Reference count.ob_type: Pointer to theinttype object.ob_size: The number of “digits” (Python uses base-2^30 for internal math).ob_digit: An array of digits that make up the large number.
2. Floating-Point Numbers (float)
Section titled “2. Floating-Point Numbers (float)”Any number with a decimal point is a float. Python follows the IEEE 754 double-precision standard.
gravity = 9.81scientific = 1.5e-3 # 0.0015The Precision Trap
Section titled “The Precision Trap”Computers use binary (Base-2) to represent decimals. Some Base-10 decimals (like 0.1) cannot be represented perfectly in binary.
print(0.1 + 0.2 == 0.3) # Output: False!print(0.1 + 0.2) # Output: 0.30000000000000004Context: For financial or high-precision scientific code, never use floats for exact equality. Use the decimal module instead.
3. Booleans (bool)
Section titled “3. Booleans (bool)”Booleans represent truth values: True and False.
The Integer Connection
Section titled “The Integer Connection”In Python, bool is a subclass of int.
Trueis essentially the integer1.Falseis essentially the integer0.
print(True + 5) # Output: 6While this works, you should never perform math on booleans. It is a legacy behavior from early versions of Python that is preserved for compatibility.
4. The None Type
Section titled “4. The None Type”None is a special constant used to represent the absence of a value. It is the only instance of the NoneType class.
Proper Comparison
Section titled “Proper Comparison”Always use the is operator when checking for None, not ==.
result = None
if result is None: print("No data received.")Why is? Because is checks Identity (is this the exact same object in memory?), and there is only ever one None object in a running Python program. This is faster and safer than ==.
5. Summary Table
Section titled “5. Summary Table”| Type | Class | Mutability | Example |
|---|---|---|---|
| Integer | int | Immutable | 42, -1 |
| Float | float | Immutable | 3.14, 2.0 |
| Boolean | bool | Immutable | True, False |
| None | NoneType | Immutable | None |