Foundations-of-Programming-Python practice question 5 of 10

Which Python data structure cannot be modified after creation?

Full set: 60 questions
Answer options
A: List
B: Dictionary
C: Tuple Correct answer
D: Set
Correct Answer:
C. Tuple
Explanation

A tuple is an immutable data structure in Python. Once a tuple is created, its contents cannot be altered. This means you cannot add, remove, or change its elements. Any attempt to modify a tuple, such as through item assignment (e.g., mytuple[0] = 'newvalue'), will result in a TypeError. This immutability makes tuples suitable for use as dictionary keys or in sets, where hashable, constant values are required.

Why Incorrect

A. A list is a mutable data structure; its elements can be changed, added, or removed after creation.
B. A dictionary is mutable; key-value pairs can be added, updated, or deleted.
D. A set is a mutable collection; elements can be added or removed.

References

1. Python Software Foundation. (2023). The Python Tutorial, Section 5.3, "Tuples and Sequences". Retrieved from https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

2. MIT OpenCourseWare. (2016). 6.0001 Introduction to Computer Science and Programming in Python, Lecture 3 Notes, "Tuples". (This lecture explicitly defines tuples as immutable sequences).

3. Lutz, M. (2013). Learning Python, 5th Edition. O'Reilly Media, Chapter 9, "Tuples, Files, and Everything Else". (This chapter contrasts the immutability of tuples with the mutability of lists).

Scroll to Top