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
Tuple immutability
๐ Scenario: You are organizing a small library system. Each book is represented by a tuple containing its title and author. Since book details should not change once added, tuples are a good choice to store this information.
๐ฏ Goal: You will create a tuple for a book, try to change its content (which is not allowed), and then create a new tuple with updated information to understand tuple immutability.
๐ What You'll Learn
Create a tuple named book with the title 'The Alchemist' and author 'Paulo Coelho'.
Create a variable named new_author and set it to 'P. Coelho'.
Create a new tuple named updated_book by replacing the author in book with new_author.
Print the original book tuple and the updated_book tuple.
๐ก Why This Matters
๐ Real World
Tuples are used to store fixed collections of data like coordinates, dates, or records that should not change accidentally.
๐ผ Career
Understanding tuple immutability helps in writing safer code and working with data structures in many programming jobs.
Progress0 / 4 steps
1
Create the initial book tuple
Create a tuple called book with the title 'The Alchemist' and author 'Paulo Coelho'.
Python
Hint
Use parentheses () to create a tuple with two strings.
2
Create a new author variable
Create a variable called new_author and set it to the string 'P. Coelho'.
Python
Hint
Just assign the string to the variable new_author.
3
Create a new tuple with updated author
Create a new tuple called updated_book by keeping the title from book and replacing the author with new_author.
Python
Hint
Use book[0] to get the title from the original tuple.
4
Print both tuples
Print the original book tuple and the updated_book tuple on separate lines.
Python
Hint
Use two print() statements, one for each tuple.
Practice
(1/5)
1. What does it mean that a tuple in Python is immutable?
easy
A. You can change items only if you use a special method.
B. You can add or remove items anytime.
C. You can change items only if they are numbers.
D. You cannot change its items after it is created.
Solution
Step 1: Understand the meaning of immutability
Immutable means something cannot be changed after it is made.
Step 2: Apply this to tuples
Tuples cannot have their items changed once created, unlike lists.
Final Answer:
You cannot change its items after it is created. -> Option D
Quick Check:
Tuple immutability = no item changes allowed [OK]
Hint: Immutable means no changes allowed after creation [OK]
Common Mistakes:
Thinking tuples can be changed like lists
Confusing immutability with read-only access
Believing only numbers in tuples are immutable
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
Step 1: Recall tuple syntax for single items
A single-item tuple needs a comma after the item inside parentheses.
Step 2: Check each option
my_tuple = (5) is just an integer in parentheses, not a tuple. my_tuple = [5] is a list. my_tuple = {5} is a set. Only my_tuple = (5,) is a tuple with one item.
Final Answer:
my_tuple = (5,) -> Option C
Quick Check:
Single-item tuple = (item,) [OK]
Hint: Single-item tuple needs a comma after the item [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?
t = (1, 2, 3)
t[1] = 5
print(t)
medium
A. TypeError
B. SyntaxError
C. (1, 5, 3)
D. (1, 2, 3)
Solution
Step 1: Understand tuple item assignment
Tuples are immutable, so you cannot assign a new value to an existing index.
Step 2: Identify the error type
Trying to assign t[1] = 5 causes a TypeError because tuples do not support item assignment.