# Nakafa Framework: LLM URL: /en/subject/university/bachelor/ai-ds/ai-programming/immutable-mutable-identity Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/immutable-mutable-identity/en.mdx Output docs content for large language models. --- export const metadata = { title: "Immutable, Mutable, and Identity", description: "Master Python object mutability, identity, and memory management. Learn how lists, references, and object identity work with practical examples and code.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/18/2025", subject: "AI Programming", }; ## Lists and Mutability In Python, lists are data structures that can be modified after they are created. Let's understand how lists behave when we perform certain operations. When you create a list and store it in a variable, Python doesn't copy the contents of that list. Instead, the variable stores a reference or memory address where the list is located. Think of it like a house address, the variable only stores the address, not the house itself. Notice how changes to `list1` also affect `list2`. This happens because `list2` doesn't store a copy of `list1`, but rather a reference to the same list object in memory. ### Slice Operations Create New Copies Unlike regular assignment, slice operations on lists will create a new list object in memory. You can prove this by using the `id()` function which returns the memory address of an object. ## Object Identity, Type, and Value Every object in Python has three important characteristics that distinguish it from other objects. B[Identity] A --> C[Type] A --> D[Value] B --> B1[Unique memory address] C --> C1[Determines operations] D --> D1[Stored data] `} /> ### Object Identity Identity is the unique memory address where an object is stored. Python provides the `id()` function to view an object's identity and the `is` operator to compare the identity of two objects. print(id(a)) # 10120 b = a # b references the same object as a print(id(b)) # 10120 (same as a) a = a + 1 # Creates new object print(id(a)) # 10492 (different from before)` } ]} /> ### Identity and Value Comparison Python has special optimization for small integers (usually to ). Integers in this range use the same object in memory to save space. ## Mutable and Immutable Objects Python classifies objects based on their ability to be changed after creation. ### Immutable Objects Immutable objects cannot be changed after creation. Any operation that appears to change the object actually creates a new object. Immutable data types in Python include: - `bool` (boolean) - `int` (integer) - `float` (floating point) - `complex` (complex numbers) - `str` (string) - `tuple` (tuple) ### Mutable Objects Mutable objects can be changed after creation without creating a new object. The object's identity remains the same even though its value changes. Mutable data types in Python include: - `list` (list) - `dict` (dictionary) - `set` (set) ### Assignment vs Modification Behavior It's important to distinguish between assignment (giving new value) and in-place modification. C[List Object
Data: 0, 1, 2] B[list2] --> C end subgraph After ["After list1[1] = x"] D[list1] --> F[Same List Object
Data: 0, x, 2] E[list2] --> F end Before -.-> After `} /> Understanding mutability is very important in Python programming. Immutable objects are safer to use in multi-threading contexts because they cannot be changed, while mutable objects provide flexibility to modify data efficiently without creating new objects every time.