Academic Resource Center

For Loops in Python

Updated on

Python has two basics loops, While Loop and For Loop. We use a for loop when we need to apply an expression to each item in the sequence. When we loop through all the items in the sequence, the for loop ends.

Python For Loop.pdf - Google Drive - Google Chrome

Syntax

Python For Loop.pdf - Google Drive - Google Chrome

Example

Consider an example where we have a basket of fruits: apple, banana, orange, peach, and mango. We want to iterate over all the items in the basket and print the names of the fruits one at a time. This can be accomplished using print() statement in the body of our for loop.

Python For Loop.pdf - Google Drive - Google Chrome
Python For Loop.pdf - Google Drive - Google Chrome

As you can see from the output, every time the body of the loop is executed, it prints the fruit name and comes back to the basket for the next item. When there are no more fruits in the basket, the for loop ends.

Often, we want to access item in the sequence by its index. For loops present us with a great opportunity to count items, access each item separately, or determine if an item exists in the sequence. Consider the following example, where we still have our basket of fruits, but now we want to count the fruits and print their indexes.

Please remember that there are many ways to perform the same action in Python. The built-in Python method enumerate() is used here for demonstration.

Python For Loop.pdf - Google Drive - Google Chrome
Python For Loop.pdf - Google Drive - Google Chrome

As you can see from the output, the for loop provided us with wide range of information about the basket and its items. We know the fruit’s name, index, and count, and now are ready to implement additional statements to control the for loop.

Additional For Loop Control Statements

A good habit is to use control statements, break or continue, exit, or skip some parts of the for loop.

Python Break Statement

Break statement is placed inside the for loop to break out the loop prematurely. After the break statement, no codes are executed inside the loop.

Python For Loop.pdf - Google Drive - Google Chrome

Syntax

Python For Loop.pdf - Google Drive - Google Chrome

Example

In our example, we use the break statement to exit the for loop as soon as we find peach.

Python For Loop.pdf - Google Drive - Google Chrome
Python For Loop.pdf - Google Drive - Google Chrome

Python Continue Statement

Continue statement is placed inside the for loop to shortcut the loop skipping over an item and proceeding to the next one in the sequence.

Python For Loop.pdf - Google Drive - Google Chrome

Syntax

Python For Loop.pdf - Google Drive - Google Chrome

Example

In our example, we can use the continue statement to skip over one item, banana, and proceed with the iteration over the rest of the fruit in the basket.

Python For Loop.pdf - Google Drive - Google Chrome
Python For Loop.pdf - Google Drive - Google Chrome

Need More Help?

Click here to schedule a 1:1 with a tutor, coach, and or sign up for a workshop. *If this link does not bring you directly to our platform, please use our direct link to "Academic Support" from any Brightspace course at the top of the navigation bar.

Previous Article Escape Sequences in Python
Next Article While Loop in Python
Still Need Help? Schedule a service in the Academic Support Center