In Python, loops allow you to execute a block of code multiple times, either for a fixed number of iterations or until a certain condition is met. There are two main types of loops in Python: “for” loops and “while” loops. In this article, we will learn about these two types of loops and how to use them to solve real-world problems.

The “for” Loop

The “for” loop is used to iterate over a sequence of elements, such as a list, tuple, or string. The syntax for a “for” loop is as follows:

for variable in sequence:
    # code to be executed for each iteration

For example, consider the following code:

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

The output of this code will be “0 1 2 3 4”.

In the above example, the “for” loop iterates over the sequence “range(5)”, which generates a sequence of numbers from 0 to 4, and the variable “i” is assigned the value of each element in the sequence in turn. The code inside the loop is then executed for each iteration, and in this case, it prints the value of “i”.

You can also use the “for” loop to iterate over a list of elements. For example:

names = ["John", "Sarah", "Mike"]
for name in names:
    print(name)

The output of this code will be “John Sarah Mike”.

The “while” Loop

The “while” loop is used to execute a block of code repeatedly until a certain condition is met. The syntax for a “while” loop is as follows:

while condition:
    # code to be executed while condition is true

For example, consider the following code:

i = 0
while i < 5:
    print(i)
    i += 1

The output of this code will be “0 1 2 3 4”.

In the above example, the “while” loop continues to execute the code inside the loop as long as the condition “i < 5” is true. The variable “i” is incremented by 1 each iteration, and when “i” reaches 5, the condition is no longer true and the loop terminates.

It is important to include a way to update the condition inside the loop, otherwise the loop will become an infinite loop and continue to execute indefinitely.

Conclusion

In this article, we learned about the two main types of loops in Python: “for” loops and “while” loops. We saw how to use these loops to iterate over sequences and execute a block of code repeatedly until a certain condition is met. Understanding how to use loops is an essential skill for any Python programmer.

In our “Introduction to Python” course, we will cover these concepts in more depth and show you how to use loops to solve real-world problems. We hope you will join us on this journey!

Exercises

Here are some exercises with solutions to help you practice what you just learned:

How do you use a “for” loop in Python?

To use a “for” loop in Python, you can specify a variable and a sequence of elements to iterate over after the “for” keyword, and a block of code to be executed for each iteration. The syntax for a “for” loop is as follows:

for variable in sequence:
    # code to be executed for each iteration

For example, consider the following code:

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

The output of this code will be “0 1 2 3 4”.

How do you use a “while” loop in Python?

To use a “while” loop in Python, you can specify a condition after the “while” keyword and a block of code to be executed while the condition is true. The syntax for a “while” loop is as follows:

while condition:
    # code to be executed while condition is true

For example, consider the following code:

i = 0
while i < 5:
    print(i)
    i += 1

The output of this code will be “0 1 2 3 4”.

How do you use a “break” statement in a loop in Python?

To use a “break” statement in a loop in Python, you can specify the “break” keyword inside the loop to exit the loop early. The syntax for a “break” statement is as follows:

for variable in sequence:
    # code to be executed for each iteration
    if condition:
        break

For example, consider the following code:

for i in range(10):
    if i == 5:
        break
    print(i)

The output of this code will be “0 1 2 3 4”.

How do you use a “continue” statement in a loop in Python?

To use a “continue” statement in a loop in Python, you can specify the “continue” keyword inside the loop to skip the rest of the current iteration and move on to the next one. The syntax for a “continue” statement is as follows:

for variable in sequence:
    # code to be executed for each iteration
    if condition:
        continue

For example, consider the following code:

for i in range(10):
    if i % 2 == 0:
        continue
    print(i)

The output of this code will be “1 3 5 7 9”.

How do you use the “else” clause in a loop in Python?

To use the “else” clause in a loop in Python, you can specify the “else” keyword after the loop and a block of code to be executed if the loop terminates normally (i.e., not through a “break” statement). The syntax for a loop with an “else” clause is as follows:

for variable in sequence:
    # code to be executed for each iteration
else:
    # code to be executed if loop terminates normally

For example, consider the following code:

for i in range(10):
    if i == 5:
        break
else:
    print("The loop terminated normally")

The output of this code will be nothing, because the loop is terminated early through the “break” statement. If we remove the “break” statement, the output will be “The loop terminated normally”.

The “else” clause can also be used with “while” loops in the same way. For example:

i = 0
while i < 10:
    if i == 5:
        break
    i += 1
else:
    print("The loop terminated normally")

The output of this code will be nothing if the “break” statement is included, or “The loop terminated normally” if it is removed.