Bird
Raised Fist0
Pythonprogramming~10 mins

Why tuples are used in Python - Test Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a tuple with three elements.

Python
my_tuple = (1, 2, [1])
Drag options to blanks, or click blank then click option'
A3
B[3]
C{3}
D"3"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using square brackets [] which create a list instead of a tuple.
Using curly braces {} which create a set instead of a tuple.
Putting the number 3 inside quotes which makes it a string.
2fill in blank
medium

Complete the code to access the second element of the tuple.

Python
second_element = my_tuple[1]
Drag options to blanks, or click blank then click option'
A[1]
B(1)
C{1}
D<1>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses () which is for tuples, not indexing.
Using curly braces {} which are for sets or dictionaries.
Using angle brackets <> which are not valid syntax.
3fill in blank
hard

Complete the code to attempt to modify the first element of the tuple, demonstrating its immutability by causing an error.

Python
my_tuple = (1, 2, 3)
my_tuple[1] = 4
Drag options to blanks, or click blank then click option'
A.append(4)
B[0]
C(0)
D[1]
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using .append(4) which causes a syntax error instead of demonstrating immutability.
Using parentheses (0) which is invalid syntax.
Using [1] which also errors but targets the wrong element.
4fill in blank
hard

Fill both blanks to create a tuple and check its type.

Python
data = [1](1, 2, 3)
type_of_data = type(data) == [2]
Drag options to blanks, or click blank then click option'
Atuple
Blist
Ddict
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using list() instead of tuple() to create the data.
Comparing type to list or dict instead of tuple.
Using incorrect syntax for type comparison.
5fill in blank
hard

Fill all three blanks to create a tuple, access an element, and check immutability.

Python
my_tuple = ([1], [2], [3])
value = my_tuple[1]
try:
    my_tuple[1] = 10
except TypeError:
    error = True
Drag options to blanks, or click blank then click option'
A10
B20
C30
D40
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using lists or other data types instead of numbers.
Trying to assign a new value to a tuple element without error handling.
Confusing tuple immutability with list mutability.

Practice

(1/5)
1. Why are tuples commonly used in Python?
tuple_example = (1, 2, 3)
easy
A. Because they are slower to access than lists
B. Because they use more memory than lists
C. Because their values cannot be changed after creation
D. Because they allow duplicate keys in dictionaries

Solution

  1. Step 1: Understand tuple immutability

    Tuples are immutable, meaning their values cannot be changed once created.
  2. Step 2: Compare with other options

    The other options are incorrect: tuples have faster or equal access speeds to lists, use less memory than lists, and do not enable duplicate keys in dictionaries (dictionary keys must be unique).
  3. Final Answer:

    Because their values cannot be changed after creation -> Option C
  4. Quick Check:

    Tuples are immutable [OK]
Hint: Remember: tuples are fixed, lists are changeable [OK]
Common Mistakes:
  • Thinking tuples can be changed like lists
  • Confusing memory use between tuples and lists
  • Believing tuples allow duplicate dictionary keys
2. Which of the following is the correct way to create a tuple with one item?
easy
A. my_tuple = (5,)
B. my_tuple = [5]
C. my_tuple = (5)
D. my_tuple = {5}

Solution

  1. Step 1: Identify tuple syntax for single item

    A tuple with one item requires a comma after the item inside parentheses, like (5,).
  2. Step 2: Check other options

    my_tuple = (5) is just an integer in parentheses, B is a list, and D is a set.
  3. Final Answer:

    my_tuple = (5,) -> Option A
  4. Quick Check:

    Single-item tuple needs comma [OK]
Hint: Single-item tuple always ends with a comma [OK]
Common Mistakes:
  • Omitting the comma for single-item tuples
  • Using square brackets instead of parentheses
  • Confusing sets with tuples
3. What will be the output of this code?
coords = (10, 20)
try:
    coords[0] = 15
except TypeError as e:
    print(e)
medium
A. 'tuple' object does not support item assignment
B. (15, 20)
C. No output, code runs fine
D. TypeError: 'int' object is not subscriptable

Solution

  1. Step 1: Understand tuple immutability

    Tuples cannot be changed after creation, so trying to assign a new value to an index causes an error.
  2. Step 2: Identify the error message

    The code catches a TypeError with message "'tuple' object does not support item assignment" and prints it.
  3. Final Answer:

    'tuple' object does not support item assignment -> Option A
  4. Quick Check:

    Assigning to tuple index causes TypeError [OK]
Hint: Tuples cannot be changed; assignment causes TypeError [OK]
Common Mistakes:
  • Expecting tuple values to change
  • Confusing error messages
  • Thinking code runs without error
4. Find the error in this code and fix it:
my_dict = {}
key = [1, 2]
my_dict[key] = "value"
medium
A. Convert the list to a string: key = str([1, 2])
B. No error, code runs fine
C. Use a list as the dictionary value instead of key
D. Use a tuple instead of a list as the key: key = (1, 2)

Solution

  1. Step 1: Identify dictionary key requirements

    Dictionary keys must be immutable and hashable. Lists are mutable and cannot be keys.
  2. Step 2: Fix by using a tuple

    Tuples are immutable and can be used as dictionary keys. Changing key to (1, 2) fixes the error.
  3. Final Answer:

    Use a tuple instead of a list as the key: key = (1, 2) -> Option D
  4. Quick Check:

    Dictionary keys must be immutable [OK]
Hint: Use tuples, not lists, as dictionary keys [OK]
Common Mistakes:
  • Trying to use a list as a dictionary key
  • Ignoring the TypeError raised
  • Using string conversion which changes key meaning
5. You want to store fixed GPS coordinates as keys in a dictionary for quick lookup. Which data type should you use and why?
gps_data = { (40.7128, -74.0060): "New York", [34.0522, -118.2437]: "Los Angeles" }
hard
A. Use lists for coordinates because they are mutable and faster
B. Use tuples for coordinates because they are immutable and hashable
C. Use dictionaries as keys for better structure
D. Use strings for coordinates to avoid errors

Solution

  1. Step 1: Understand dictionary key requirements

    Keys must be immutable and hashable. Tuples are immutable, lists are not.
  2. Step 2: Analyze given example

    The example uses a tuple for New York coordinates (correct) and a list for Los Angeles (incorrect, causes error).
  3. Step 3: Choose the correct data type

    Tuples should be used for fixed GPS coordinates to ensure keys are valid and safe.
  4. Final Answer:

    Use tuples for coordinates because they are immutable and hashable -> Option B
  5. Quick Check:

    Immutable keys like tuples [OK]
Hint: Immutable types like tuples make safe dictionary keys [OK]
Common Mistakes:
  • Using lists as dictionary keys causing errors
  • Choosing strings which lose numeric meaning
  • Using dictionaries as keys which is invalid