Q.Write the names of the immutable data objects from the following:
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Immutable data objects cannot be changed after they are created; from the given options, Tuple and String are immutable.
The concept of mutability versus immutability is fundamental in programming, especially in Python. It refers to whether an object's state can be modified after it has been created.
- Mutable objects are those whose internal state can be changed after they are created. Think of it like a whiteboard: you can write on it, erase parts, and write new things, all on the same board.
- Immutable objects are those whose internal state cannot be changed after they are created. Once an immutable object is made, it's fixed. If you want to "change" it, you actually have to create an entirely new object with the desired modifications. This is like a printed book: you can't change the text on a page; if you want different text, you need a new edition of the book.
Understanding this distinction is crucial because it affects how objects behave when passed to functions, how they are stored in memory, and whether they can be used as keys in dictionaries or elements in sets (only immutable objects can be hashed and thus used in these contexts).
Let's examine each of the given data objects:
-
List
Lists in Python are mutable. This means you can add, remove, or change elements within a list after it has been created, without creating a new list object.
Consider this example:
my_list = [1, 2, 3] print(f"Original list: {my_list}") # Output: Original list: [1, 2, 3] # Modify an element my_list[0] = 10 print(f"List after changing an element: {my_list}") # Output: List after changing an element: [10, 2, 3] # Add an element my_list.append(4) print(f"List after appending: {my_list}") # Output: List after appending: [10, 2, 3, 4]As you can see, the
my_listobject itself was modified in place. -
Tuple
Tuples in Python are immutable. Once a tuple is created, you cannot change its elements, add new elements, or remove existing ones.
Consider this example:
my_tuple = (1, 2, 3) print(f"Original tuple: {my_tuple}") # Output: Original tuple: (1, 2, 3) # Attempt to modify an element try: my_tuple[0] = 10 except TypeError as e: print(f"Error when trying to modify tuple: {e}") # Output: Error when trying to modify tuple: 'tuple' object does not support item assignmentThis
TypeErrorclearly demonstrates that tuples cannot be modified after creation. If you need a "modified" tuple, you must construct a new one.Watch outWhile a tuple itself is immutable, if a tuple contains mutable objects (like lists), those mutable objects within the tuple can still be modified. For example,
my_tuple = ([1, 2], 3)allowsmy_tuple[0].append(4), makingmy_tupleeffectively([1, 2, 4], 3). The tuple's references to its elements don't change, but the contents of the mutable elements do. -
String
Strings in Python are also immutable. Just like tuples, once a string is created, its characters cannot be changed. Any operation that appears to modify a string, such as concatenation or replacing characters, actually results in the creation of a new string object.
Consider this example:
my_string = "hello" print(f"Original string: {my_string}") # Output: Original string: hello # Attempt to modify a character try: my_string[0] = 'H' except TypeError as e: print(f"Error when trying to modify string: {e}") …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.