{"id":32806,"date":"2021-05-04T10:21:39","date_gmt":"2021-05-04T08:21:39","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=32806"},"modified":"2021-05-04T10:40:40","modified_gmt":"2021-05-04T08:40:40","slug":"how-to-make-a-while-loop-in-python","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/blog-python\/como-hacer-un-bucle-while-en-python\/","title":{"rendered":"While Loop in Python"},"content":{"rendered":"<p>In previous weeks we talked to you about the <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/python-reserved-words\/\" class=\"rank-math-link\">reserved words or keywords<\/a>, in that list of words<strong> \u201cwhile\u201d was included<\/strong> and we told you that it was used for loops, well, let&#039;s see <strong>How those loops work.<\/strong> If you want to perform<strong> programming jobs<\/strong>, you must train in this area and for that we recommend the <strong>&nbsp;<a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\" class=\"rank-math-link\">Master of Advanced Programming in Python for Big Data, Hacking and Machine Learning<\/a>&nbsp;<\/strong>in&nbsp;<a href=\"https:\/\/eiposgrados.com\/eng\/\">EIP<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Loop operation<\/h2>\n\n\n\n<p>As we can see in the diagram below, <strong>loop operation <em>while<\/em> it&#039;s simple,<\/strong> 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 <em><strong>true <\/strong><\/em>and <strong>instructions will be executed<\/strong> rechecking the condition when they have been completed.<\/p>\n\n\n\n<p>If, on the other hand, the result of the condition check was<strong> <em>False<\/em><\/strong>, <strong>the loop would end<\/strong> and no instruction would be carried out.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/O3yK6zFcPF9-dS5JdysvBbmN08c9h2nRkDSJpBoMWGhzKRMDgEvmjf3_txu_qY0QfCZnJag21Btu0-gppkRTqyAMloKFXWhBJDLUIHe8UhMGS3tgpoj-GpA2ibmYKlFshX6wf2EZ8p_mcAngQg\" alt=\"how to make a while loop in python\" width=\"424\" height=\"365\" title=\"\"><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Let&#039;s see it with examples<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><span style=\"font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;\">In the following code we will <strong>run a loop <\/strong><\/span><em style=\"font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;\"><strong>while<\/strong><\/em><span style=\"font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;\"><strong> so that it prints a countdown from 10 to 0.<\/strong><\/span><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"214\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.48.44-1024x214.png\" alt=\"\" class=\"wp-image-32807\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.48.44-1024x214.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.48.44-300x63.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.48.44-768x161.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.48.44.png 1396w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>In the code we declare the variable i with a value of 10<\/strong>, in the condition we indicate that as long as the value of &#039;i&#039; is greater than or equal to zero it enters the loop and once inside the loop we print the value of &#039;i&#039; and subtract one from it so that it goes down. Once &#039;i&#039; is &#039;-1&#039;, the condition is not met, so it would not enter the loop printing the message that the countdown has ended.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In the following example <strong>let&#039;s make a loop <em>while<\/em> controlled by event.<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"146\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.13-1024x146.png\" alt=\"\" class=\"wp-image-32808\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.13-1024x146.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.13-300x43.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.13-768x110.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.13.png 1414w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>The loop works as long as the user does not end it by entering a zero<\/strong>. We can see that the condition is that the number entered is different from zero.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Now let&#039;s go <strong>combine a while loop with a &#039;<em>else&#039;<\/em><\/strong>, unlike the conditional &#039;<em>if&#039;<\/em>, the complete block &#039;<em>else&#039;<\/em> will always be executed when the loop ends <em>while<\/em>.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"248\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.38-1024x248.png\" alt=\"\" class=\"wp-image-32809\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.38-1024x248.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.38-300x73.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.38-768x186.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.49.38.png 1428w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Starting from the previous example, <strong>We have added a new variable called &#039;sum&#039;<\/strong> where we have been adding the numbers entered. Once the loop is finished, the sum variable will be printed.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Now let&#039;s see an example of<strong> use of the sentence &#039;<em>break&#039;<\/em><\/strong><em> <\/em>to end a loop.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"174\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.33-1024x174.png\" alt=\"\" class=\"wp-image-32810\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.33-1024x174.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.33-300x51.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.33-768x131.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.33.png 1432w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As we can see though <strong>the condition is met<\/strong>, when the number has the value of 6 we enter the <em>&#039;if&#039; <\/em>and the sentence <em>break<\/em> It takes us out of it.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Let&#039;s see now <strong>An example of the use of the continue statement.<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"224\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.55-1024x224.png\" alt=\"\" class=\"wp-image-32811\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.55-1024x224.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.55-300x66.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.55-768x168.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/Captura-de-pantalla-2021-05-04-a-las-9.51.55.png 1426w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this last example, starting from the previous example, we see that when we reach the number 6 and enter the &#039;<em>if&#039;<\/em>, sentence<strong> <em>continue<\/em> <\/strong>makes it automatically jump to the beginning of the loop, ignoring the rest of the instructions and stopping printing the value 6.<\/p>","protected":false},"excerpt":{"rendered":"<p>We explain how to make a While Loop in Python and how they work. So that it is understood in the best way, we explain it to you with examples!<\/p>","protected":false},"author":51,"featured_media":32825,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-32806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/32806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=32806"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/32806\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/32825"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=32806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=32806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=32806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}