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: While

Capítulo 12

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Control structures in Python are essential tools for any developer, as they allow you to control the flow of execution of your program. Among these structures is the while statement, which is the focus of this chapter in our ebook on building systems with Python and Django.

The while statement is a control structure that allows repeated execution of a block of code as long as a condition is true. The basic structure of the while statement in Python is as follows:

while condition:
    code block

In the structure above, "condition" is an expression that returns a Boolean value (True or False). The "code block" is a set of instructions that will be executed while the condition is true. When the condition becomes false, execution of the code block stops and control is passed to the next statement after the while.

Let's consider a simple example to better understand how the while statement works. Suppose we want to print the numbers 1 to 5. We can do this using the while statement as follows:

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

In the example above, the variable i is initialized to 1. The while condition is "i <= 5", which is true at the beginning. So the block of code inside the while is executed, printing the value of i and incrementing i by 1. This continues until i becomes 6, at which point the condition becomes false and execution of the while stops.

The while statement is very useful for situations where we don't know in advance how many times we need to execute a block of code. For example, in an authentication system, we can use a while loop to continue prompting the user to enter their password until they enter the correct password.

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

In addition, Python also provides a "break" statement that can be used to break out of a while loop before the condition becomes false. For example, we can modify the previous example to break out of the loop when i becomes 3 as follows:

i = 1
while i <= 5:
    if i == 3:
        break
    print(i)
    i = i + 1

In the example above, when i becomes 3, the break statement is executed and the while loop is stopped immediately, even though the while condition is still true.

In summary, the while statement is a powerful tool in Python that allows you to effectively control the flow of execution of your program. Understanding how it works is critical for any Python developer, whether you're a novice or a seasoned pro. In the next chapter of our ebook, we'll explore another important control structure in Python: the for statement.

Now answer the exercise about the content:

What is the function of the while statement in Python?

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

You missed! Try again.

The while statement in Python allows repeated execution of a block of code as long as a condition is true. This control structure is useful for looping through code when the number of iterations is not known in advance.

Next chapter

Control Structures in Python: For

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