{"id":17205,"date":"2023-02-27T22:06:10","date_gmt":"2023-02-27T16:36:10","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=17205"},"modified":"2023-02-27T22:06:13","modified_gmt":"2023-02-27T16:36:13","slug":"python-if-else","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/python-if-else\/","title":{"rendered":"Python if\u2026else Conditional Statement (With Examples)"},"content":{"rendered":"\n<p>We would have seen most protagonists utter the words \u2018No ifs &#038; buts\u2026..\u2019 in the movies, to get things done in the only way they want. It\u2019s all fine in the movies but in reality, things don\u2019t work that way! There shall be a truckload of \u2018ifs\u2019 &#038; \u2018buts\u2019 thrown at your face in every real-life situation you face. That leaves programming as no exception. <\/p>\n\n\n\n<p>But the thing about programming is that it assists you in navigating through these \u2018ifs\u2019 (They don\u2019t seem to like \u2018buts\u2019 I suppose). In this article, we shall look into the <em>if-else&nbsp;<\/em>statement in Python and its variations alongside examples. Let\u2019s get started with some rudimentary concepts such as knowing what exactly the <em>if-else&nbsp;<\/em>statement is destined to do.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python if\u2026else Statement<\/h2>\n\n\n\n<p>The <em>if-else&nbsp;<\/em>statement contains a condition against which the input data would be compared to find whether the condition is met or not. Once the condition is met, the line of codes following the \u2018if\u2019&nbsp;shall be executed, whilst in the event of not being met, those following the <em>\u2018else\u2019&nbsp;<\/em>shall be run. The condition generally is constructed using logical operators viz. &lt;, &gt;, !=, ==, &lt;= or &gt;=. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Flowchart of Python if\u2026else Statement<\/h3>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"590\" height=\"391\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Logic-of-if-else-statement.png\" alt=\"Logic Of If Else Statement\" class=\"wp-image-17207\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Logic-of-if-else-statement.png 590w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Logic-of-if-else-statement-300x199.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><figcaption class=\"wp-element-caption\">The logic of the <em>if-else<\/em>&nbsp;statement<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Forms of Python if\u2026else Statement<\/h2>\n\n\n\n<p>Like a good old villain, the <em>if\u2026else\u00a0<\/em>statement offers a choice for the course of the program to pick up &#038; this essentially has helped it gain its alias as the <em>selection <\/em>statement. Let us now get on with the different variants of the <em>if\u2026else\u00a0<\/em>statement that exists within Python.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Simple <em>if <\/em>statement<\/strong><\/li>\n\n\n\n<li><strong><em>if\u2026else <\/em>statement<\/strong><\/li>\n\n\n\n<li><strong>Nested <em>if\u2026else\u00a0<\/em>statement<\/strong><\/li>\n\n\n\n<li><strong><em>if&#8230;elif&#8230;else<\/em><\/strong> <strong>statement<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python if\u00a0Statement<\/strong><\/h3>\n\n\n\n<p>Just as the name indicates this statement is decluttered from the <em>else\u00a0<\/em>portion &#038; only contains the <em>if\u00a0<\/em>part. So, when the condition is not met, the line of codes following the <em>if\u00a0<\/em>shall be skipped and the program is run with the subsequent lines of codes following the <em>if\u00a0<\/em>statement.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nY = 1990\nif Y&lt;=1999 and Y&gt;=1990:\n    print(&#039;Hello there, you 90s kid&#039;)\n<\/pre><\/div>\n\n\n<p>It could be seen that following the <em>if&nbsp;<\/em>there seems to be an indentation. This is a prerequisite that ought to be followed while constructing any variant of the <em>if&nbsp;<\/em>statement. Following is the result when the above code is run.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"975\" height=\"123\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-simple-if-statement.png\" alt=\"Result Of Simple If Statement\" class=\"wp-image-17214\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-simple-if-statement.png 975w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-simple-if-statement-300x38.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-simple-if-statement-768x97.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><figcaption class=\"wp-element-caption\">Result of simple<em> if<\/em>&nbsp;statement<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python if\u2026else<em>\u00a0<\/em>Statement<\/strong><\/h3>\n\n\n\n<p>This statement has both plan A &#038; plan B for the program to choose from depending on whether it meets the specified condition or not. The following example explains it better.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nY = 2006\nif Y&lt;=1999 and Y&gt;=1990:\n    print(&quot;Hello there, you &#039;90s kid&quot;)\nelse:\n   print(&quot;It&#039;s sad you weren&#039;t born in the &#039;90s&quot; )\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"975\" height=\"151\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-the-if-else-statement.png\" alt=\"Result Of The If Else Statement\" class=\"wp-image-17217\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-the-if-else-statement.png 975w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-the-if-else-statement-300x46.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-of-the-if-else-statement-768x119.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><figcaption class=\"wp-element-caption\">Result of the <em>if-else<\/em>&nbsp;statement<\/figcaption><\/figure>\n\n\n\n<p>It could be observed since the input falls out of the range specified in the <em>if\u00a0<\/em>condition, the <em>else\u00a0<\/em>block is executed. The same can also be done using the below code in a shorter form.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nY = 2006\nprint(&quot;Hello there, you &#039;90s kid&quot;) if Y&lt;=1999 and Y&gt;=1990 else print(&quot;It&#039;s sad you weren&#039;t born in the &#039;90s&quot; )\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"975\" height=\"116\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Short-form-of-if-else-statement.png\" alt=\"Short Form Of If Else Statement\" class=\"wp-image-17219\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Short-form-of-if-else-statement.png 975w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Short-form-of-if-else-statement-300x36.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Short-form-of-if-else-statement-768x91.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><figcaption class=\"wp-element-caption\">Short form of<em> if-else<\/em>&nbsp;statement<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python Nested if\u2026else<em>\u00a0<\/em>Statement<\/strong><\/h3>\n\n\n\n<p>At times it is not sufficient to get things done with one else block. This necessity paved way for the construction of nested <em>if\u2026else<\/em> statements. The example below shows how a typical nested <em>if\u2026else<\/em> statement shall work.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nY = 1986\nif Y&gt;1999:\n    print(&quot;It&#039;s sad you weren&#039;t born in the &#039;90s&quot;)\n   if Y&gt;1990:\n        print(&quot;Hello there, you &#039;90s kid&quot;)\nelse:\n   print(&quot;How was life in the &#039;80s?&quot; )\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"975\" height=\"206\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Results-of-nested-if-else-statement.png\" alt=\"Result of Nested If Else Statement\" class=\"wp-image-17222\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Results-of-nested-if-else-statement.png 975w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Results-of-nested-if-else-statement-300x63.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Results-of-nested-if-else-statement-768x162.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><figcaption class=\"wp-element-caption\">Results of nested <em>if-else<\/em>&nbsp;statement<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Python if\u2026elif\u2026else Statement<\/h3>\n\n\n\n<p>This is Python\u2019s gift to those who are sick &#038; tired of typing multiple <em>else\u2019s\u00a0<\/em>and <em>ifs.\u00a0<\/em>The if\u2026elif\u2026else statement effectively combines both the <em>else\u00a0<\/em>&amp; <em>if\u00a0<\/em>statements together so that it is kept short and not repeated often. It serves the same purpose as the nested <em>if\u2026<\/em>else<em>\u00a0<\/em>statement albeit in a compact manner. Let\u2019s have a look at an example.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"975\" height=\"211\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-for-elif-statement.png\" alt=\"Result For Elif Statement\" class=\"wp-image-17223\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-for-elif-statement.png 975w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-for-elif-statement-300x65.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Result-for-elif-statement-768x166.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><figcaption class=\"wp-element-caption\">The result for <em>elif<\/em>\u00a0statement<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Hope this article has thrown light upon the different variants of the<em> if\u2026else\u00a0<\/em>statement that exists within Python. Here\u2019s another article emphasising the <a href=\"https:\/\/codeforgeek.com\/python-language-for-quant-traders\/\" data-type=\"URL\" data-id=\"https:\/\/codeforgeek.com\/python-language-for-quant-traders\/\" target=\"_blank\" rel=\"noreferrer noopener\">importance of Python for traders<\/a>. There are numerous other interesting and equally informative articles in <em><a href=\"https:\/\/codeforgeek.com\/\" data-type=\"URL\" data-id=\"https:\/\/codeforgeek.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">CodeforGeek <\/a><\/em>that would be of great help for you to gain valuable insights. Until then, <em>ciao!<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reference<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/tutorial\/controlflow.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/docs.python.org\/3\/tutorial\/controlflow.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We would have seen most protagonists utter the words \u2018No ifs &#038; buts\u2026..\u2019 in the movies, to get things done in the only way they want. It\u2019s all fine in the movies but in reality, things don\u2019t work that way! There shall be a truckload of \u2018ifs\u2019 &#038; \u2018buts\u2019 thrown at your face in every [&hellip;]<\/p>\n","protected":false},"author":90,"featured_media":17227,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-17205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/if-else-statement.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/if-else-statement-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/if-else-statement-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/if-else-statement-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/if-else-statement-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/if-else-statement.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/if-else-statement.png",1200,800,false]},"uagb_author_info":{"display_name":"Arulius Savio","author_link":"https:\/\/codeforgeek.com\/author\/arulius\/"},"uagb_comment_info":0,"uagb_excerpt":"We would have seen most protagonists utter the words \u2018No ifs &#038; buts\u2026..\u2019 in the movies, to get things done in the only way they want. It\u2019s all fine in the movies but in reality, things don\u2019t work that way! There shall be a truckload of \u2018ifs\u2019 &#038; \u2018buts\u2019 thrown at your face in every&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/17205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=17205"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/17205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/17227"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=17205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=17205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=17205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}