C:\Discounts> Use code System.OutOfMemoryException and get FREE SHIPPING_

Python - For Loop

Python - For Loop

for, for loop, loop, nested loop, python, python for loop

Python for loops are one of the one of the most commonly used methods for iterating over a sequence. The Python for loop is similar to that of a for loop in other programming languages. View our for loop examples below.

Python for loop flowchart

Before we dive into examples of how to use for loops in Python, let's take a quick look at the for loop flowchart below. First we enter the loop, execute the body then we check a condition. Think of this as a high level view of how pretty much all for loops work across programming languages. The only thing that changes per language is the syntax.

 

Iterating over a string

We can use the Python for loop to iterate over the characters in a string. See the example below, let's assume that we want to loop over the letters in the word "strawberry".

for x in "strawberry":
  print(x)
#Output
s
t
r
a
w
b
e
r
r
y


Iterating over a collection

Iterating over a collection in Python is very similar to iterating over the characters, string except well we are iterating objects now. Check out the example below where we are given a list of fruit and printing them within a for loop.

fruits = ["apricot""blueberry""strawberry"]
for x in fruits:
  print(x)
#Output
apricot
blueberry
strawberry


Using the break statement

When we are iterating a for loop, sometimes it may be required to break at a certain condition. Let's use the same example as above and consider a scenario in which we need to stop looping when we reach the string "blueberry".

fruits = ["apricot""blueberry""strawberry"]
for x in fruits:
  if x == "blueberry":
    break
  print(x)
#Output
apricot

 

Using the continue statement

Similar to a scenario where we want to break in a for loop, we can also continue when a certain condition occurs. Consider the same example above, except instead of stopping when we reach "blueberry" we just want to skip over it. Check out the example below.

fruits = ["apricot""blueberry""strawberry"]
for x in fruits:
  if x == "blueberry":
    continue
  print(x)
#Output
apricot
strawberry


Using the range() method

If you need to loop through a specific range of values, you can use the range() method. Range() returns a sequence of numbers, you can pass it a single number whereas it starts at 0, or pass it two values an specify a min and max for the range. Let's look at this for loop below which shows how to loop using a range given a min/max range. There is also an overload to pass the increment number, we will not show this but it is used in the same manner as shown below.

for x in range(26):
  print(x) #Output
3
4
5


Else for loop

Another useful way to use Python for loops is to specify an else condition. This allows us to specify code that will be executed when the for loop finished. Let's take the Range() example from above and show what is possible when using else. Below we will display the text "I'm done!" after we finish executing all iterations of the for loop.

for x in range(26):
  print(x)
else:
  print("I'm done!")
#Output
3
4
5
I'm done!


Working with nested for loops

Last but certainly not least we have the nested for loops! Nested loops are great when dealing with iterating multiple collections at the same time. Suppose we have two arrays, fruit colors and fruits. For this example we are going to randomly assign a color to each fruit, in the real world you may be mapping two data points together.

colors = ["red""blue""green"]
fruits = ["apple""banana""cherry"]

for x in colors:
  for y in fruits:
    print(x, y)
#Output
red apple
red banana
red cherry
blue apple
blue banana
blue cherry
green apple
green banana
green cherry