{"id":124,"date":"2023-06-02T19:23:26","date_gmt":"2023-06-02T19:23:26","guid":{"rendered":"http:\/\/python.garden\/index.php\/2023\/06\/02\/conditionals-if-else-elif\/"},"modified":"2023-06-02T19:39:02","modified_gmt":"2023-06-02T19:39:02","slug":"conditionals-if-else-elif","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/06\/02\/conditionals-if-else-elif\/","title":{"rendered":"Chapter 2.1: Conditionals &#8211; If, Else, Elif"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this chapter, we will discuss how to control the flow of our Python programs using conditional statements: <code>if<\/code>, <code>else<\/code>, and <code>elif<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>if<\/code> Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>if<\/code> statement is the most basic type of conditional statement in Python. It checks a condition and executes a block of code only if the condition is true.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = 5\n\nif x &gt; 0:\n    print(&quot;x is positive&quot;)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, <code>x &gt; 0<\/code> is the condition. Since <code>x<\/code> is <code>5<\/code>, and <code>5<\/code> is indeed greater than <code>0<\/code>, the print statement is executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note the use of indentation to define the block of code to be executed if the condition is true. Python uses indentation (typically four spaces or one tab) to define blocks of code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>else<\/code> Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>else<\/code> statement follows an <code>if<\/code> statement and defines a block of code to be executed if the condition in the <code>if<\/code> statement is false.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = -5\n\nif x &gt; 0:\n    print(&quot;x is positive&quot;)\nelse:\n    print(&quot;x is not positive&quot;)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, <code>x<\/code> is <code>-5<\/code>, so <code>x &gt; 0<\/code> is <code>False<\/code>. The <code>if<\/code> block is skipped and the <code>else<\/code> block is executed instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>elif<\/code> Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>elif<\/code> is a contraction of &#8220;else if&#8221;. It allows us to check multiple conditions and execute a specific block of code as soon as one of the conditions is true.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = 0\n\nif x &gt; 0:\n    print(&quot;x is positive&quot;)\nelif x &lt; 0:\n    print(&quot;x is negative&quot;)\nelse:\n    print(&quot;x is zero&quot;)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, <code>x<\/code> is <code>0<\/code>, so <code>x &gt; 0<\/code> is <code>False<\/code> and <code>x &lt; 0<\/code> is also <code>False<\/code>. Both the <code>if<\/code> and <code>elif<\/code> blocks are skipped, and the <code>else<\/code> block is executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember, the <code>elif<\/code> and <code>else<\/code> statements are optional. An <code>if<\/code> statement can exist on its own or be followed by any number of <code>elif<\/code> statements. The <code>else<\/code> statement, if used, must come last.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">With <code>if<\/code>, <code>else<\/code>, and <code>elif<\/code>, you can make your Python programs make decisions and behave differently based on various conditions. This is a foundational concept in any programming language and critical in mastering Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter, we will discuss how to control the flow of our Python programs using conditional statements: if, else, and elif. The if Statement The if statement is the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[115,99,116,94],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-conditionals","category-control-structures","category-if-else-elif","category-introduction-to-python-2"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}