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: Control Flow Commands

Capítulo 8

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Control structures in Python are a crucial aspect of the programming language. They allow programmers to control the flow of execution of a program, making decisions based on specific conditions and repeating a block of code over and over again. These frameworks are fundamental to building complex and efficient systems with Python and Django.

Flow control statements in Python include conditional statements (like `if`, `elif`, `else`), loops (like `for`, `while`), and loop control (like `break`, `continue`). These commands are used to control the execution of blocks of code based on specific conditions or to repeat the execution of a block of code until a condition is met.

1. Conditional Structures

The `if` statement is the most basic conditional structure in Python. It checks for a condition and, if the condition is true, executes the next block of code. For example:

if 5 > 3:
    print("5 is greater than 3")

The `elif` command is used to check various conditions. If the condition in the `if` statement is false, Python checks the condition in the `elif` statement. If the condition in the `elif` statement is true, the following block of code is executed. For example:

if 5 < 3:
    print("5 is less than 3")
elif 5 > 3:
    print("5 is greater than 3")

The `else` command is used to execute a block of code when all the conditions in the `if` and `elif` commands are false. For example:

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

if 5 < 3:
    print("5 is less than 3")
elif 5 == 3:
    print("5 equals 3")
else:
    print("5 is greater than 3")

2. Loops

The `for` statement is used to repeat a block of code a specified number of times. For example, to print the numbers 1 to 5, you can use the following code:

for i in range(1, 6):
    print(i)

The `while` statement is used to repeat a block of code as long as a condition is true. For example, to print the numbers 1 to 5, you can use the following code:

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

3. Loop Control

The `break` command is used to break the execution of a loop. When Python encounters the `break` command, it exits the current loop and continues program execution from the code that follows the loop. For example:

for i in range(1, 6):
    if i == 3:
        break
    print(i)

The `continue` command is used to skip the current iteration of a loop and continue with the next iteration. When Python encounters the `continue` statement, it skips the rest of the block of code in the loop and continues with the next iteration. For example:

for i in range(1, 6):
    if i == 3:
        continues
    print(i)

In summary, control structures in Python are powerful tools that allow programmers to control the flow of execution of their programs. They are essential for building complex and efficient systems with Python and Django.

Now answer the exercise about the content:

Which of the following Python flow control statements is used to stop the execution of a loop?

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

You missed! Try again.

The Python flow control statement used to stop the execution of a loop is the break statement. When the interpreter encounters a break command within a loop, it immediately terminates the loop and transfers control to the statement following the loop. The continue statement, another loop control command, does not stop the loop but instead skips the current iteration and continues with the next one. Thus, break is the correct answer.

Next chapter

Control Structures in Python: Decision Structures

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