{"id":31695,"date":"2024-11-01T09:01:50","date_gmt":"2024-11-01T09:01:50","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31695"},"modified":"2024-11-01T11:48:38","modified_gmt":"2024-11-01T11:48:38","slug":"python-course-while-statement","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31695\/","title":{"rendered":"Python Course: while statement"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Python Course: while Statement<\/h1>\n\n\n\n<p>In Python, there are mainly two ways to create loops: the&nbsp;<code>for<\/code>&nbsp;statement and the&nbsp;<code>while<\/code>&nbsp;statement. In this course, we will learn in detail how the&nbsp;<code>while<\/code>&nbsp;statement works and how to use it. The&nbsp;<code>while<\/code>&nbsp;statement allows the block of code to be executed as long as the given condition is&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Structure of the while Statement<\/h2>\n\n\n\n<p>The basic syntax of the&nbsp;<code>while<\/code>&nbsp;statement is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while condition:\n    block of code to execute\n    (usually includes code that makes the condition false)<\/code><\/pre>\n\n\n\n<p>In simple terms, for the&nbsp;<code>while<\/code>&nbsp;statement, the block of code below is repeatedly executed every time the condition is true. And when the condition becomes false, the loop ends. Now let&#8217;s look at a simple example of a&nbsp;<code>while<\/code>&nbsp;statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Example<\/h2>\n\n\n\n<p>Let&#8217;s examine how the&nbsp;<code>while<\/code>&nbsp;statement works through an example. Here, we will look at a simple example that prints the numbers from 1 to 5.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i = 1\nwhile i &lt;= 5:\n    print(i)\n    i += 1<\/code><\/pre>\n\n\n\n<p>In this example, the initial variable&nbsp;<code>i<\/code>&nbsp;is set to 1. The&nbsp;<code>while<\/code>&nbsp;statement continues executing the loop as long as the condition&nbsp;<code>i &lt;= 5<\/code>&nbsp;is true. Inside the loop, it prints the value of&nbsp;<code>i<\/code>&nbsp;and adds 1 to&nbsp;<code>i<\/code>. Eventually, when&nbsp;<code>i<\/code>&nbsp;becomes 6, the condition evaluates to false, and the loop ends.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Infinite Loop<\/h2>\n\n\n\n<p>If the condition is always true, the&nbsp;<code>while<\/code>&nbsp;statement can enter an infinite loop. This means that the program will keep running without stopping. Infinite loops can be useful when paired with control mechanisms that appropriately end the loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    user_input = input(\"Enter 'q' to exit: \")\n    if user_input == 'q':\n        break<\/code><\/pre>\n\n\n\n<p>The above code runs indefinitely because of the condition&nbsp;<code>True<\/code>, but when the user enters &#8216;q&#8217;, the loop will terminate through the&nbsp;<code>break<\/code>&nbsp;statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using an else Block with a while Statement<\/h2>\n\n\n\n<p>You can also attach an&nbsp;<code>else<\/code>&nbsp;block to the&nbsp;<code>while<\/code>&nbsp;statement. The&nbsp;<code>else<\/code>&nbsp;block is executed after the&nbps; <code>while<\/code>&nbsp;block is finished, unless the loop was terminated by a&nbsp;<code>break<\/code>&nbsp;statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i = 1\nwhile i &lt;= 5:\n    print(i)\n    i += 1\nelse:\n    print(\"The loop has ended naturally.\")<\/code><\/pre>\n\n\n\n<p>In this code, when&nbsp;<code>i<\/code>&nbsp;becomes 6, the&nbsp;<code>while<\/code>&nbsp;loop ends, and the statements in the&nbsp;<code>else<\/code>&nbsp;block are executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Applications of the while Statement<\/h2>\n\n\n\n<p>Through examples, we will explore some useful ways to use the&nbsp;<code>while<\/code>&nbsp;statement. Each example demonstrates how the&nbsp;<code>while<\/code>&nbsp;statement can be used to solve more complex problems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">User Input Validation<\/h3>\n\n\n\n<p>You can write a program that keeps requesting input from the user until valid input is received.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    number_str = input(\"Please enter a number: \")\n    try:\n        number = int(number_str)\n        break\n    except ValueError:\n        print(\"That's not a valid number. Please try again.\")<\/code><\/pre>\n\n\n\n<p>In this case, the program continues to prompt for input until the user enters a number. If an invalid input is received, it shows an error message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reading Until End of File<\/h3>\n\n\n\n<p>You can write a program that reads a file line by line and stops when there is no more data to read.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open('some_file.txt', 'r') as file:\n    line = file.readline()\n    while line:\n        print(line, end='')\n        line = file.readline()<\/code><\/pre>\n\n\n\n<p>This code opens a file and prints its content while reading each line sequentially. The loop ends when there are no more lines to read.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Things to Be Careful About When Using while Statements<\/h2>\n\n\n\n<p>When using the&nbsp;<code>while<\/code>&nbsp;statement, it is important to set the condition clearly to avoid getting stuck in an infinite loop. Infinite loops can often stop the program and create unintended situations. Therefore, within the loop, you should properly modify the condition or use statements like&nbsp;<code>break<\/code>&nbsp;to ensure the loop ends as intended.<\/p>\n\n\n\n<p>Additionally, when using the&nbsp;<code>while<\/code>&nbsp;statement, if the number of iterations is not properly limited, performance issues may arise. It is important to optimize operations within the loop and avoid unnecessary tasks to minimize long processing times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now you understand how the&nbsp;<code>while<\/code>&nbsp;statement works and have learned ways to use it in various situations. The&nbsp;<code>while<\/code>&nbsp;statement is a powerful structure that executes repeatedly as long as the given condition is true, and can be used in various scenarios such as infinite loops and user input validation. I hope this course enables you to use the&nbsp;<code>while<\/code>&nbsp;statement with confidence.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Course: while Statement In Python, there are mainly two ways to create loops: the&nbsp;for&nbsp;statement and the&nbsp;while&nbsp;statement. In this course, we will learn in detail how the&nbsp;while&nbsp;statement works and how to use it. The&nbsp;while&nbsp;statement allows the block of code to be executed as long as the given condition is&nbsp;True. Basic Structure of the while Statement &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31695\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Course: while statement&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[98],"tags":[95],"class_list":["post-31695","post","type-post","status-publish","format-standard","hentry","category--en","tag--en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Course: while statement - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31695\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Course: while statement - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Python Course: while Statement In Python, there are mainly two ways to create loops: the&nbsp;for&nbsp;statement and the&nbsp;while&nbsp;statement. In this course, we will learn in detail how the&nbsp;while&nbsp;statement works and how to use it. The&nbsp;while&nbsp;statement allows the block of code to be executed as long as the given condition is&nbsp;True. Basic Structure of the while Statement &hellip; \ub354 \ubcf4\uae30 &quot;Python Course: while statement&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31695\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:01:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:48:38+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31695\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31695\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Course: while statement\",\"datePublished\":\"2024-11-01T09:01:50+00:00\",\"dateModified\":\"2024-11-01T11:48:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31695\/\"},\"wordCount\":650,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"keywords\":[\"\ud30c\uc774\uc36c\uac15\uc88c\"],\"articleSection\":[\"Python Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31695\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31695\/\",\"name\":\"Python Course: while statement - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:01:50+00:00\",\"dateModified\":\"2024-11-01T11:48:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31695\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31695\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31695\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Course: while statement\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Course: while statement - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/31695\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Course: while statement - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Python Course: while Statement In Python, there are mainly two ways to create loops: the&nbsp;for&nbsp;statement and the&nbsp;while&nbsp;statement. In this course, we will learn in detail how the&nbsp;while&nbsp;statement works and how to use it. The&nbsp;while&nbsp;statement allows the block of code to be executed as long as the given condition is&nbsp;True. Basic Structure of the while Statement &hellip; \ub354 \ubcf4\uae30 \"Python Course: while statement\"","og_url":"https:\/\/atmokpo.com\/w\/31695\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:01:50+00:00","article_modified_time":"2024-11-01T11:48:38+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31695\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31695\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Course: while statement","datePublished":"2024-11-01T09:01:50+00:00","dateModified":"2024-11-01T11:48:38+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31695\/"},"wordCount":650,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"keywords":["\ud30c\uc774\uc36c\uac15\uc88c"],"articleSection":["Python Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31695\/","url":"https:\/\/atmokpo.com\/w\/31695\/","name":"Python Course: while statement - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:01:50+00:00","dateModified":"2024-11-01T11:48:38+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31695\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31695\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31695\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Course: while statement"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31695"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31695\/revisions"}],"predecessor-version":[{"id":31696,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31695\/revisions\/31696"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}