{"id":7153,"date":"2024-03-19T06:43:40","date_gmt":"2024-03-19T06:43:40","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7153"},"modified":"2024-03-19T06:43:40","modified_gmt":"2024-03-19T06:43:40","slug":"python-if-else-statement","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-if-else-statement\/","title":{"rendered":"Python \u2212 if-else Statement"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#types-of-control-flow-in-python\">Types of Control Flow in Python<\/a><\/li><li ><a href=\"#what-is-python-if-statement\">What is Python if statement?<\/a><\/li><li ><a href=\"#what-is-pythons-if-else-statement\">What is Python&#8217;s If-Else Statement?<\/a><\/li><li ><a href=\"#using-python-if-else-statement\">Using Python if-else statement<\/a><\/li><li ><a href=\"#python-if-else-statement-in-a-list-comprehension\">Python if else statement in a List Comprehension<\/a><\/li><li ><a href=\"#what-is-a-needed-if-statement-in-python\">What is a Needed-if statement in Python?<\/a><\/li><li ><a href=\"#what-is-python-if-else-else-ladder\">What is Python if-else-else Ladder?<\/a><\/li><li ><a href=\"#what-is-a-short-hand-if-statement\">What is a Short Hand if  statement?<\/a><\/li><li ><a href=\"#what-is-a-short-hand-if-else-statement\">What is a Short Hand if-else statement?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-if-else-statement-fa-qs\">Python  if-else Statement- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p> In <a href=\"https:\/\/www.skillvertex.com\/blog\/c-programming-language-tutorial\/\" data-type=\"post\" data-id=\"1719\">computer programming,<\/a> the if statement is considered a conditional statement. It will function to run the block of code only when the specific condition is met. This article has listed the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-control-flow\/\" data-type=\"post\" data-id=\"7128\">Python<\/a> -if-else statement.<\/p>\n\n\n\n<p>Hence, the conditional statement in Python language will determine the direction of the <a href=\"https:\/\/www.skillvertex.com\/blog\/program-error-signals\/\" data-type=\"post\" data-id=\"3693\">program <\/a>execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-control-flow-in-python\">Types of Control Flow in Python<\/h2>\n\n\n\n<p>Several types of control flow are given below:<\/p>\n\n\n\n<p>a. The if statement<\/p>\n\n\n\n<p>b. The if-else statement<\/p>\n\n\n\n<p>c.The nested-if statement<\/p>\n\n\n\n<p>d. The if-elif-else ladder<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-if-statement\">What is Python if statement?<\/h2>\n\n\n\n<p>The<a href=\"https:\/\/www.skillvertex.com\/blog\/python-if-statement\/\" data-type=\"post\" data-id=\"7145\"> if statement<\/a> is referred to as the simple decision-making statement. Hence, it can decide whether a certain statement or<a href=\"https:\/\/www.skillvertex.com\/blog\/goto-statement-in-c\/\" data-type=\"post\" data-id=\"2318\"> block of statements <\/a>will be executed.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if condition:\n   # Statements to execute if\n   # condition is true<\/code><\/pre>\n\n\n\n<p>However, this condition after doing the evaluation will come as either true or False whereas, if statements will accept <a href=\"https:\/\/www.skillvertex.com\/blog\/python-booleans\/\" data-type=\"post\" data-id=\"7030\">boolean values<\/a>. The block of statements will run only if the value comes true.<\/p>\n\n\n\n<p>It is known that  <a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\">Python<\/a> will use indentation to find a block. Look at the example provided below to identify the block under the <a href=\"https:\/\/www.skillvertex.com\/blog\/c-if-statement\/\" data-type=\"post\" data-id=\"2265\">if statement.<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if condition:\n   statement1\nstatement2\n# Here if the condition is true, if block \n# will consider only statement1 to be inside \n# its block.<\/code><\/pre>\n\n\n\n<p><strong>Example  of Python if statement<\/strong><\/p>\n\n\n\n<p>The<a href=\"https:\/\/www.skillvertex.com\/blog\/python-interview-questions\/\" data-type=\"post\" data-id=\"3614\"> Python<\/a> if statement will be executed,  only if the <a href=\"https:\/\/www.skillvertex.com\/blog\/conditional-or-ternary-operator-in-c\/\" data-type=\"post\" data-id=\"2262\">condition<\/a> that exists in the if statement came false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example 1: Basic if statement\nx = 10\n\nif x &gt; 5:\n    print(\"x is greater than 5\")<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is greater than 5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-pythons-if-else-statement\">What is Python&#8217;s If-Else Statement?<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.skillvertex.com\/blog\/python-operators\/\" data-type=\"post\" data-id=\"6937\">if-else <\/a>statement will inform us that the condition statement is true and will run the block of statements. Otherwise, it won&#8217;t run the code.<\/p>\n\n\n\n<p>Whereas, it is possible to use the if statement when the condition becomes false. <\/p>\n\n\n\n<p>Syntax of Python If-Else.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition):\n    # Executes this block if\n    # condition is true\nelse:\n    # Executes this block if\n    # condition is false<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-python-if-else-statement\">Using Python if-else statement<\/h2>\n\n\n\n<p>The else statement will follow the block of code and will run when the condition that is present in the if statement is false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example of if-else statement\nage = 18\n\nif age &gt;= 18:\n    print(\"You are eligible to vote.\")\nelse:\n    print(\"You are not eligible to vote yet.\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>You are eligible to vote.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-if-else-statement-in-a-list-comprehension\">Python if else statement in a List Comprehension<\/h2>\n\n\n\n<p>Let us look into the <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-examples\/\" data-type=\"post\" data-id=\"4337\">example<\/a> given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example of if-else statement in a List Comprehension\nnumbers = &#91;1, 2, 3, 4, 5, 6]\n\n# Create a new list where even numbers are squared, and odd numbers are cubed\nresult = &#91;x**2 if x % 2 == 0 else x**3 for x in numbers]\n\nprint(result)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<p>[1, 4, 27, 16, 125, 36]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-needed-if-statement-in-python\">What is a Needed-if statement in Python?<\/h2>\n\n\n\n<p>A nested if is referred to as an if statement that is the target of another statement. A nested if statement refers to the if statement inside another if statement.<\/p>\n\n\n\n<p>Python will add the nest if statements within the if statements<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition1):\n   # Executes when condition1 is true\n   if (condition2): \n      # Executes when condition2 is true\n   # if Block is end here\n# if Block is end here<\/code><\/pre>\n\n\n\n<p><strong>Example of Python Nested if statement<\/strong><\/p>\n\n\n\n<p>The example provided below will illustrate the nested if conditions in the <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-work-with-unicode-in-python\/\" data-type=\"post\" data-id=\"6901\">code<\/a>. Therefore, the conditions will be executed one by one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example of nested if statement\nx = 10\ny = 5\n\nif x &gt; 5:\n    print(\"x is greater than 5\")\n    \n    if y &gt; 2:\n        print(\"y is greater than 2\")\n    else:\n        print(\"y is not greater than 2\")\n\nelse:\n    print(\"x is not greater than 5\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is greater than 5\ny is greater than 2\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-if-else-else-ladder\">What is Python if-else-else Ladder?<\/h2>\n\n\n\n<p>The user can plan among the multiple options. If statements will be run from the top down.<\/p>\n\n\n\n<p>The statements will be run only if the conditions are true.  The rest of the ladder will be bypassed. Otherwise, the final statement will be run.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition):\n    statement\nelif (condition):\n    statement\n.\n.\nelse:\n    statement<\/code><\/pre>\n\n\n\n<p><strong>Example of Python if-else-else ladder<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example of if-else-else ladder\nx = 15\n\nif x &gt; 10:\n    print(\"x is greater than 10\")\nelif x &gt; 5:\n    print(\"x is greater than 5 but not 10\")\nelse:\n    print(\"x is not greater than 5\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is greater than 10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-short-hand-if-statement\">What is a Short Hand if  statement?<\/h2>\n\n\n\n<p>Shorthand will be used when there is only a single statement to be run. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if condition: statement\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Shorthand version using ternary conditional operator\nx = 15\n\nresult = \"x is greater than 10\" if x &gt; 10 else \"x is greater than 5 but not 10\" if x &gt; 5 else \"x is not greater than 5\"\n\nprint(result)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i is less than 15\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-short-hand-if-else-statement\">What is a Short Hand if-else statement?<\/h2>\n\n\n\n<p>This short-hand if-else statement functions to write the if-else statements in a single line.   Hence, only one statement will be necessary for both the if and else blocks.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>statement_when_True if condition else statement_when_False\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Shorthand version using ternary conditional operator\nx = 8\n\nresult = \"x is greater than 5\" if x &gt; 5 else \"x is not greater than 5\"\n\nprint(result)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is greater than 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, the if-else statement in Python provides a straightforward way to introduce decision-making into your code. It allows you to execute different blocks of code based on whether a given condition is true or false. <\/p>\n\n\n\n<p>However, the simplicity of the syntax makes it easy to understand, and it forms the foundation for more complex control flow structures in Python. By using if-else, you can create flexible and readable code that adapts to various scenarios, enhancing the logic and functionality of your Python programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-if-else-statement-fa-qs\">Python  if-else Statement- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707981974807\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the if-else statement in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In Python, if-else helps make decisions in your code. If a condition is true, a specific set of code runs; if false, another set of code runs. It&#8217;s a way to choose between different actions based on whether a condition is met or not, making your code more adaptable and easy to follow.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707981985959\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. Can you write 2 if statements in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Yes, it is possible to write the if statement inside the other if statement.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707981995221\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the syntax of if else if?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. If a condition is true, the code inside the IF statement runs. Otherwise, the code inside the ELSE statement runs.<br \/>If a condition is true, the code inside the IF statement runs. After that, the code inside the ELSE-IF statement is checked.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In computer programming, the if statement is considered a conditional statement. It will function to run the block of code only when the specific condition is met. This article has listed the Python -if-else statement. Hence, the conditional statement in Python language will determine the direction of the program execution. Types of Control Flow in &#8230; <a title=\"Python \u2212 if-else Statement\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-if-else-statement\/\" aria-label=\"More on Python \u2212 if-else Statement\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[907,72,57,866],"class_list":["post-7153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-if-else-statement","tag-programming","tag-python","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7153"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7153\/revisions"}],"predecessor-version":[{"id":8263,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7153\/revisions\/8263"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7155"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}