4.3. Control Structures in Python: Loops

Página 7

Control structures in Python are essential programming tools that allow the developer to manipulate the flow of execution of a program. Among these structures, repeating loops play a critical role. They are used to run a block of code multiple times, which can save considerable time and effort.

There are two main types of loops in Python: the 'for' loop and the 'while' loop. Both allow a block of code to be repeated multiple times, but they work in slightly different ways.

1. '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) or other iterable objects. The basic syntax is as follows:

for value in string:
    # code block

The block of code inside the 'for' loop is executed once for each item in the sequence. Here is a simple example:

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

This will print the numbers from 0 to 4. The 'range()' function is often used with the 'for' loop to generate a sequence of numbers.

2. 'while' loop

The 'while' loop in Python is used to repeat a block of code while a specific condition is true. The basic syntax is as follows:

while condition:
    # code block

The block of code inside the 'while' loop will continue to execute as long as the condition is true. Here is a simple example:

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

This will print the numbers 0 through 4, just like the 'for' loop example above.

Loop control

Python also provides several instructions that allow you to control the flow of repeating loops. The most common ones are 'break', 'continue' and 'pass'.

1. Break

The 'break' statement is used to terminate the current loop and resume execution at the next statement after the loop. For example:

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

This will print the numbers 0 through 2, and then the loop will exit when i equals 3.

2. Continue

The 'continue' statement is used to skip the remainder of the code block within the current loop and continue with the next iteration of the loop. For example:

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

This will print the numbers 0 through 2 and 4, ignoring the number 3.

3. Pass

The 'pass' statement in Python is used when a statement is needed syntactically, but you don't want to execute any commands or code. It is a null operation - nothing happens when it is executed. It's useful as a placeholder when you're working on new code and haven't decided what to put there. For example:

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

This will print the numbers 0 to 4 as the 'pass' statement has no effect.

In summary, Python's loop control structures are powerful tools that allow you to manipulate the flow of execution of a program. They are essential for any Python programmer and are widely used in all types of programs, from simple scripts to complex software applications.

Now answer the exercise about the content:

_What is the difference between 'for' and 'while' loops in Python?

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

You missed! Try again.

Next page of the Free Ebook:

84.4. Control Structures in Python: Control Flow Commands

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text