π Python Crash Course – Chapter 4: Working With Lists – Exercise 4-11 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 4-11
Task:
Start with your program from Exercise 4-1
.Make a copy of the list of pizzas, and call it friend_pizzas .
Then, do the following:
• Add a new pizza to the original list .
• Add a different pizza to the list friend_pizzas .
• Prove that you have two separate lists .
Print the message, My favorite
pizzas are:, and then use a for loop to print the first list .
Print the message,
My friend’s favorite pizzas are:, and then use a for loop to print the sec
ond list .
Make sure each new pizza is stored in the appropriate list
✅ Solution Code:
pizza=['good pizza','better pizza', 'best pizza']
friend_pizza = pizza[:]
pizza.append("original pizza")
friend_pizza.append("friend pizza")
print(pizza)
print(friend_pizza)
print("-------------------------------------------------------------")
print(f'my favourite pizzas are:')
for name in pizza:
print(name)
print("-------------------------------------------------------------")
print(f'my friends favourite pizzas are:')
for name in friend_pizza:
print(name)
π§ Code Explanation:
This Python code demonstrates how to create two separate lists by copying an existing list and modifying them independently. Here's how it works:
-
Create the original list:
The variable
pizza
is assigned a list containing three types of pizzas:['good pizza', 'better pizza', 'best pizza']
. -
Copy the list:
The slice
pizza[:]
is used to create a copy of thepizza
list. This copy is stored in the variablefriend_pizza
. The slicing ensures that the two lists are independent of each other. -
Modify the lists:
pizza.append("original pizza")
: Adds a new pizza to the originalpizza
list.friend_pizza.append("friend pizza")
: Adds a different pizza to thefriend_pizza
list.
-
Prove the lists are separate:
Both lists are printed to show that they contain different elements, proving that they are independent.
-
Iterate through the lists:
Two separate
for
loops are used to iterate through each list and display their contents with appropriate messages.
This code demonstrates how to copy a list in Python using slicing and how to modify and iterate through separate lists independently.
π Output:
['good pizza', 'better pizza', 'best pizza', 'original pizza']
['good pizza', 'better pizza', 'best pizza', 'friend pizza']
-------------------------------------------------------------
my favourite pizzas are:
good pizza
better pizza
best pizza
original pizza
-------------------------------------------------------------
my friends favourite pizzas are:
good pizza
better pizza
best pizza
friend pizza
π 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