Exercise 4-11 Solution Python Crash Course Chapter 4: Working With Lists

 

📘 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:

  1. Create the original list:

    The variable pizza is assigned a list containing three types of pizzas: ['good pizza', 'better pizza', 'best pizza'].

  2. Copy the list:

    The slice pizza[:] is used to create a copy of the pizza list. This copy is stored in the variable friend_pizza. The slicing ensures that the two lists are independent of each other.

  3. Modify the lists:
    • pizza.append("original pizza"): Adds a new pizza to the original pizza list.
    • friend_pizza.append("friend pizza"): Adds a different pizza to the friend_pizza list.
  4. Prove the lists are separate:

    Both lists are printed to show that they contain different elements, proving that they are independent.

  5. 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:


🔗 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

Previous Post Next Post

Contact Form