Locked lesson.
About this lesson
Learn to loop using a For loop.
Exercise files
Download this lesson’s related exercise files.
For Loops.docx59.1 KB For Loops - Solution.docx
59.1 KB
Quick reference
For Loops
The For loop lets us easily loop through things.
When to use
Whenever you have a list, tuple, dictionary, or range of any sort, use the for loop to loop through and grab each item.
Instructions
To create a for loop you need something to loop through like a string, list, tuple, dictionary, etc.
names = ["John", "Tim", "Mary"]
for x in names:
print(x)
To loop through a dictionary:
pizza = {
"John": "Pepperoni",
"Tim": "Mushroom",
"Mary": "Cheese"
}
for key, value in pizza.items():
print(key)
Hints & tips
- For loops let us loop through items in a list, tuple, etc.
Lesson notes are only available for subscribers.