Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we'll learn how to loop through lists! Let's start with the for loop. Can anyone tell me what a for loop does?
Does it allow you to repeat something?
Exactly! It repeats the code for each item in the list. For example, if we have a list of fruits, we can print each fruit with a for loop: for fruit in fruits: print(fruit). Can anyone guess what happens here?
It will print every fruit in the list?
Right! Every iteration accesses the next item in the list until it has gone through them all.
How does Python know where to start?
Great question! Python starts from the first element, which is indexed at 0. So, the first fruit will be fruits[0]. Remembering the beginning index helps when working with lists!
Can we get the part for printing each item in a different way?
Yes! You could technically use a loop with indices, but that’s less common with for loops. Let’s summarize: for loops are a clean and readable way to iterate through all items in a list.
Now, let's discuss the while loop. Who can tell me how it works?
Does it also repeat things?
Correct! But it continues until a certain condition becomes false. Let's look at an example of looping through our list of fruits while checking a condition: i = 0; while i < len(fruits): print(fruits[i]); i += 1. What do you think will happen here?
I think it will print the fruits too!
Yes! But we need to be careful with our control variable i. If we forget the i += 1, we will create an infinite loop. That's key to remember!
What is an infinite loop?
An infinite loop occurs when the loop never stops executing. It's like being stuck in a loop with no way to exit! So always ensure your condition can eventually become false.
So both loops work for lists, but with for, it feels more straightforward?
Exactly! We often use for loops for list iterations due to their simplicity, but understanding while loops adds flexibility in controlling the flow.
Now that we've covered both types of loops, when do you think we should use one over the other?
Maybe use for if we know the list size?
Exactly! Use a for loop when iterating over items until the last one is reached. What about while loops?
When we don’t know how many times to iterate?
That’s right! While loops fit well in cases with uncertain iterations or conditions that may be dependent on user input or other factors. Awesome job!
Can we sum the items in a list using a loop?
Absolutely! You can use either loop type to iterate through a list to sum values. That’s a common task! Let’s summarize what we learned today about looping through lists.
So for loops are preferable when we know the length, and while is for when we need to check a condition?
Perfect summary! Understanding these loops equips you with a powerful tool in your programming toolkit!
For loops iterate in a set track, / While loops can go forward or back.
Imagine a farmer checking each fruit on his tree one by one - that’s a lot like a for loop! Meanwhile, if he keeps checking as long as it’s sunny, that’s kind of like a while loop.
F = For Each, W = While Wait.
FL = For Loop, WL = While Loop.
Term
What is a for loop?
Click to flip
Term
How does a while loop work?
Click to flip
A control flow statement that allows code to be executed repeatedly based on a given condition.
A control flow statement that executes a block of code as long as the specified condition is true.
The process of repeating a set of instructions a certain number of times or until a specific condition is met.
This section introduces students to looping through lists in Python using both for and while loops.
In this section, learners explore how to iterate through lists using for and while loops, providing practical examples to emphasize the importance of accessing each element in a list effectively.
In Python, lists can be iterated through using loops, which is a fundamental concept in programming. This section covers two primary types of loops: for loops and while loops.
The for loop is a standard way to traverse through a list. It allows you to execute a block of code for each element of the list. The syntax is clean and concise, making it easy to read. For example, to print each item in a list of fruits:
for fruit in fruits:
print(fruit)The while loop is another way to iterate through a list, but it requires a control variable to manage the loop’s execution. This type of loop continues executing as long as a specified condition is true. Here’s how it can be used:
i = 0
while i < len(fruits):
print(fruits[i])
i += 1Both looping structures are important for accessing elements within a list and executing repetitive tasks effectively. Understanding how to implement these loops enhances your programming capability significantly.