+ INFORMATION

Share on social networks!

While Loop in Python

In previous weeks we talked to you about the reserved words or keywords, in that list of words “while” was included and we told you that it was used for loops, well, let's see How those loops work. If you want to perform programming jobs, you must train in this area and for that we recommend the  Master of Advanced Programming in Python for Big Data, Hacking and Machine Learning in EIP.

Loop operation

As we can see in the diagram below, loop operation while it's simple, When we enter the loop, it is verified that the condition that we have indicated is met, if so the result of the verification of that condition will be true and instructions will be executed rechecking the condition when they have been completed.

If, on the other hand, the result of the condition check was False, the loop would end and no instruction would be carried out.

how to make a while loop in python

Let's see it with examples

  • In the following code we will run a loop while so that it prints a countdown from 10 to 0.
Screenshot 2021 05 04 at 9.48.44

In the code we declare the variable i with a value of 10, in the condition we indicate that as long as the value of 'i' is greater than or equal to zero it enters the loop and once inside the loop we print the value of 'i' and subtract one from it so that it goes down. Once 'i' is '-1', the condition is not met, so it would not enter the loop printing the message that the countdown has ended.

  • In the following example let's make a loop while controlled by event.
Screenshot 2021 05 04 at 9.49.13

The loop works as long as the user does not end it by entering a zero. We can see that the condition is that the number entered is different from zero.

  • Now let's go combine a while loop with a 'else', unlike the conditional 'if', the complete block 'else' will always be executed when the loop ends while.
Screenshot 2021 05 04 at 9.49.38

Starting from the previous example, We have added a new variable called 'sum' where we have been adding the numbers entered. Once the loop is finished, the sum variable will be printed.

  • Now let's see an example of use of the sentence 'break' to end a loop.
Screenshot 2021 05 04 at 9.51.33

As we can see though the condition is met, when the number has the value of 6 we enter the 'if' and the sentence break It takes us out of it.

  • Let's see now An example of the use of the continue statement.
Screenshot 2021 05 04 at 9.51.55

In this last example, starting from the previous example, we see that when we reach the number 6 and enter the 'if', sentence continue makes it automatically jump to the beginning of the loop, ignoring the rest of the instructions and stopping printing the value 6.

Subscribe to our newsletter to stay up to date with all the news

EIP International Business School informs you that the data in this form will be processed by Mainjobs Internacional Educativa y Tecnológica, SA as the person responsible for this website. The purpose of collecting and processing personal data is to respond to the query made as well as to send information about the services of the data controller. Legitimation is the consent of the interested party.
You can exercise your rights of access, rectification, limitation and deletion of data in compliance@grupomainjobs.com as well as the right to file a claim with the supervisory authority. You can consult additional and detailed information on Data Protection in the Privacy Policy that you will find in our Web page
marter-in-python

1 thought on “Bucle While en Python”

Leave a comment