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

 

📘 Python Crash Course – Chapter 4: Working With Lists – Exercise 4-1 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


📝 Exercise 4-1

Task:
Think of at least three kinds of your favorite pizza . Store these pizza names in a list, and then use a for loop to print the name of each pizza .
• Modify your for loop to print a sentence using the name of the pizza instead of printing just the name of the pizza . For each pizza you should have one line of output containing a simple statement like I like pepperoni pizza .
• Add a line at the end of your program, outside the for loop, that states how much you like pizza . The output should consist of three or more lines about the kinds of pizza you like and then an additional sentence, such as
I really love pizza!

✅ Solution Code:


pizza=['good pizza','better pizza', 'best pizza']

for pizza_name in pizza:
    print(pizza_name)

print("--------------------------------------------------------------------")


for pizza_name in pizza:
    print(f'I like {pizza_name}')

print("--------------------------------------------------------------------")


for pizza_name in pizza:
    print(f'I like {pizza_name}')
print(f'I like Pizza because Iron Man also Likes Pizza\n\tI really love pizza!')

print("--------------------------------------------------------------------")

🧠 Code Explanation:

This Python code demonstrates how to use a list and a for loop to iterate through items and print personalized messages. Here's how it works:

  1. Create a list of pizzas:

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

  2. Print each pizza name:

    A for loop is used to iterate through the pizza list, and the print() function is used to display each pizza name.

  3. Print personalized statements:

    The for loop is modified to include an f-string that prints a personalized statement for each pizza, such as "I like good pizza".

  4. Add a concluding statement:

    After the loop, a final statement is printed outside the loop, expressing a general love for pizza. This includes a multiline string with a tab character (\t) for formatting.

🔍 Output:


good pizza
better pizza
best pizza
--------------------------------------------------------------------
I like good pizza
I like better pizza
I like best pizza
--------------------------------------------------------------------
I like good pizza
I like better pizza
I like best pizza
I like Pizza because Iron Man also Likes Pizza
        I really love 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 #introducingLists

Previous Post Next Post

Contact Form