Free Ebook cover System creation course with Python and Django complete

System creation course with Python and Django complete

New course

176 pages

Control Structures in Python: Repeating Structures

Capítulo 6

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Python Control Structures: Repetition Structures

The control structures in Python are powerful features that allow the programmer to control the flow of execution of the program. Among these frameworks, looping frameworks stand out for their ability to execute a block of code multiple times. In Python, we have two main loop 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) or any other iterable object. The basic syntax of the 'for' loop is as follows:

for value in string:
    # block of code to be repeated

In this loop, the 'value' variable takes the value of each element in the sequence, one at a time, and the block of code inside the loop is executed for each value.

For example, we can use the 'for' loop to print all the numbers in a list:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)

This will print the numbers 1 through 5, each on a new line.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

'while' loop

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

while condition:
    # block of code to be repeated

In this loop, the condition is checked before each iteration. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop terminates.

For example, we can use the 'while' loop to print all numbers from 1 to 5:

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

This will also print the numbers 1 through 5, each on a new line.

Loop control

In Python, we also have a few keywords that can be used to control the flow of loops: 'break' and 'continue'.

The 'break' keyword is used to end the current loop and resume execution at the next statement after the loop. For example, we can use 'break' to break a 'for' loop when a certain number is found:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num == 3:
        break
    print(num)

This will print the numbers 1 and 2. When the number 3 is found, the loop stops and the program continues to the next statement after the loop.

The 'continue' keyword is used to skip the remainder of the code block within the loop and continue with the next iteration of the loop. For example, we can use 'continue' to skip printing a certain number:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num == 3:
        continues
    print(num)

This will print the numbers 1, 2, 4, and 5. When the number 3 is found, the rest of the block of code is skipped and the loop continues with the next number.

In summary, looping structures in Python are powerful tools that allow the programmer to efficiently control the flow of program execution. With practice, you will become more and more comfortable using these structures in your programs.

Now answer the exercise about the content:

What are the two main looping structures in Python and what is the function of each?

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

You missed! Try again.

The text describes two main looping structures in Python: the 'for' loop and the 'while' loop. The 'for' loop is used to iterate over a sequence or any iterable object, executing a block of code for each element. The 'while' loop repeats a block of code as long as a condition is true, checking the condition before each iteration. Thus, option 1 correctly describes these functions.

Next chapter

Control Structures in Python: Loops

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