{"id":399148,"date":"2025-12-16T09:04:11","date_gmt":"2025-12-16T08:04:11","guid":{"rendered":"https:\/\/dutable.com\/?p=399148"},"modified":"2025-12-16T16:38:13","modified_gmt":"2025-12-16T15:38:13","slug":"control-flow-in-python-if-elif-else","status":"publish","type":"post","link":"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/","title":{"rendered":"Introduction to Control Flow in Python: Using Conditional Statements"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#What_are_Conditional_Statements\" >What are Conditional Statements?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#Conditional_Statement_Types_in_Python\" >Conditional Statement Types in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#The_if_Statement_The_Introduction_to_Decision-Making\" >The if Statement: The Introduction to Decision-Making<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#The_elif_Statement_Condition_Testing\" >The elif Statement: Condition Testing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#The_else_Statement_The_Fallback_Path\" >The else Statement: The Fallback Path<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#Combinations_of_Multiple_Conditions_and_Logical_Operators\" >Combinations of Multiple Conditions and Logical Operators<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#Nested_Conditional_Statements_Decision-Making_in_Decision-Making\" >Nested Conditional Statements: Decision-Making in Decision-Making<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/dutable.com\/control-flow-in-python-if-elif-else\/#Conclusion_Understanding_Control_Flow_in_Python\" >Conclusion: Understanding Control Flow in Python<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">Decision-making in the world of programming is a basic concept that enables computers to decide on some condition depending on some conditions. Decision making, in contrast to just executing one line of code after another sequentially, allows programs to act in an intelligent way by taking different paths based on the data they are presented with. In this paper we shall discuss if, elif, and else statements that control the flow of a Python program and therefore make it capable of making decisions and reacting to various situations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Control flow is the sequence of execution of the individual statements, instructions, or function calls in a given program. With a perfect command of conditional statements, you will be in a position to create more dynamic and interactive programs that respond to user feedback or environmental factors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most programming applications rely on decision-making. Whether you are creating games, web applications, or data-processing applications, creating the ability to steer the flow of your program according to various conditions is a strong skillset. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_are_Conditional_Statements\"><\/span><strong>What are Conditional Statements?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Conditional statements can enable the program to test a condition (or a series of conditions) and select a route to take based on whether the condition is <strong>True<\/strong> or <strong>False<\/strong>. This form of flow control will give a program the ability not to execute in a single direction but to choose based on the state of variables, user-provided information, or other elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conditional_Statement_Types_in_Python\"><\/span><strong>Conditional Statement Types in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python has three major decision-making options in your code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>if<\/strong><strong> statement<\/strong>: It is the easiest type of decision-making. It checks a condition, and in case it is true, the block of code underneath it will run.<br><\/li>\n\n\n\n<li><strong>elif<\/strong><strong> statement<\/strong>: Abbreviation for \u201celse if,\u201d enables one to check more than one condition. When the former is not true, the next condition is examined by the program, and so on.<br><\/li>\n\n\n\n<li><strong>else<\/strong><strong> statement<\/strong>: It is the statement that helps to define a block of code that will be executed in case all the preceding conditions are not true.<br><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All these structures are significant in ensuring that your program is able to accommodate various inputs and situations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_if_Statement_The_Introduction_to_Decision-Making\"><\/span><strong>The <\/strong><strong>if<\/strong><strong> Statement: The Introduction to Decision-Making<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The most basic and simplest Python decision-making tool is the <strong>if<\/strong> statement. It tests a specified condition and executes the respective block of code when that condition is <strong>True<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax of an <\/strong><strong>if<\/strong><strong> statement<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">if condition:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;# Code block to be executed if condition is True<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">age = 18<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if age &gt;= 18:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You can vote!&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this, the program will verify whether the variable age is equal to or more than 18. In case it is <strong>True<\/strong>, it will show the message, &#8220;You are eligible to vote!&#8221;. When the condition is <strong>False<\/strong>, the program just does nothing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_elif_Statement_Condition_Testing\"><\/span><strong>The <\/strong><strong>elif<\/strong><strong> Statement: Condition Testing<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In some cases, you may wish to test a variety of conditions. In this situation, the <strong>elif<\/strong> statement is involved. It means &#8220;otherwise&#8221; and allows you to verify several conditions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax of <\/strong><strong>elif<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">if condition1:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;# Code block for condition1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">elif condition2:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;# Code block for condition2<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">elif condition3:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;# Code block for condition3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;# Code block when all the conditions are not true<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">age = 20<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if age &lt; 13:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You are a child.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">elif age &lt; 20:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You are a teenager.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You are an adult.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the program initially determines whether the age is below 13. If that is not so, it examines whether age is under 20. In case of failure of both conditions, then the else block is executed, indicating that the individual is an adult.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why use <\/strong><strong>elif<\/strong><strong>?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When you have many conditions, it is easier to deal with them using <strong>elif<\/strong> as opposed to having a scenario in which you may have a series of <strong>if<\/strong> statements that are nested within one another. This maintains the code&#8217;s cleanliness and efficiency.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_else_Statement_The_Fallback_Path\"><\/span><strong>The <\/strong><strong>else<\/strong><strong> Statement: The Fallback Path<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The last catch-all condition that will execute is the <strong>else<\/strong> statement. This block will run when all the conditions of the <strong>if<\/strong> and <strong>elif<\/strong> statements have not been satisfied. It comes in handy when there is no definite condition that is true and you have to deal with the default scenario.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax of <\/strong><strong>else<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">if condition1:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;# Code block for condition1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;# Code block when condition1 is False<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">score = 75<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if score &gt;= 90:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Grade A&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">elif score &gt;= 75:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Grade B&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Grade C&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>else<\/strong> block, in this instance, will run if the score is lower than 75, and this way all the possible scenarios will be considered.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Combinations_of_Multiple_Conditions_and_Logical_Operators\"><\/span><strong>Combinations of Multiple Conditions and Logical Operators<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Logical operators such as <strong>and<\/strong>, <strong>or<\/strong>, and <strong>not<\/strong> can be used to combine several conditions within a single <strong>if<\/strong>, <strong>elif<\/strong>, or <strong>else<\/strong> statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Logical Operators:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>and<\/strong>: Conditional assurance that both conditions must be <strong>True<\/strong> to run the block of code.<br><\/li>\n\n\n\n<li><strong>or<\/strong>: Checks if at least one of the conditions is <strong>True<\/strong> to run the block of code.<br><\/li>\n\n\n\n<li><strong>not<\/strong>: This reverses the result of a condition.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">age = 25<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">has_driving_license = True<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if age &gt;= 18 and has_driving_license:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You can drive a car!&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You cannot drive a car.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the program will print &#8220;You can drive a car!&#8221; if both conditions are fulfilled, i.e., (<strong>age &gt;= 18<\/strong> and <strong>has_driving_license<\/strong>) is <strong>True<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nested_Conditional_Statements_Decision-Making_in_Decision-Making\"><\/span><strong>Nested Conditional Statements: Decision-Making in Decision-Making<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Conditional statements can also be nested, meaning one <strong>if<\/strong> statement can be placed within another <strong>if<\/strong> statement. This is applicable when you are required to assess a more complicated state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">age = 25<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">has_driving_license = True<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">is_car_available = False<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if age &gt;= 18:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;if has_driving_license:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if is_car_available:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You can drive the car.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;The car is not available.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You do not have a driving license.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;You are too young to drive.&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the conditions to be considered are several, and the program continues with the corresponding action in each scenario.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion_Understanding_Control_Flow_in_Python\"><\/span><strong>Conclusion: Understanding Control Flow in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Conditional statements are a powerful tool in programming that allow for decision-making, giving your code the ability to respond to various inputs and situations. By mastering the use of <strong>if<\/strong>, <strong>elif<\/strong>, and <strong>else<\/strong> statements, you&#8217;ll unlock the ability to create more dynamic and interactive Python programs. Whether you&#8217;re building simple applications or complex systems, these statements provide a clear path for directing the flow of your program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining <strong>logical operators<\/strong> and <strong>nested conditionals<\/strong>, you can handle even the most complex decision-making scenarios. The power of decision-making in programming is at your fingertips\u2014ready to be applied to a wide range of projects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember that decision-making in programming is not just about following paths but about enabling your program to react intelligently based on conditions. Keep practicing, and you&#8217;ll soon master the art of control flow in Python!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Decision-making in the world of programming is a basic concept that enables computers to decide on some condition depending on some conditions. Decision making, in contrast to just executing one line of code after another sequentially, allows programs to act in an intelligent way by taking different paths based on the data they are presented&#8230;<\/p>\n","protected":false},"author":1905,"featured_media":399149,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30700],"tags":[31591,31592,31590],"class_list":["post-399148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-control-flow","tag-decision-making-in-python","tag-python-programming"],"_links":{"self":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/399148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/users\/1905"}],"replies":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/comments?post=399148"}],"version-history":[{"count":2,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/399148\/revisions"}],"predecessor-version":[{"id":399369,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/399148\/revisions\/399369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media\/399149"}],"wp:attachment":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media?parent=399148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/categories?post=399148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/tags?post=399148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}