GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Mutable vs Immutable Objects in Python - GeeksforGeeks Mutable vs Immutable Objects in PythonLast Updated : 21 May, 2026 Every variable in Python holds an instance of an object. There are two types of objects in Python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at runtime and it can't be changed afterwards. However, its state can be changed if it is a mutable object. Immutable ObjectsImmutable Objects are of in-built datatypes like int, float, bool, string (str) and tuple. In simple words, an immutable object can’t be changed after it is created. Example 1: We will take a tuple and try to modify its value at a particular index and print it. As a tuple is an immutable object, it will throw an error when we try to modify it.
Error: Traceback (most recent call last): Example 2: We will take a Python string and try to modify its value. Similar to the tuple, strings are immutable and will throw an error.
Error: Traceback (most recent call last): Mutable ObjectsMutable Objects are of type Python list, Python dict, or Python set. Custom classes are generally mutable. Are Lists Mutable?Yes, Lists are mutable in Python. We can add or remove elements from the list. In Python, mutability refers to the capability of an object to be changed or modified after its creation. Example 1: Add and Remove items from a list in Python Here, a list is modified by adding, inserting, removing and deleting elements. Lists are mutable, which means their contents can be changed after creation using methods such as append(), insert(), extend(), remove() and pop().
Output [1, 2, 3, 4] Example 2: Modify item from a dictionary in Python Dictionaries are mutable, so we can directly update the value associated with a key after the dictionary is created.
Output {'name': 'Ram', 'age': 37}
Example 3: Modify item from a Set in Python Here is an example of Set that are mutable i.e., we can make changes in the set.
Output {1, 2, 3, 4}
Python's Mutable vs Immutable
The tuple contains a string and a list. While the tuple itself is immutable, mutable objects inside it (such as lists) can still be modified. In general, built-in scalar types are immutable, whereas container types are often mutable.
Comment Explore | |||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |