0
0
Pythonprogramming~15 mins

Why loops are needed in Python - See It in Action

Choose your learning style9 modes available
Why loops are needed
📖 Scenario: Imagine you have a list of fruits and you want to say hello to each fruit. Doing this one by one is slow and boring. Loops help us do this quickly and easily.
🎯 Goal: You will create a list of fruits, set up a counter, use a loop to greet each fruit, and then print all greetings.
📋 What You'll Learn
Create a list called fruits with these exact items: 'apple', 'banana', 'cherry'
Create a variable called greetings and set it to an empty list
Use a for loop with variable fruit to go through fruits
Inside the loop, add the string "Hello, {fruit}!" to greetings
Print the greetings list
💡 Why This Matters
🌍 Real World
Loops are used in many real tasks like sending emails to many people or checking items in a list.
💼 Career
Understanding loops is important for any programming job because they help automate repetitive work.
Progress0 / 4 steps
1
Create the list of fruits
Create a list called fruits with these exact items: 'apple', 'banana', 'cherry'
Python
Need a hint?

Use square brackets [] to make a list and separate items with commas.

2
Create an empty greetings list
Create a variable called greetings and set it to an empty list []
Python
Need a hint?

An empty list is made with [].

3
Use a for loop to greet each fruit
Use a for loop with variable fruit to go through fruits. Inside the loop, add the string f"Hello, {fruit}!" to the greetings list using append()
Python
Need a hint?

Use for fruit in fruits: and inside the loop use greetings.append(f"Hello, {fruit}!").

4
Print the greetings list
Write print(greetings) to display the list of greetings
Python
Need a hint?

Use print(greetings) to show the list.