π Python Crash Course – Chapter 4: Working With Lists – Exercise 4-13 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 4-13
Task:
A buffet-style restaurant offers only five basic foods .
Think of five
simple foods, and store them in a tuple .
• Use a for loop to print each food the restaurant offers .
• Try to modify one of the items, and make sure that Python rejects the
change .
• The restaurant changes its menu, replacing two of the items with different
foods .
Add a block of code that rewrites the tuple, and then use a for
loop to print each of the items on the revised menu
✅ Solution Code:
foods = ("meat","egg","fish","rice","chapati")
for food in foods:
print(food)
print("--------------------------------------------------------")
#the below code show error as tuples cant be modified
# foods[0] = "water" # show error ---'tuple' object does not support item assignment
print("----------------------------------------------------------")
foods = ("meat","pizza","burger","rice","chapati") # replace 2 items
for food in foods:
print(food)
π§ Code Explanation:
This Python code demonstrates how to work with tuples, including their immutability and how to redefine them. Here's how it works:
-
Create a tuple of foods:
The variable
foods
is assigned a tuple containing five food items:("meat", "egg", "fish", "rice", "chapati")
. Tuples are immutable, meaning their elements cannot be changed after creation. -
Iterate through the tuple:
A
for
loop is used to iterate through each item in thefoods
tuple and display its contents. -
Attempt to modify the tuple:
A commented-out line of code attempts to modify the first element of the tuple (
foods[0] = "water"
). This would raise an error because tuples do not support item assignment. -
Redefine the tuple:
The
foods
variable is reassigned to a new tuple with two items replaced:("meat", "pizza", "burger", "rice", "chapati")
. This demonstrates that while tuples themselves are immutable, the variable referencing the tuple can be reassigned to a new tuple. -
Iterate through the new tuple:
A
for
loop is used again to iterate through the updatedfoods
tuple and display its contents.
This code demonstrates the immutability of tuples in Python and how to redefine a tuple by assigning a new one to the same variable.
π Output:
meat
egg
fish
rice
chapati
--------------------------------------------------------
----------------------------------------------------------
meat
pizza
burger
rice
chapati
π Related Exercises from Chapter 4:
- ➤ Exercise 4-1 | Pizzas
- ➤ Exercise 4-2 | Animals
- ➤ Exercise 4-3 | Counting to Twenty
- ➤ Exercise 4-4 | One Million
- ➤ Exercise 4-5 | Summing a Million
- ➤ Exercise 4-6 | Odd Numbers
- ➤ Exercise 4-7 | Threes
- ➤ Exercise 4-8 | Cubes
- ➤ Exercise 3-9 | Cube Comprehension
- ➤ Exercise 4-10 | Slices
- ➤ Exercise 4-11 | My Pizzas, Your Pizzas
- ➤ Exercise 4-12 | More Loops
- ➤ Exercise 4-13 | Buffet
- ➤ Exercise 4-14 | PEP 8
- ➤ Exercise 4-15 | Code Review
π Connect With Us:
“The only way to learn a new programming language is by writing programs in it.”
– Dennis Ritchie
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment