Loops

In Python, loops are used to repeat a block of code, like Subprograms (see here) but easier to understand.

There are many types of loops.

For Loops

We use for loops to go through specific lists, tuples, dictionaries, etc in a sequence. With for loops, we can run code for each item in the list/tuple/dictionary.

Printing

For example, if we want to print each fruit in a fruit list, we can use this-

fruits = ["apple", "orange", "banana"]
for x in fruits:
     print(x)

We can also loop through strings since they contain characters. This example of a for loop will print out all the letters in the word “fruit”-

for x in "fruit":
     print(x)

The range() Function

The range() function lets us loop through a code a specific number of times. It starts from 0.

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

Did you know that Python counts numbers in a different way? Instead of printing the numbers from 0 to 5, it only prints the numbers from 0 to 4! Yeah, Python is weird that way. You’ll need to be careful of that.

The starting number doesn’t always have to be 0! You can change it to other numbers, like this-

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

In this case Python will only print the numbers 2, 3 and 4. It’s a bit confusing at first.

You can also change the increment value by adding a third parameter. This loop will add to the sequence by 3, so the numbers will be 1,4,7,10 all the way to the end number.

for x in range(1,16,3):
     print(x)

While Loops

Using a while loop, a block of code will keep running based on the Boolean condition that is given. A Boolean condition can only either be True, or False. A code can run for infinity if it is not stopped.

number = 0
while number < 4:
    number = number + 1
    print("Hello there!")
Hello there!
Hello there!
Hello there!
Hello there!

In this example, the Boolean condition for the while loop will always be True and continue running, provided the number is below 4.

Setting Boolean Conditions

You can also set a Boolean condition to another variable that is used in while loops-

sunny = True

while sunny == True:
     print("Enjoy your day")

This code will keep running for an infinite number of times since the Boolean condition, sunny, is set to True.

For loops are usually used when you want to run a code for a set number of times. While loops, on the other hand, are used when you don’t know how many times you want to run your code.

Nested Loops

Python nested loops mean loops inside a loop! The inner/outer loop can either be a while or for loop, and for every iteration of the outer loop, the inner loop will run once.

For example, a for loop in a for loop:

colors = ["red, yellow, green"]
fruits = ["apple, banana, orange"]

for x in colors:
     for y in fruits:
          print(x,y)
red apple
red banana
red orange
yellow apple
yellow banana
yellow orange
green apple
green banana
green orange

However, using nested loops can make your code harder to read. Some people might find your code hard to tell which loop is which.

Conclusion

That’s all! I hope you found this guide helpful.