{"id":41,"date":"2024-01-24T14:02:32","date_gmt":"2024-01-24T14:02:32","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=41"},"modified":"2024-01-24T14:02:35","modified_gmt":"2024-01-24T14:02:35","slug":"python-while","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-while\/","title":{"rendered":"Python while"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python&nbsp;<code>while<\/code>&nbsp;statement and how to use it to run a code block as long as a condition is true.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python&nbsp;<code>while<\/code>&nbsp;statement<\/h2>\n\n\n\n<p>Python&nbsp;<code>while<\/code>&nbsp;statement allows you to execute a code block repeatedly as long as a condition is&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>The following shows the syntax of the Python\u00a0<code>while<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>while condition: body<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>condition<\/code>\u00a0is an expression that evaluates to a\u00a0boolean\u00a0value, either\u00a0<code>True<\/code>\u00a0or\u00a0<code>False<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>while<\/code>&nbsp;statement checks the condition at the beginning of each iteration. It\u2019ll execute the body as long as the&nbsp;<code>condition<\/code>&nbsp;is True.<\/p>\n\n\n\n<p>In the body of the loop, you need to do something to stop the loop at some time.<\/p>\n\n\n\n<p>Otherwise, you\u2019ll get an&nbsp;<strong>indefinite loop<\/strong>&nbsp;that will run forever until you close the application.<\/p>\n\n\n\n<p>Because the&nbsp;<code>while<\/code>&nbsp;statement checks the&nbsp;<code>condition<\/code>&nbsp;at the beginning of each iteration, it\u2019s called a&nbsp;<strong>pretest loop<\/strong>.<\/p>\n\n\n\n<p>If the&nbsp;<code>condition<\/code>&nbsp;is&nbsp;<code>False<\/code>&nbsp;from the beginning, the&nbsp;<code>while<\/code>&nbsp;statement will do nothing.<\/p>\n\n\n\n<p>The following flowchart illustrates the&nbsp;<code>while<\/code>&nbsp;loop statement:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-while-loop.png\" alt=\"\" class=\"wp-image-840\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Python while statement examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the Python&nbsp;<code>while<\/code>&nbsp;statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Simple Python while statement example<\/h3>\n\n\n\n<p>The following example uses a\u00a0<code>while<\/code>\u00a0statement to show 5 numbers from 0 to 4 to the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>max = 5 counter = 0 while counter &lt; max: print(counter) counter += 1<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0 1 2 3 4<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define two variables called&nbsp;<code>max<\/code>&nbsp;and&nbsp;<code>counter<\/code>&nbsp;with the initial values of five and zero.<\/li>\n\n\n\n<li>Second, use the&nbsp;<code>while<\/code>&nbsp;statement with the condition&nbsp;<code>counter &lt; max<\/code>. It\u2019ll execute the loop body as long as the value of the&nbsp;<code>counter<\/code>&nbsp;is less than the value of&nbsp;<code>max<\/code>.<\/li>\n\n\n\n<li>Third, show the value of the&nbsp;<code>counter<\/code>&nbsp;variable and increase it by one in each iteration. After five iterations, the value of the&nbsp;<code>counter<\/code>&nbsp;is 5, which makes the condition&nbsp;<code>counter &lt; max<\/code>&nbsp;evaluates to&nbsp;<code>False<\/code>&nbsp;and hence the loop stops.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using the Python while statement to build a simple command prompt program<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>while<\/code>\u00a0statement to prompt users for input and echo the command that you entered back. It\u2019ll run as long as you don\u2019t enter the\u00a0<code>quit<\/code>\u00a0command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>command = '' while command.lower() != 'quit': command = input('>') print(f\"Echo: {command}\") <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Note that the&nbsp;<code>command.lower()<\/code>&nbsp;returns the command in lowercase format. This allows you to enter the quit command such as&nbsp;<code>quit<\/code>,&nbsp;<code>QUIT<\/code>, or&nbsp;<code>Quit<\/code>.<\/p>\n\n\n\n<p>Example output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>Hi Echo: Hi >Python while Echo: Python while >quit Echo: quit<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python&nbsp;while&nbsp;statement and how to use it to run a code block as long as a condition is true. Introduction to the Python&nbsp;while&nbsp;statement Python&nbsp;while&nbsp;statement allows you to execute a code block repeatedly as long as a condition is&nbsp;True. The following shows the syntax of the Python\u00a0while\u00a0statement: The\u00a0condition\u00a0is an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-3-control-flow"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":42,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/41\/revisions\/42"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}