π 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:
-
Create a list of pizzas:
The variable
pizza
is assigned a list containing three types of pizzas:['good pizza', 'better pizza', 'best pizza']
. -
Print each pizza name:
A
for
loop is used to iterate through thepizza
list, and theprint()
function is used to display each pizza name. -
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"
. -
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:
- ➤ 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 #introducingLists
π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