The loop control framework in Python is one of the most important foundations for creating efficient and effective systems. Loops are used to run a specific piece of code multiple times until a specific condition is met. Python offers two main forms of loop control structures: the 'for' loop and the 'while' loop.

'for' loop

The 'for' loop in Python is used to iterate over a sequence (which can be a list, a tuple, a dictionary, a set, or a string). This type of loop executes the block of code for each item in the sequence and terminates when it reaches the end of the sequence.

For example:

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

In this example, the 'for' loop will print the numbers 0 through 4. The 'range()' function is used to generate a sequence of numbers that the 'for' loop will iterate through.

'while' loop

The 'while' loop in Python is used to iterate through a block of code while a specific condition is true. The loop will continue until the condition is false. It's important to note that if the condition never becomes false, the 'while' loop will continue indefinitely, creating an infinite loop.

For example:

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

In this example, the 'while' loop will print the numbers 0 to 4. The variable 'i' is incremented on each iteration of the loop.

Loop control

Python also provides several instructions that let you control the flow of loops. These include 'break', 'continue' and 'pass'.

'break'

The 'break' statement is used to end the current loop and resume execution at the next statement after the loop. For example, if we want to stop the 'for' loop when 'i' equals 3, we can do the following:

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

In this example, the 'for' loop will print the numbers 0 through 2. When 'i' becomes 3, the 'break' statement is executed and the loop is terminated.

'continue'

The 'continue' statement is used to skip the remainder of the code within the current loop and continue with the next iteration of the loop. For example, if we want to skip the iteration when 'i' equals 3, we can do the following:

for i in range(5):
    if i == 3:
        continues
    print(i)

In this example, the 'for' loop will print the numbers 0 through 2 and 4. When 'i' becomes 3, the 'continue' statement is executed and the rest of the code in the loop is ignored for that iteration.< /p>

'pass'

The 'pass' statement is used when a statement is syntactically needed, but the program does not require any action. For example:

for i in range(5):
    if i == 3:
        pass
    print(i)

In this example, the 'for' loop will print the numbers 0 through 4. When 'i' becomes 3, the 'pass' statement is executed, but nothing happens and the program continues as if the 'pass' statement did not. were there.

In short, Python's loop control structures are an essential part of Python programming. They allow programmers to run a block of code multiple times, which is useful for many tasks such as processing items in a list, repeating an action until a condition is met, and much more. Learning to effectively use loops and loop control statements can help you create more efficient and effective code.

Now answer the exercise about the content:

What are the two main forms of loop control structures in Python?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Control Structures in Python: If-else 11

Next page of the Free Ebook:

Control Structures in Python: If-else

Estimated reading time: 3 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks