Beginner · 10 min
Looping through items
Use a for loop to process each list item.
Visit every item
A for loop takes items from a list in order. The variable after for holds the current item. Indent the instructions that should repeat.
names = ["Ada", "Mina"]
for name in names:
print(name)Output
Ada Mina
Work inside and after a loop
Indented lines run for each item. A line aligned with for runs after the loop finishes.
items = ["tea", "bread"]
for item in items:
print(item.upper())
print("Done")Output
TEA BREAD Done
Looping through items
- Follow one loop iteration at a time.
- Check which lines are inside the loop.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Greet each learner
Loop through names and print a greeting for each name. The lines must be Hi, Ada and Hi, Mina.
Inside the loop, use an f-string with name after the greeting.