Quickly assess your understanding of the core differences and uses of Python lists and tuples, covering mutability, syntax, and usage in common scenarios.
Which statement correctly describes the primary difference between a Python list and a tuple?
Explanation: Lists can be changed after creation (mutable), while tuples cannot be changed (immutable). The second option mismatches the bracket types. The third is reversed, and the fourth incorrectly limits list data types.
Which code snippet correctly defines a tuple containing the numbers 1, 2, and 3?
Explanation: Parentheses are used to create tuples. Option 2 is a list, option 3 is a set, and 4 is not valid Python syntax.
How do you define a list in Python with elements 1, 2, and 3?
Explanation: Lists use square brackets. Tuples use parentheses, sets use curly braces, and angle brackets are not used for these structures.
If you try to change the first element of a tuple in Python, what happens?
Explanation: Since tuples cannot be changed, attempting to modify an element results in an error. No automatic conversion or warning occurs, and elements are not updated.
Which brackets are used for creating lists in Python?
Explanation: Lists use square brackets. Tuples use parentheses, sets use curly braces, and angle brackets are not used in this context.
Consider: my_tuple = (1, 2, 3). What happens if you run my_tuple[0] = 100?
Explanation: Trying to assign a new value to an element in a tuple raises an error. Tuples are immutable so no changes occur, and there is no automatic conversion or warning-only behavior.
Given my_list = [1, 2, 3], what will be the result of my_list[0] = 100?
Explanation: Lists are mutable, so changing an element updates its value. No error occurs, the type doesn't change to tuple, and nothing is removed.
Which of the following would NOT create a tuple in Python?
Explanation: Using square brackets creates a list, not a tuple. The other options use correct tuple syntax, including the single element tuple with a comma.
What does it mean when we say tuples are immutable in Python?
Explanation: Immutability means that the content can't be changed. Tuples can be created, can hold any type, and are not necessarily empty.
Which of the following objects can you change after creation?
Explanation: Lists are mutable and allow changes after creation, while tuples are immutable and do not permit changes.