Language: EN

python-bucles

Loops in Python

Loops are a tool that allows us to repeat a block of code multiple times. In Python, we have two main types of loops: for and while.

for Loop

The for loop is used to iterate over a sequence (like a list, tuple, dictionary, etc.) and execute a block of code for each element in that sequence.

The basic syntax of the for loop is as follows:

for element in sequence:
    # block of code to execute for each element

Examples of for loop

while Loop

The while loop is used to repeat a block of code while a condition is true. The basic syntax of the while loop is as follows:

while condition:
    # block of code to execute while the condition is true

Examples of while loop

For example, we can use a while loop to count from 1 to 5:

counter = 1

while counter <= 5:
    print(counter)
    counter += 1

In this case, the while loop will execute as long as the condition is true, that is, while counter is less than or equal to 5. In each iteration, the value of counter is printed and then incremented by 1. The output will be:

1
2
3
4
5

It is important to be careful with conditions in while loops, as if the condition never becomes false, the loop will run indefinitely, which can cause an infinite loop

break and continue Statements

Within loops in Python, we can also use the break and continue statements to control the flow of execution.

Ends the loop and executes the block of code that is after the loop.

Let’s see an example using break to exit a for loop:

fruits = ["apple", "banana", "cherry", "watermelon", "grape"]

for fruit in fruits:
    print(fruit)
    if fruit == "watermelon":
        break

In this case, the for loop will print each fruit from the list until it reaches “watermelon”, at which point break will be executed and the loop will stop.

On the other hand, continue allows us to skip to the next iteration without executing the rest of the block of code for that iteration. Let’s see an example:

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number % 2 == 0:
        continue
    print(number)

In this example, the for loop iterates over the numbers in the numbers list. When it encounters an even number (divisible by 2), continue is executed, meaning that print(number) is not executed for that number, and it skips to the next iteration.

In certain situations, break and continue can help improve the readability of the code. However, in general, it’s best not to overuse them as they can make it difficult to follow the flow of the code.

Loops with else

In Python, loops can also have an else clause, which is executed when the loop ends without being interrupted by a break. Let’s see an example:

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 0:
        break
else:
    print("The loop has finished without finding the number 0")

In this case, since there is no 0 in the numbers list, the loop will execute completely and at the end it will print “The loop has finished without finding the number 0”.