{"id":27783,"date":"2022-02-27T17:29:24","date_gmt":"2022-02-27T17:29:24","guid":{"rendered":"https:\/\/www.askpython.com\/?p=27783"},"modified":"2022-02-27T17:29:25","modified_gmt":"2022-02-27T17:29:25","slug":"conditionals-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/conditionals-in-python","title":{"rendered":"Conditionals in Python &#8211; A Quick Guide"},"content":{"rendered":"\n<p>The world is much more complex than it seems. That is why in some cases we need to put conditions to resolve the complexity. With the help of conditionals, you can skip over some complexities or run a series of statements very quickly. You can also choose between statements and execute a program. For instance, in everyday life, we use a lot of conditionals such as if it snows today, I will not go to the market or else I will go. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conditionals in Python<\/h2>\n\n\n\n<p>In this tutorial, we are going to learn how we can use conditionals in Python. So let\u2019s get started!<\/p>\n\n\n\n<p>Let\u2019s look at the conditional statements one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">If Statement<\/h3>\n\n\n\n<p>We will start with the very basic \u201cif\u201d statement. The syntax of the if statement is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif(condition):\n             (statement)\n<\/pre><\/div>\n\n\n<p><strong>The syntax means that if the condition satisfies, the statement will be executed, or else it will be skipped.<\/strong><\/p>\n\n\n\n<p><strong>For example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nA=1\nB=3\n\nif(A&lt;B):\n        print(\u2018A is true\u2019)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nA is true\n<\/pre><\/div>\n\n\n<p><strong>Now, if we want to execute multiple statements together, the if statement will look something like this:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif(condition):\n\n               &lt;statement&gt;\n               &lt;statement&gt;\n                &lt;statement&gt;\n                 \u2026\u2026\u2026\u2026\u2026\n\n&lt; Other statements&gt;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Else Statement<\/h3>\n\n\n\n<p>Let\u2019s now see what the else condition looks like. In this case, we will combine the if statement with the else condition.<\/p>\n\n\n\n<p><strong>The basic syntax of the else statement:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif(condition):\n                &lt;statement&gt;\n\nelse:\n              &lt;statement&gt;\n\n\n<\/pre><\/div>\n\n\n<p><strong>For example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx=200\n\nIf x&lt;300:\n         print(\u2018200 is smaller\u2019)\n\nelse:\n        print(\u2018200 is bigger\u2019)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n200 is smaller\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Elif Statement<\/h3>\n\n\n\n<p>The elif statement is another conditional statement in Python that helps to check for multiple conditions that are true. Elif statement is almost like the if statement with the exception that there can only be one else statement whereas there can be multiple elif statements.<\/p>\n\n\n\n<p><strong>Following is the syntax of the elif statement.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif(condition1):\n              &lt;statement&gt;\n\nelif(condition2):\n                &lt;statement&gt;\nelif(condition3):\n                  &lt;statement&gt;\nelse:\n          &lt;statement&gt;\n\n<\/pre><\/div>\n\n\n<p><strong>Let\u2019s take a look at the following example.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprice=180\n\nif price&gt;100:\n                print(\u201cPrice is greater than 100\u201d)\n\nelif price==100:\n                 print(\u201cPrice is 100\u201d)\n\nelif price&lt;100:\n                 print(\u201cPrice is less than 100\u201d)\n\nelse :\n                print(\u201cInvalid Price\u201d)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPrice is greater than 100\n<\/pre><\/div>\n\n\n<p><strong>Note:<\/strong> All the statements should maintain their indentation level or else it will throw an indentation error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion:<\/h2>\n\n\n\n<p>In this article, we have learned about the conditionals or control structures in Python. These control structures determine the execution of a program. You can use loops along with these control structures to execute different programs. Hence, it is very important for you to know the conditional statements in Python.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The world is much more complex than it seems. That is why in some cases we need to put conditions to resolve the complexity. With the help of conditionals, you can skip over some complexities or run a series of statements very quickly. You can also choose between statements and execute a program. For instance, [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":27785,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-27783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/27783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=27783"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/27783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/27785"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=27783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=27783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=27783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}