{"id":3156,"date":"2022-11-04T20:28:30","date_gmt":"2022-11-04T20:28:30","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3156"},"modified":"2022-11-04T20:28:32","modified_gmt":"2022-11-04T20:28:32","slug":"python-if-elif-statements","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-if-elif-statements\/","title":{"rendered":"Python if elif statements"},"content":{"rendered":"<p style=\"text-align: center;\">THE IF ELIF STATEMENTS IN PYTHON<\/p>\n<p>We are now quite familiar with the Python if else statements. We have some expression, and if the expression evaluates to True, then the if block executes, and if the expression evaluates to False, then the else block executes. So here, in the else, we are not checking any condition, but at times, it would be a requirement, to check some condition here as well, and in such situations, we can have the elif keyword.<\/p>\n<h2 style=\"text-align: center;\">Python if elif statements<\/h2>\n<p>Let\u2019s understand this thing with an example, where we are going to take a number as user input, and then we will check whether the number entered by the user, is 0, greater than 0, or less than 0. Let\u2019s have a look at the simple program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3157 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON.jpg\" alt=\"\" width=\"1119\" height=\"201\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON.jpg 1119w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-300x54.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-1024x184.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-768x138.jpg 768w\" sizes=\"auto, (max-width: 1119px) 100vw, 1119px\" \/><\/p>\n<p>As you can see in the above program, we are taking a number as user input, and then we are checking first of all, that whether the number is greater than zero. If the expression evaluates to False, then we are again checking that if the number is less than zero, and if the expression again evaluates to False, then we are printing that the number is zero! Let\u2019s have a look at the output of the above program.<\/p>\n<p>Please enter a number: -29<br \/>\nThe number is less than zero!<\/p>\n<p>As you can see, we tried to input some negative number, making the expression after the first if statement as False, and we moved to the elif statement, where we are again checking the condition, for number to be less than zero. This time, the expression evaluates to True, due to which, we get the output as the number is less than zero! This is simply helpful for us, at times, when we need to check some condition again, when the expression with if evaluates to False.<\/p>\n<p>Let\u2019s consider one more example, so that we become more familiar with the concept. Note that with the elif keyword, we are able to again check some other condition, when the expression from the previous if evaluated to False.<\/p>\n<p>In the below example, we are going to check if the shopkeeper is in profit, or loss, or no profit no loss. For this, we would simply take cost price, and selling price as user inputs. The simple logic behind this is that if the selling price is more than the cost price, then it is profit, and if the selling price is less than the cost price, then it is loss. Let\u2019s have a look at the program &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3158 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-1.jpg\" alt=\"\" width=\"1112\" height=\"244\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-1.jpg 1112w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-1-300x66.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-1-1024x225.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-IF-ELIF-STATEMENTS-IN-PYTHON-1-768x169.jpg 768w\" sizes=\"auto, (max-width: 1112px) 100vw, 1112px\" \/><\/p>\n<p>As you can see in the above program, we are taking the cost price and the selling price as the user input. Now, first of all we are checking that if the selling_price is greater than the cost_price, and if this evaluates to True, we simply print that the person is in profit, with the amount. Now, if the expression written with the if statement evaluates to False, we are going into the elif, and again checking another condition, that if the cost_price is greater than the selling_price, which is the loss condition. If this condition is also False, then we are printing that there is no profit no loss. Let\u2019s have a look at the output &#8211;<\/p>\n<p>Please enter the cost price:1200<br \/>\nPlease enter the selling price:1850<br \/>\nYou are in profit! amount = 650.0<\/p>\n<p>As you can see from the output, we enter the cost price, and the selling price, and just get the output accordingly. So, in the situations, when we are required to check some other condition, if the previous condition from the if was False, we can simply make use of the if elif statements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>THE IF ELIF STATEMENTS IN PYTHON We are now quite familiar with the Python if else statements. We have some expression, and if the expression evaluates to True, then the if block executes, and if the expression evaluates to False, then the else block executes. So here, in the else, we are not checking any [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3156","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3156"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3156\/revisions"}],"predecessor-version":[{"id":3968,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3156\/revisions\/3968"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}