4.8. Control Structures in Python: While

Página 12

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.

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.

Next page of the Free Ebook:

134.9. Control Structures in Python: For

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