{"id":31699,"date":"2024-11-01T09:01:52","date_gmt":"2024-11-01T09:01:52","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31699"},"modified":"2024-11-01T11:48:37","modified_gmt":"2024-11-01T11:48:37","slug":"chapter-03-building-the-structure-of-a-python-program-control-statements","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31699\/","title":{"rendered":"Chapter 03: Building the Structure of a Python Program! Control Statements"},"content":{"rendered":"\n<p>In this chapter, we will delve into the control statements in Python with a deep understanding. Control statements are important tools that manage the flow of a program and make logical decisions. By effectively utilizing control statements, we can write more complex and efficient programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Basic Concepts of Control Statements<\/h2>\n\n\n\n<p>In programming, control statements are commands that change the execution flow of code. Controlling this flow allows the execution or repetition of code based on conditions, ensuring that the program operates correctly and runs more efficiently.<\/p>\n\n\n\n<p>Common control statements used in Python include conditional statements and loop statements. Conditional statements are used to decide whether to execute a code block based on a given condition, while loop statements are used when a specific code block needs to be executed multiple times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Conditional Statements: if, elif, else<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 Basic If Statement<\/h3>\n\n\n\n<p>Conditional statements allow us to implement the logic of &#8216;if some condition is true, execute this code&#8217;. A basic if statement is written as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nif condition:\n    code_to_execute\n        <\/code><\/pre>\n\n\n\n<p>Here, if the condition is true, the indented code block will be executed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 If-Else Statement<\/h3>\n\n\n\n<p>An if statement can be combined with an else statement to specify an alternative code to execute when the condition is false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nif condition:\n    code_to_execute_if_true\nelse:\n    code_to_execute_if_false\n        <\/code><\/pre>\n\n\n\n<p>This structure allows us to write more robust code by providing a code block to execute when the condition is false.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3 If-Elif-Else Statement<\/h3>\n\n\n\n<p>When multiple conditions are needed, we can use elif (short for else if). This checks multiple conditions in order and executes only the code block for the first true condition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nif condition1:\n    code_to_execute1\nelif condition2:\n    code_to_execute2\nelse:\n    code_to_execute_default\n        <\/code><\/pre>\n\n\n\n<p>This structure allows us to execute only the corresponding code block if any of several conditions are met, and specifies a default code block to execute otherwise.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Loop Statements: for, while<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 While Statement<\/h3>\n\n\n\n<p>The while statement is used to repeatedly execute a code block as long as a given condition is true. Here is the basic structure of a while statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nwhile condition:\n    code_to_repeat\n        <\/code><\/pre>\n\n\n\n<p>When the condition is true, the code block to repeat is executed, and the repetition ends when the condition becomes false.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 For Statement<\/h3>\n\n\n\n<p>The for statement is primarily used when the number of repetitions is specified or when iterating over items of iterable objects (e.g., lists, tuples, strings, etc.).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfor variable in iterable_object:\n    code_to_repeat\n        <\/code><\/pre>\n\n\n\n<p>The code to repeat is executed for each element of the iterable object, and the iteration ends when there are no more elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Nested Control Statements<\/h2>\n\n\n\n<p>Control statements can be nested within each other. This is useful for implementing complex conditions or looping structures. For example, you can place an if statement inside a for statement or a while statement inside a for statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfor i in range(5):\n    if i % 2 == 0:\n        print(f\"{i} is even.\")\n    else:\n        print(f\"{i} is odd.\")\n        <\/code><\/pre>\n\n\n\n<p>The above example iterates over numbers from 0 to 4, determining whether each number is even or odd and printing the result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. break and continue Statements<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 Break Statement<\/h3>\n\n\n\n<p>The break statement immediately terminates a loop. It is commonly used when you want to stop the loop when a specific condition is met.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfor i in range(10):\n    if i == 5:\n        break\n    print(i)\n        <\/code><\/pre>\n\n\n\n<p>The code above iterates from 0 to 9, but stops repeating when it reaches 5.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 Continue Statement<\/h3>\n\n\n\n<p>The continue statement skips the remaining code in the current iteration and starts the next iteration. It is useful when you want to perform the next iteration before checking the next condition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfor i in range(5):\n    if i == 2:\n        continue\n    print(i)\n        <\/code><\/pre>\n\n\n\n<p>This example iterates from 0 to 4, skipping 2 and printing the remaining numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Example Program Using Control Statements<\/h2>\n\n\n\n<p>Finally, let&#8217;s write a simple program utilizing the control statements we&#8217;ve learned. Here, we will create a program that generates a Fibonacci sequence based on a number entered by the user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# Fibonacci sequence generator\ndef fibonacci(n):\n    a, b = 0, 1\n    while a < n:\n        print(a, end=' ')\n        a, b = b, a + b\n    print()\n\nnum = int(input(\"Please enter the range to generate the Fibonacci sequence: \"))\nfibonacci(num)\n        <\/code><\/pre>\n\n\n\n<p>This program generates and prints the Fibonacci sequence up to the number entered by the user. It uses a while statement to calculate the sequence and limits the generation of the sequence based on user input.<\/p>\n\n\n\n<p>In this chapter, we learned about the basic control statements in Python. Control statements are important components that determine the logical flow of a program, and understanding and utilizing them well allows us to write stronger and easier-to-maintain code. In the next chapter, we will expand on control statements and cover various application scenarios.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter, we will delve into the control statements in Python with a deep understanding. Control statements are important tools that manage the flow of a program and make logical decisions. By effectively utilizing control statements, we can write more complex and efficient programs. 1. Basic Concepts of Control Statements In programming, control statements &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31699\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Chapter 03: Building the Structure of a Python Program! Control Statements&#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-31699","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>Chapter 03: Building the Structure of a Python Program! Control Statements - \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\/31699\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Chapter 03: Building the Structure of a Python Program! Control Statements - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this chapter, we will delve into the control statements in Python with a deep understanding. Control statements are important tools that manage the flow of a program and make logical decisions. By effectively utilizing control statements, we can write more complex and efficient programs. 1. Basic Concepts of Control Statements In programming, control statements &hellip; \ub354 \ubcf4\uae30 &quot;Chapter 03: Building the Structure of a Python Program! Control Statements&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31699\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:01:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:48:37+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\/31699\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31699\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Chapter 03: Building the Structure of a Python Program! Control Statements\",\"datePublished\":\"2024-11-01T09:01:52+00:00\",\"dateModified\":\"2024-11-01T11:48:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31699\/\"},\"wordCount\":674,\"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\/31699\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31699\/\",\"name\":\"Chapter 03: Building the Structure of a Python Program! Control Statements - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:01:52+00:00\",\"dateModified\":\"2024-11-01T11:48:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31699\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31699\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31699\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Chapter 03: Building the Structure of a Python Program! Control Statements\"}]},{\"@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":"Chapter 03: Building the Structure of a Python Program! Control Statements - \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\/31699\/","og_locale":"ko_KR","og_type":"article","og_title":"Chapter 03: Building the Structure of a Python Program! Control Statements - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this chapter, we will delve into the control statements in Python with a deep understanding. Control statements are important tools that manage the flow of a program and make logical decisions. By effectively utilizing control statements, we can write more complex and efficient programs. 1. Basic Concepts of Control Statements In programming, control statements &hellip; \ub354 \ubcf4\uae30 \"Chapter 03: Building the Structure of a Python Program! Control Statements\"","og_url":"https:\/\/atmokpo.com\/w\/31699\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:01:52+00:00","article_modified_time":"2024-11-01T11:48:37+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\/31699\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31699\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Chapter 03: Building the Structure of a Python Program! Control Statements","datePublished":"2024-11-01T09:01:52+00:00","dateModified":"2024-11-01T11:48:37+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31699\/"},"wordCount":674,"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\/31699\/","url":"https:\/\/atmokpo.com\/w\/31699\/","name":"Chapter 03: Building the Structure of a Python Program! Control Statements - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:01:52+00:00","dateModified":"2024-11-01T11:48:37+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31699\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31699\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31699\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Chapter 03: Building the Structure of a Python Program! Control Statements"}]},{"@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\/31699","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=31699"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31699\/revisions"}],"predecessor-version":[{"id":31700,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31699\/revisions\/31700"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}