Python Lists vs Tuples Essentials Quiz Quiz

Quickly assess your understanding of the core differences and uses of Python lists and tuples, covering mutability, syntax, and usage in common scenarios.

  1. Basic Difference in Lists and Tuples

    Which statement correctly describes the primary difference between a Python list and a tuple?

    1. A list is mutable, while a tuple is immutable.
    2. A list can only hold integers, while a tuple can hold any type.
    3. A list is immutable, while a tuple is mutable.
    4. A list uses parentheses, while a tuple uses square brackets.

    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.

  2. Creating a Tuple in Python

    Which code snippet correctly defines a tuple containing the numbers 1, 2, and 3?

    1. my_tuple = <1, 2, 3>
    2. my_tuple = {1, 2, 3}
    3. my_tuple = [1, 2, 3]
    4. my_tuple = (1, 2, 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.

  3. List Syntax Identification

    How do you define a list in Python with elements 1, 2, and 3?

    1. my_list = <1, 2, 3>
    2. my_list = (1, 2, 3)
    3. my_list = {1, 2, 3}
    4. my_list = [1, 2, 3]

    Explanation: Lists use square brackets. Tuples use parentheses, sets use curly braces, and angle brackets are not used for these structures.

  4. Mutability Check

    If you try to change the first element of a tuple in Python, what happens?

    1. Python issues a warning but changes the value.
    2. You get an error because tuples are immutable.
    3. The tuple becomes a list automatically.
    4. The tuple element is successfully updated.

    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.

  5. Square Brackets vs Parentheses

    Which brackets are used for creating lists in Python?

    1. Square brackets [ ]
    2. Parentheses ( )
    3. Angle brackets < >
    4. Curly braces { }

    Explanation: Lists use square brackets. Tuples use parentheses, sets use curly braces, and angle brackets are not used in this context.

  6. Tuple Modification Try

    Consider: my_tuple = (1, 2, 3). What happens if you run my_tuple[0] = 100?

    1. The first element becomes 100.
    2. The tuple is automatically converted to a list.
    3. An error is raised since tuples do not support item assignment.
    4. A warning is shown and nothing happens.

    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.

  7. List Element Change

    Given my_list = [1, 2, 3], what will be the result of my_list[0] = 100?

    1. The first element is removed.
    2. An error is raised.
    3. my_list becomes (100, 2, 3)
    4. my_list becomes [100, 2, 3]

    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.

  8. Tuple Creation Syntax

    Which of the following would NOT create a tuple in Python?

    1. my_tuple = tuple([1, 2, 3])
    2. my_tuple = (1, 2, 3)
    3. my_tuple = [1, 2, 3]
    4. my_tuple = (1,)

    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.

  9. Immutability Meaning

    What does it mean when we say tuples are immutable in Python?

    1. Their elements cannot be changed after creation.
    2. They cannot be created.
    3. They must only hold numbers.
    4. They are always empty.

    Explanation: Immutability means that the content can't be changed. Tuples can be created, can hold any type, and are not necessarily empty.

  10. Changing List vs Tuple

    Which of the following objects can you change after creation?

    1. Both list and tuple
    2. Tuple
    3. List
    4. Neither

    Explanation: Lists are mutable and allow changes after creation, while tuples are immutable and do not permit changes.