Loops in Python 3

Written by Jacqueline Lim

What will I be covering today?

I will be covering the various types of loops that are found in Python 3 and other programming languages. I will use Python 3 as an example, though not all the loops look the same for every programming language.

First of all, what are loops?

Loops are used to execute/run a bunch of instructions/code multiple times without needing to rewrite it, and it also helps to simplify the programming process by not making the completed code too long and difficult to read.

What types of loops can be found in Python 3?

There are various loops, from the simple if-elif-else loop to the more complicated try-catch loops.

I will be covering most of the easy loops in this post, the more complicated ones will be in another post.

Loops that I will cover:

  • If-elif-else loop

  • For loop

  • While loop

  • Nested loops

  • Infinite loops

Here's how to use an if-elif-else loop:

The if statement checks a condition against a variable. For example: x = 1, then is x == 1? If the statement is true then the output should be Yes.

x = 1
if x == 1:
    print("Yes")

The elif statement kicks in when the if statement is false, but another condition can be applied to the same statement. For example, x = 2, if x == 1, then the output should be Yes

x = 2
if x == 1:
    print("Yes")
elif x == 2:
    print("Yes")

The else statement only kicks in when the previous if and elif statements have failed, it is the last condition that can be applied to the same statement. For example, x = 0, if x == 1, print Yes, elif x == 2, print Yes, else, print No

x = 0
if x == 1:
    print("Yes")
elif x == 2:
    print("Yes")
else:
    print("No")

Here's how to use a for loop:

The for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

x = 100
for (int i = 0; i < x; i++):
    x /= 10
print(x)

Here's how to use a While loop:

It is similar to the for loop, but it requires an external counter, which is a variable that is outside of the loop. It can then iterate the loop, which can then execute a set of statements as long as the condition is true.

x = 0
y = 3
while (x < 10):
    y *= 3
    x++
print(y)
#it will output the value of y, which will be 59049

Here's how to use a nested loop:

A nested loop is a loop that is within another loop, for example, an if loop is inside a for loop, and it is really common for this kind of loop to pop up, as they can help to save some time when writing code.

#for loop first, then if loop inside
x = 10
z = 100
for (int y = 0; y < 10; y++):
    x--
    if (x / 2 == 0):
        z -= x
print(z)
#output will be

What is an Infinite loop?

An infinite loop happens when the sequence of instructions in a computer program loops endlessly, either due to the loop having no exit condition. It can be done intentionally.

Why would an Infinite loop be done intentionally?

There are a few situations when this is desired. For example, the games on cartridge-based game consoles (like GBA/GBCA) typically have no exit condition in their main loop, as there is no operating system for the program to exit to; the loop runs until the console is powered off.

how_many = 0
while is_there_more_data() do
    how_many = how_many + 1
end
display "the number of items counted = " how_many

When is an Infinite loop unintentional?

When it is the result of a bug/not the intended result of the code.

Conclusion

There are many different types of loops in Python (or any programming language out there). And these are just the most common ones, there are also some advanced loops that are included in this post.

So have fun learning about loops and how to use them :) I'll be coming back with more articles soon.