Loops (For and While)

Written by Anish Ahuja (8/9/2020)

Introduction

In this lesson, we will learn about using for and while loops. These loops are important to iteration in Python, which is useful when rerunning functions.

While Loops

While loops are, as suggested by the name, statements that repeat while a condition is true. The notation is written as such

x = 3
while (x < 10):
    print (x)
    x = x + 1

In this function, the set of statements in the while loop continues repeating until cat is no longer less than 10. The code does not progress until the while loop is complete.

For Loops

For loops are used to iterate over a function a set amount of times. For loops can be written in multiple ways. To iterate over every item in a list or set of data, this is the notation:

people = ["Ferhawn", "Harnoor", "Anish", "Nora", "Gillian"]
for x in people:
    print (x)

This function iterates and prints every item in the list of people. Within a for loop, break and continue commands can be implemented. Break will exit the loop entirely while continue will exit the specific iteration. Here is an example of break

characters = ["waluigi", "wario", "mario", "luigi", "peach"]
for x in characters:
    print (x)
    if (x == "wario"):
        break
    

This loop will end once the character wario is reached in the list. In contrast, here is the same example using continue.

characters = ["waluigi", "wario", "mario", "luigi", "peach"]
for x in characters:
    if (x == "wario"):
        continue
    print (x)

In this example, only wario is skipped as the for loop would end the current iteration if the term wario is reached. However, it would continue running through the other characters in the list.

For Loops with a Range

For loops can be written with a range instead of a list or set. Here is how:

for x in range(5):
    print (x)

This loop will print the numbers from 0 through 4. The loop prints 5 numbers, with 0 as the starting number, instead of 1. To change the beginning number, edit the code like so:

for x in range (2,5):
    print (x)

This loop will print the numbers 2 through 5, but will not include the number 5. It edits the starting number and changes it to 2.

To go through a ranged for loop with a set interval, a 3rd number is added to the range. Look at the example below:

for x in range (2,30, 3):
    print (x)

This for loop will print the numbers between 2 and 29, counting by 3s.

Else with For Loops

After a for loop completes iterating, an else statement can be added to direct its next actions. This can be seen in the example below.

for x in range (3):
    print ("the sun")
else:
    print ("is hot")

This for loop would print the word evan 3 times. After the for loop, the function would print is short.

Nested For Loops

Nested for loops are for loops placed within for loops. The inner for loop will execute once for every time the outer for loop iterates. Look at the example below, which will combine every possible pair of terms from the first list with the terms in the second list.

firstTerm = ["giant", "puny", "annoying"]
secondTerm = ["vampire", "cat", "pirahna"]

for x in firstTerm:
    for y in secondTerm:
        print (x, y)

Pass Statement

While for loops should not be empty, as they will simply waste resources, if a for loop is left empty, use a pass statement to avoid error messages.

for x in range (87):
    pass

Conclusion

In this lesson, we learned about for loops. For loops are important functions in iteration in Python and can be applied in multiple ways, as shown here. We will explore for loops in projects later, so it is important that you get the basics down now.

Last updated