Every computer program processes data. In Python, a data type defines the category of value an object holds, the operations that can be performed on it, and how it is stored in physical memory. Because Python is an object-oriented language, everything in Python is an object, and every object is an instance of a specific built-in data type class.
Figure 12.1: Complete architectural hierarchy of Python built-in data types categorized by mutability.
type() and isinstance()Python provides two built-in functions to verify the data type of any variable:
# Inspecting types
data = [10, 20, 30]
print(type(data)) #
print(isinstance(data, list)) # True (Recommended: supports inheritance!)
print(isinstance(data, (list, tuple))) # True (Checks if it matches either type)
In university exams and developer interviews, the concept of mutability is paramount:
| Feature | Immutable Data Types | Mutable Data Types |
|---|---|---|
| Definition | Cannot be changed after creation. Any change creates a new object. | Can be modified in place without changing its memory ID address. |
| Python Types | int, float, complex, bool, str, tuple, frozenset |
list, dict, set, bytearray |
| Dictionary Keys | Can safely be used as dictionary keys (hashable). | Cannot be used as dictionary keys (raises TypeError: unhashable). |
| Performance | Thread-safe, lower memory footprint, highly optimized. | Efficient for fast in-place additions, deletions, and sorting. |
• 1. Numeric Types (int, float, complex): Python 3 integers have arbitrary precision (unlimited digits limited only by available RAM).
• 2. String (str): Immutable sequence of Unicode characters. See our dedicated Python Strings Class.
• 3. List (list): Ordered, mutable sequence of mixed objects. See our dedicated Python Lists Class.
• 4. Tuple (tuple) & Set (set): Fixed sequences and unique unordered collections. See our Tuples & Sets Class.
• 5. Dictionary (dict): Ultra-fast key-value mapping backed by a C hash table. See our Dictionaries Class.
• 6. Boolean & NoneType (bool, None): Logical truth values and the singleton None.
# Proving string immutability vs list mutability:
name = "Veewom"
print("Original string ID:", id(name))
name = name + ".com"
print("New string ID: ", id(name)) # DIFFERENT MEMORY ADDRESS!
items = ["Keyboard", "Mouse"]
print("Original list ID: ", id(items))
items.append("Monitor")
print("Updated list ID: ", id(items)) # EXACT SAME MEMORY ADDRESS!
isinstance(object, Class) supports object-oriented inheritance. If a class inherits from list, isinstance() returns True, whereas type() == list returns False.
set is mutable (you can add and remove elements). A frozenset is immutable and frozen upon creation, making it hashable and usable as a dictionary key or an element inside another set.