Everything in Python is an object. Python's built-in data types are categorized by whether their internal state can be altered in place (mutable) or requires creating a new object upon modification (immutable).
flowchart TD
A["Python Data Types"] --> B["Immutable Types"]
A --> C["Mutable Types"]
B --> D["Numeric: int, float, complex"]
B --> E["Sequence: str, tuple, bytes"]
B --> F["Other: bool, frozenSet"]
C --> G["Sequence: list, bytearray"]
C --> H["Mapping: dict"]
C --> I["Set: set"]
| Type Category | Type Name | Mutable? | Literal Example |
|---|---|---|---|
| Numeric | int, float, complex |
No | 42, 3.14, 1+2j |
| Sequence | str |
No | "Kodersolution" |
| Sequence | list |
Yes | [1, 2, 3] |
| Sequence | tuple |
No | (10, 20) |
| Mapping | dict |
Yes | {"key": "value"} |
| Set | set |
Yes | {1, 2, 3} |
from typing import Any, Dict, List, Tuple
def inspect_data_types() -> None:
# Explicit type assignments
user_id: int = 1001
user_score: float = 98.5
user_name: str = "Alice Developer"
active_status: bool = True
tags: List[str] = ["python", "backend", "fastapi"]
location_coords: Tuple[float, float] = (37.7749, -122.4194)
profile: Dict[str, Any] = {"role": "Engineer", "level": 3}
print(f"user_id: {type(user_id).__name__} = {user_id}")
print(f"user_score: {type(user_score).__name__} = {user_score}")
print(f"tags: {type(tags).__name__} = {tags}")
print(f"coords: {type(location_coords).__name__} = {location_coords}")
# Inspecting Mutability via id()
print("
--- Mutability Check ---")
original_str = "hello"
str_id_before = id(original_str)
original_str += " world"
print(f"String modification changes ID: {str_id_before != id(original_str)}")
original_list = [1, 2]
list_id_before = id(original_list)
original_list.append(3)
print(f"List modification retains ID: {list_id_before == id(original_list)}")
if __name__ == "__main__":
inspect_data_types()
def func(data=[]) because default mutable arguments persist across function calls; use data: list | None = None instead.isinstance() for Type Checks: Always check types using isinstance(obj, TargetType) rather than comparing type(obj) == TargetType.tuple for Heterogeneous Fixed Records: Tuples consume less memory than lists and guarantee immutability.Write a function analyze_container(container: Any) -> None that prints its data type, length (if applicable), and tests if mutating an element changes its memory id().
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
You've completed this section! Take a quick 5-question quiz to check your understanding.