{"id":19844,"date":"2021-07-06T14:40:18","date_gmt":"2021-07-06T14:40:18","guid":{"rendered":"https:\/\/www.askpython.com\/?p=19844"},"modified":"2021-07-09T19:57:24","modified_gmt":"2021-07-09T19:57:24","slug":"multi-conditional-if-statement","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/multi-conditional-if-statement","title":{"rendered":"Multi-Conditional If Statement in Python [Explained]"},"content":{"rendered":"\n<p>Hello learner! Today, we will understand how to implement multiple conditions in the &#8216; <a href=\"https:\/\/www.askpython.com\/course\/python-course-if-else-statement\" data-type=\"post\" data-id=\"18446\">if statement<\/a>&#8216;. By the end of this article, you will learn different cases of implementing the if-else condition. So let&#8217;s begin.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is the if statement in Python?<\/h2>\n\n\n\n<p>&#8216;If&#8217; statement is a conditional statement that is used to check whether a particular expression is true or not. The program control first checks the condition written with &#8216; if &#8216; and if the condition proves to be true, the if block is executed. Otherwise, the program control goes to the else block and executes it.<\/p>\n\n\n\n<p><strong>SYNTAX:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif(condition) :\n    code block 1  \nelse :\n    code block 2 \n<\/pre><\/div>\n\n\n<p>Code block 1 executes if the condition is satisfied. If not, code block 2 is executed.<\/p>\n\n\n\n<p>We all generally use a basic if statement, i.e., if statement with only one condition. This is used when we want to compare a variable with another variable or we want to check if a variable is true or not. for eg:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnum1 = int(input(&quot;Enter a number:&quot;)\n\nif( num1 % 2 == 0 ):\n    print(&quot;The number is even!&quot;)\nelse:\n    print(&quot;The number is odd!&quot;)\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEnter a number: 37\nThe number is odd!\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to use multiple conditions in an if statement?<\/h2>\n\n\n\n<p>Now, we will see how to use multiple conditions in an if statement. The syntax and example are explained below:<\/p>\n\n\n\n<p><strong>SYNTAX<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif ((cond1) AND\/OR (cond2)) :\n    code block 1\nelse :\n    code block 2\n<\/pre><\/div>\n\n\n<p>The multiple conditions can be used using <strong>AND <\/strong>or <strong>OR <\/strong> or <strong>BOTH <\/strong>in the single if statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Multiple conditions using &#8216;and&#8217;<\/h3>\n\n\n\n<p>AND condition is used when you want all the conditions to be satisfied. Take a look at the below example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nage = int (input (&quot; What is your age? &quot;))\nexp = int (input (&quot; Enter your work experience in years: &quot;))\n\nif (age &gt; 30 and age &lt; 60) and (exp &gt; 4):\n    Print (&quot; You are hired! &quot;)\nelse:\n    Print (&quot; Sorry! you are not eligible :( &quot;)\n<\/pre><\/div>\n\n\n<p>The above code uses AND condition which means every condition written must be true. Age must be between 30 to 60 and experience should be more than 4 years, then only you&#8217;ll be hired.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nOutput:\nWhat is your age?  32\nEnter your work experience in years: 6\nYou are hired!\n\nWhat is your age? 28\nEnter your work experience in years: 5\nSorry! you are not eligible :(  \n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Multiple conditions using &#8216;or&#8217;<\/h3>\n\n\n\n<p>OR condition is used when you want at least one condition to be satisfied. Let&#8217;s take a look at an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnum1 = int(input(&quot;Enter any number : &quot;))\nrem = num1 % 10\n\nif (rem == 0 ) or ( rem == 5 ) :\n    print( &quot;{} is divisible by 5 &quot;.format(num1))\nelse :\n    print(&quot; {} is not divisible by 5&quot;.format(num1))\n<\/pre><\/div>\n\n\n<p>The above code checks whether the entered number is divisible by 5 or not. For that, it first finds out the last digit of the number by finding out the remainder when divided by 10 (Using&nbsp;modulo 10) and if the remainder is equal to 0 or 5, it prints that the number is divisible by 5. If not, it prints that the number is not divisible by 5.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nOUTPUT :\n\nEnter any number : 90\n90 is divisible by 5 \n\nEnter any number : 27\n27 is not divisible by 5 \n\nEnter any number : 15\n15 is divisible by 5 \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So, this was how we can use multiple conditions in an if statement. Do try out different combinations of if-else conditions and feel free to drop questions below if any!<\/p>\n\n\n\n<p>Thank you! \ud83d\ude42<\/p>\n\n\n\n<p>  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello learner! Today, we will understand how to implement multiple conditions in the &#8216; if statement&#8216;. By the end of this article, you will learn different cases of implementing the if-else condition. So let&#8217;s begin. What is the if statement in Python? &#8216;If&#8217; statement is a conditional statement that is used to check whether a [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":19847,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-19844","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\/19844","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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=19844"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19844\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/19847"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=19844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=19844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=19844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}