Table of Contents
- Example 1: Immutable Integer
- Example 2: Immutable String
- Example 3: Mutable List
- Example 4: Modifying a List Element
- Example 5: Tuples are Immutable
- Example 6: Attempting to Modify a Tuple
- Example 7: Assignment with Mutable Objects
- Example 8: Creating an Independent Copy
- Example 9: Dictionary is Mutable
- Example 10: Set is Mutable
- Common Mistakes
- Best Practices
- Key Points to Remember
Every object in Python is either mutable or immutable.
- A mutable object can be modified after it is created.
- An immutable object cannot be modified after it is created.
Understanding this concept is important because it explains why some objects can be changed directly while others require a new object to be created.
Some common immutable objects are:
intfloatboolstrtuple
Some common mutable objects are:
listdictset
Example 1: Immutable Integer #
main.py
number = 100
print(id(number))
number = 200
print(id(number))
Sample Output #
140516082641360
140516082644560
Explanation #
- Initially,
numberrefers to the integer object100. - After assigning
200, Python creates another integer object. - The variable now refers to the new object.
- Integers are immutable, so the original object is never modified.
Example 2: Immutable String #
main.py
text = "Python"
print(id(text))
text = text + " Programming"
print(text)
print(id(text))
Sample Output #
Python Programming
140516082751824
Explanation #
- Strings cannot be modified after creation.
- The expression
text + " Programming"creates a new string. - The variable now refers to the new string object.
- The original string remains unchanged.
Example 3: Mutable List #
main.py
numbers = [10, 20, 30]
print(id(numbers))
numbers.append(40)
print(numbers)
print(id(numbers))
Output #
[10, 20, 30, 40]
140516082913344
Explanation #
numbersrefers to a list.- The
append()method modifies the existing list. - The identity of the list remains the same.
- Lists are mutable objects.
Example 4: Modifying a List Element #
main.py
numbers = [10, 20, 30]
numbers[1] = 50
print(numbers)
Output #
[10, 50, 30]
Explanation #
- Lists allow individual elements to be modified.
- The second element changes from
20to50. - The list object itself remains the same.
Example 5: Tuples are Immutable #
main.py
numbers = (10, 20, 30)
print(numbers)
Output #
(10, 20, 30)
Explanation #
- Tuples are immutable collections.
- Once created, their elements cannot be modified.
- To change a tuple, a new tuple must be created.
Example 6: Attempting to Modify a Tuple #
main.py
numbers = (10, 20, 30)
numbers[1] = 50
Output #
TypeError: 'tuple' object does not support item assignment
Explanation #
- Python does not allow tuple elements to be changed.
- Attempting to assign a new value raises a
TypeError.
Example 7: Assignment with Mutable Objects #
main.py
list1 = [10, 20, 30]
list2 = list1
list2.append(40)
print(list1)
print(list2)
Output #
[10, 20, 30, 40]
[10, 20, 30, 40]
Explanation #
- Both variables refer to the same list object.
- Modifying the list through
list2also affectslist1. - This happens because lists are mutable.
Example 8: Creating an Independent Copy #
main.py
list1 = [10, 20, 30]
list2 = list1.copy()
list2.append(40)
print(list1)
print(list2)
Output #
[10, 20, 30]
[10, 20, 30, 40]
Explanation #
copy()creates a new list object.- The two variables now refer to different lists.
- Modifying one list does not affect the other.
Example 9: Dictionary is Mutable #
main.py
student = {
"name": "Alice",
"age": 20
}
student["age"] = 21
print(student)
Output #
{'name': 'Alice', 'age': 21}
Explanation #
- Dictionaries are mutable.
- Existing values can be updated.
- New key-value pairs can also be added later.
Example 10: Set is Mutable #
main.py
numbers = {10, 20, 30}
numbers.add(40)
print(numbers)
Sample Output #
{10, 20, 30, 40}
Explanation #
- Sets are mutable collections.
- The
add()method inserts a new element into the existing set. - The set object itself is modified.
Common Mistakes #
1. Assuming Strings Can Be Modified #
Incorrect
text = "Python"
text[0] = "J"
Output #
TypeError: 'str' object does not support item assignment
Reason
Strings are immutable.
Correct
text = "Jython"
2. Assuming Assignment Creates a Copy #
Incorrect
list1 = [1, 2, 3]
list2 = list1
Reason
Both variables refer to the same list object.
Correct
list2 = list1.copy()
3. Trying to Modify a Tuple #
Incorrect
values = (1, 2, 3)
values[0] = 10
Output #
TypeError: 'tuple' object does not support item assignment
Reason
Tuples are immutable.
4. Assuming All Collections are Mutable #
Incorrect Assumption
tuple_data = (10, 20)
tuple_data[1] = 30
Reason
Lists, dictionaries, and sets are mutable, but tuples are immutable.
Best Practices #
- Use immutable objects when values should never change.
- Use mutable collections when data needs to be updated.
- Create a copy of mutable objects when independent data is required.
- Understand the difference between assignment and copying.
- Remember that strings and tuples cannot be modified after creation.
Key Points to Remember #
- Objects in Python are either mutable or immutable.
- Mutable objects can be modified after creation.
- Immutable objects cannot be modified after creation.
- Common immutable types include
int,float,bool,str, andtuple. - Common mutable types include
list,dict, andset. - Assignment copies a reference, not the object itself.
- Use
.copy()to create an independent copy of mutable collections such as lists. - Understanding mutability helps prevent unexpected changes in programs.