{"id":7212,"date":"2023-01-24T02:29:11","date_gmt":"2023-01-23T20:59:11","guid":{"rendered":"https:\/\/tutorpython.com\/?p=7212"},"modified":"2023-10-19T15:08:42","modified_gmt":"2023-10-19T09:38:42","slug":"if-else-python","status":"publish","type":"post","link":"https:\/\/tutorpython.com\/if-else-python","title":{"rendered":"If else Python condition statement"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">In This Article<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tutorpython.com\/if-else-python\/#If_Statements_in_Python\" >If Statements in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tutorpython.com\/if-else-python\/#Python_if_else_statement\" >Python if else statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tutorpython.com\/if-else-python\/#If-elif-else_Statements_in_Python\" >If-elif-else Statements in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tutorpython.com\/if-else-python\/#Shorthand_if_else_python_or_python_one_line_if_else\" >Shorthand if else python or python one line if else<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tutorpython.com\/if-else-python\/#Multiple_if_else_one_line_python\" >Multiple if else one line python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tutorpython.com\/if-else-python\/#Nested_if_else_in_python\" >Nested if else in python<\/a><\/li><\/ul><\/nav><\/div>\n<p>The If-else condition statement is a fundamental part of programming and is used to control the flow of a program. In Python, they are used to make decisions based on certain conditions. The syntax is simple and easy to understand, making it a great tool for beginners to learn. In this article, we will learn about the if-else statements in Python and use some examples to help us understand how to use them in our Python code.<\/p>\n<p>The<a href=\"https:\/\/tutorpython.com\/tutorial\/if-else-python\" target=\"_blank\" rel=\"noopener\"> if-else statements in Python<\/a> are of the following types:<\/p>\n<ol>\n<li><strong>if<\/strong> statement<\/li>\n<li><strong>if-else<\/strong> statement<\/li>\n<li><strong>if-elif-else<\/strong> statement<\/li>\n<\/ol>\n<p>Moreover, we will also be learning about the nested if statements in Python.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"If_Statements_in_Python\"><\/span>If Statements in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An if statement in Python is used to control the flow of a program based on certain conditions. It allows us to execute different code based on whether a certain condition is true or false.<\/p>\n<p>The basic syntax of Python if statement is as follows-<\/p>\n<pre><code class=\"language-python\">if condition:\r\n    # code to execute if condition is true<\/code><\/pre>\n<p>We can use this syntax to write an example for if statements.<\/p>\n<p>Example of if statement in Python-<\/p>\n<pre><code class=\"language-python\">x = 15\r\nif x &gt; 10:\r\n    print(\"x is greater than 10\")\r\n<\/code><\/pre>\n<p>Here, the condition <strong>x &gt; 10<\/strong> is true, so the code inside the if block is executed and the output is &#8220;<strong>x is greater than 10<\/strong>&#8220;.<\/p>\n<p>You can also use <a href=\"https:\/\/tutorpython.com\/tutorial\/types-of-operators-in-python\" target=\"_blank\" rel=\"noopener\">logical operators<\/a> as well as comparison operators like <strong>and<\/strong>, <strong>or<\/strong>, and <strong>not<\/strong>,<strong>==<\/strong>,<strong> !=<\/strong>,<strong> &lt;<\/strong>,<strong> &gt;<\/strong>,<strong> &lt;=<\/strong>, and<strong> &gt;=<\/strong>.<\/p>\n<p>Example of Python if statement with operators included-<\/p>\n<pre><code class=\"language-python\">x = 5\r\ny = 10\r\nif x &gt; 2 and y &lt; 15:\r\n    print(\"x is greater than 2 and y is less than 15\")\r\n<\/code><\/pre>\n<p>Here, the condition <strong>x &gt; 2<\/strong> and <strong>y &lt; 15<\/strong> is true, so the code inside the if block is executed and the output is &#8220;<strong>x is greater than 2 and y is less than 15<\/strong>&#8220;.<\/p>\n<p>It is also possible to use if statements with <a href=\"https:\/\/tutorpython.com\/tutorial\/list-in-python\" target=\"_blank\" rel=\"noopener\">lists<\/a>, <a href=\"https:\/\/tutorpython.com\/tutorial\/strings-in-python\" target=\"_blank\" rel=\"noopener\">strings<\/a>, or any other iterable <a href=\"https:\/\/tutorpython.com\/tutorial\/data-types-in-python\" target=\"_blank\" rel=\"noopener\">data types<\/a> by using the <strong>in<\/strong> keyword.<\/p>\n<p>Example of if statement in a Python list-<\/p>\n<pre><code class=\"language-python\">fruits = [\"apple\", \"banana\", \"orange\"]\r\nif \"banana\" in fruits:\r\n    print(\"banana is in the list\")\r\n<\/code><\/pre>\n<p>Here, the condition <strong>&#8220;banana&#8221;<\/strong> in fruits is true, so the code inside the if block is executed and the output is &#8220;<strong>banana is in the list<\/strong>&#8220;.<\/p>\n<p>By using if statements along with logical and comparison operators, you can create powerful and flexible control structures for your Python programs.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Python_if_else_statement\"><\/span>Python if else statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If-else statements in Python are used to make decisions in your program based on certain conditions. They allow you to control the flow of your program and execute different codes based on whether a condition is true or false.<\/p>\n<p>The syntax for an if-else statement in Python is as follows:<\/p>\n<pre><code class=\"language-python\">if condition:\r\n    # code to execute if condition is true\r\nelse:\r\n    # code to execute if condition is false\r\n<\/code><\/pre>\n<p>We can use this syntax to write an example for if-else statements.<\/p>\n<p>Example of Python if else with the help of a simple program-<\/p>\n<pre><code class=\"language-python\">x = 15\r\nif x &gt; 10:\r\n    print(\"x is greater than 10\")\r\nelse:\r\n    print(\"x is not greater than 10\")\r\n<\/code><\/pre>\n<p>Here, the condition <strong>x &gt; 10<\/strong> is true, so the code inside the if block is executed and the output is &#8220;<strong>x is greater than 10<\/strong>&#8220;.<\/p>\n<p>We can also use if-else statements with logical operators like <strong>and<\/strong>, <strong>or<\/strong>, and <strong>not<\/strong>.<\/p>\n<p>Example of if else block including operators-<\/p>\n<pre><code class=\"language-python\">x = 15\r\ny = 20\r\nif x &gt; 10 and y &gt; 15:\r\n    print(\"x is greater than 10 and y is greater than 15\")\r\nelse:\r\n    print(\"condition is false\")\r\n<\/code><\/pre>\n<p>Here, the condition <strong>x &gt; 10 and y &gt; 15<\/strong> is true, so the code inside the if block is executed and the output is &#8220;<strong>x is greater than 10 and y is greater than 15<\/strong>&#8220;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"If-elif-else_Statements_in_Python\"><\/span>If-elif-else Statements in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We use the <strong>elif<\/strong> (short for &#8220;else if&#8221;) to check multiple conditions inside an if-else statement.<\/p>\n<p>Example of Python <strong>elif<\/strong> statement-<\/p>\n<pre><code class=\"language-python\">x = 15\r\nif x &gt; 20:\r\n    print(\"x is greater than 20\")\r\nelif x &gt; 10:\r\n    print(\"x is greater than 10 but not greater than 20\")\r\nelse:\r\n    print(\"x is not greater than 10\")\r\n<\/code><\/pre>\n<p>Here, the first condition <strong>x &gt; 20<\/strong> is false, so the program checks the next condition <strong>x &gt; 10<\/strong>, which is true, so the code inside the <strong>elif<\/strong> block is executed and the output is &#8220;<strong>x is greater than 10 but not greater than 20<\/strong>&#8220;.<\/p>\n<p><strong>Note:<\/strong> It is important to keep in mind that the code inside the if and else block should be indented by a tab or 4 spaces, otherwise, it will raise an indentation error.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Shorthand_if_else_python_or_python_one_line_if_else\"><\/span>Shorthand if else python or python one line if else<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Python, the <a href=\"https:\/\/tutorpython.com\/tutorial\/types-of-operators-in-python\" target=\"_blank\" rel=\"noopener\">ternary operator<\/a> can be used to write a one-line if-else statement.<\/p>\n<p>The basic syntax of the operator is as follows:<\/p>\n<pre><code class=\"language-python\">value_if_true if condition else value_if_false\r\n<\/code><\/pre>\n<p>Here, it evaluates to <strong>value_if_true<\/strong> if the <strong>condition<\/strong> is true and <strong>value_if_false<\/strong> if the <strong>condition<\/strong> is false.<\/p>\n<p>Example of ternary operator with if else in Python-<\/p>\n<pre><code class=\"language-python\">x = 5\r\nprint(\"x is greater than 3\") if x &gt; 3 else print(\"x is less than or equal to 3\")\r\n# output: x is greater than 3<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Multiple_if_else_one_line_python\"><\/span>Multiple if else one line python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Python, it is possible to write multiple if-else statements in one line using the ternary operator, by chaining multiple ternary operators together.<\/p>\n<p>The basic syntax for chaining multiple ternary operators is as follows:<\/p>\n<pre><code class=\"language-python\">value_if_condition_1 if condition_1 else value_if_condition_2 if condition_2 else value_if_condition_3\r\n<\/code><\/pre>\n<p>Here, it will check each condition in order and return the corresponding value if the condition is true. If none of the conditions are true, the last value in the chain will be returned.<\/p>\n<p>Example of Python multiple if else :<\/p>\n<pre><code class=\"language-python\">x = 5\r\nprint(\"x is 1\") if x == 1 else print(\"x is 2\") if x == 2 else print(\"x is not 1 or 2\")\r\n# output: x is not 1 or 2\r\n<\/code><\/pre>\n<p><strong>Note:<\/strong> As the chain of the ternary operator becomes longer and more complex, it can become harder to read and understand. It is generally recommended to use multiple if-else statements in cases where the conditions are complex or multiple conditions are to be checked.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Nested_if_else_in_python\"><\/span>Nested if else in python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Python, it is possible to write nested if-else statements, where one or more if-else statements are placed inside another if-else statement.<\/p>\n<p>This allows for more complex decision-making, where multiple conditions are evaluated in succession.<\/p>\n<p>The basic syntax for a nested if-else statement is as follows:<\/p>\n<pre><code class=\"language-python\">if condition_1:\r\n    # code to execute if condition_1 is true\r\n    if condition_2:\r\n        # code to execute if condition_2 is true\r\n    else:\r\n        # code to execute if condition_2 is false\r\nelse:\r\n    # code to execute if condition_1 is false\r\n<\/code><\/pre>\n<p>Example using the above-shown format:<\/p>\n<pre><code class=\"language-python\">x = 5\r\ny = 8\r\nif x &gt; y:\r\n    print(\"x is greater than y\")\r\nelse:\r\n    if x &lt; y:\r\n        print(\"x is less than y\")\r\n    else:\r\n        print(\"x is equal to y\")\r\n# output: x is less than y\r\n<\/code><\/pre>\n<p>Here, the first if-else statement checks if <strong>x<\/strong> is greater than <strong>y<\/strong>. If it is then the message &#8220;<strong>x is greater than y<\/strong>&#8221; is printed. If it is not, the second if-else statement checks if <strong>x<\/strong> is less than <strong>y<\/strong>. If it is, the message &#8220;<strong>x is less than y<\/strong>&#8221; is printed. If it is not, the message &#8220;<strong>x is equal to y<\/strong>&#8221; is printed.<\/p>\n<p>It is also possible to nest multiple if-else statements within an else block, which allows for even more complex decision-making.<\/p>\n<p>However, it is important to keep in mind that as the number of nested if-else statements increases, the code can become more complex and harder to understand. So it is important to use nested if-else statements judiciously and to keep the code organized and readable.<\/p>\n<h2>Conclusion<\/h2>\n<p>In conclusion, if-else statements in Python are a powerful concept for controlling the flow of our program and making decisions based on certain conditions.<\/p>\n<p>By using <strong>if<\/strong>, <strong>elif,<\/strong> and <strong>else<\/strong> statements, we can check multiple conditions and execute different codes based on whether those conditions are true or false.<\/p>\n<p>If-else statements are very useful in a wide range of programming situations, including user input validation, data manipulation, and creating control structures for your code.<\/p>\n<p>They can also be used with various data types, such as <a href=\"https:\/\/tutorpython.com\/tutorial\/strings-in-python\" target=\"_blank\" rel=\"noopener\">strings<\/a>, lists, and <a href=\"https:\/\/tutorpython.com\/tutorial\/dictionaries-in-python\" target=\"_blank\" rel=\"noopener\">dictionaries<\/a>, making them a versatile and essential part of any Python programmer&#8217;s toolkit.<\/p>\n<p>Hope you liked the tutorial, if you are interested in <a href=\"https:\/\/tutorpython.com\/\" target=\"_blank\" rel=\"noopener\">learning Python<\/a> and <a href=\"https:\/\/tutorpython.com\/\" target=\"_blank\" rel=\"noopener\">need a Python tutor<\/a> then you can <a href=\"https:\/\/tutorpython.com\/contact\" target=\"_blank\" rel=\"noopener\">contact us<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The If-else condition statement is a fundamental part of programming and is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-7212","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"acf":[],"_links":{"self":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/7212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/comments?post=7212"}],"version-history":[{"count":17,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/7212\/revisions"}],"predecessor-version":[{"id":8801,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/7212\/revisions\/8801"}],"wp:attachment":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/media?parent=7212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/categories?post=7212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/tags?post=7212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}