Learn how Python can repeat instructions for every item in a list. For loops help you write shorter, smarter programs.
Use a for loop to run the same instruction more than once.
Go through items like cat, dog, and fish one at a time.
Run real Python code in the browser and check your answer instantly.
Start with the video, then complete the loop challenge below.
A loop repeats code for each item in a collection.
Start with a list of items.
pets = ["cat", "dog", "fish"]
Create a loop variable. This variable stores one item each time the loop runs.
for pet in pets:
Indent the code you want to repeat.
for pet in pets:
print(pet)
Loop through a list and print each pet.
for pet in pets: and indent print(pet).You are given this list:
pets = ["cat", "dog", "fish"]
Use a for loop to print each pet on a new line.
Expected output:
cat
dog
fish
The checker will make sure the list is unchanged and the output is correct.
Great job using loops to work with lists.