{"id":7145,"date":"2024-03-19T06:43:28","date_gmt":"2024-03-19T06:43:28","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7145"},"modified":"2024-03-19T06:43:28","modified_gmt":"2024-03-19T06:43:28","slug":"python-if-statement","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-if-statement\/","title":{"rendered":"Python if &#8230; 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=\"#what-are-python-conditions-and-if-statements\">What are Python Conditions and if statements<\/a><\/li><li ><a href=\"#what-is-indentation\">What is Indentation?<\/a><\/li><li ><a href=\"#what-is-elif\">What is Elif?<\/a><\/li><li ><a href=\"#what-is-else\">What is Else ?<\/a><\/li><li ><a href=\"#what-is-short-hand-if\">What is Short Hand If?<\/a><\/li><li ><a href=\"#what-is-short-hand-if-else\">What is Short Hand if&#8230;Else?<\/a><\/li><li ><a href=\"#what-is-and\">What Is And?<\/a><\/li><li ><a href=\"#what-is-or\">What is Or?<\/a><\/li><li ><a href=\"#what-is-not\">What is Not ?<\/a><\/li><li ><a href=\"#what-is-nested-if\">What is Nested if ?<\/a><\/li><li ><a href=\"#what-is-the-pass-statement\">What is the Pass Statement<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-if-statement-fa-qs\">Python if &#8230; statement &#8211; FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p> Python if statement is considered very similar to the other languages. Hence, if statements have a <a href=\"https:\/\/www.skillvertex.com\/blog\/python-logical-operators\/\" data-type=\"post\" data-id=\"6978\">logical expression<\/a> through which the data is compared and the decision is mostly made out of the comparison. Read the article to know more about Python&#8230; if statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-python-conditions-and-if-statements\">What are Python Conditions and if statements<\/h2>\n\n\n\n<p>Some of the conditions that <a href=\"https:\/\/www.skillvertex.com\/blog\/python-decision-making\/\" data-type=\"post\" data-id=\"7139\">Python <\/a>supports are given below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Equals:&nbsp;a == b<\/li>\n\n\n\n<li>Not Equals:&nbsp;a != b<\/li>\n\n\n\n<li>Less than&nbsp;a &lt; b<\/li>\n\n\n\n<li>Less than or equal to&nbsp;a &lt;= b<\/li>\n\n\n\n<li>Greater than:&nbsp;a &gt; b<\/li>\n\n\n\n<li>Greater than or equal to&nbsp;a &gt;= b<\/li>\n<\/ul>\n\n\n\n<p>However, these conditions will be defined in several ways. The most commonly used ways are &#8221;<a href=\"https:\/\/www.skillvertex.com\/blog\/c-if-statement\/\" data-type=\"post\" data-id=\"2265\">if statements<\/a>&#8221; and <a href=\"https:\/\/www.skillvertex.com\/blog\/python-for-loops\/\" data-type=\"post\" data-id=\"7180\">loops.<\/a><\/p>\n\n\n\n<p>Whereas, the if statement will be written by using the if <a href=\"https:\/\/www.skillvertex.com\/blog\/generics-keyword-in-c\/\" data-type=\"post\" data-id=\"3708\">keyword<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example 1: Basic if statement\nx = 10\n\nif x &gt; 0:\n    print(\"x is positive\")\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is positive\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-indentation\">What is Indentation?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/python-interview-questions\/\" data-type=\"post\" data-id=\"3614\">Python<\/a> will depend on the indentation to tell the scope of the code. Whereas, several other<a href=\"https:\/\/www.skillvertex.com\/blog\/socket-programming-in-cpp\/\" data-type=\"post\" data-id=\"3721\"> programming language<\/a>s will use the curly brackets for this.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Incorrect usage of if statement without proper indentation\nx = 5\n\nif x &gt; 0:\nprint(\"x is positive\")  # This line is not indented, and it will cause an IndentationError\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is positive\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-elif\">What is Elif?<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.skillvertex.com\/blog\/python-comparison-operators\/\" data-type=\"post\" data-id=\"6966\">elif keyword<\/a> is looked upon as the Python&#8217;s saying in other words &#8221;if the previous conditions were not true, then try this condition.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Corrected if statement with proper indentation\nx = 5\n\nif x &gt; 0:\n    print(\"x is positive\")  # This line is properly indented\n<\/code><\/pre>\n\n\n\n<p> Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is positive<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-else\">What is Else ?<\/h2>\n\n\n\n<p>In Python, we use the <a href=\"https:\/\/www.skillvertex.com\/blog\/c-ifelse-statement\/\" data-type=\"post\" data-id=\"2270\">else keyword<\/a> with an if statement to handle situations that aren&#8217;t covered by the conditions in the &#8216;<strong>if&#8217;<\/strong> and elif statements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imagine we have a number 'x'\nx = 7\n\n# Check if 'x' is even or odd\nif x % 2 == 0:\n    print(\"The number is even.\")\nelse:\n    print(\"The number is odd.\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The number is odd.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-short-hand-if\">What is Short Hand If?<\/h2>\n\n\n\n<p>In Python, if there is only one statement to execute, then it is required to put it on the same line as the if statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imagine we have a number 'x'\nx = 10\n\n# Shorthand if-else\nresult = \"Even\" if x % 2 == 0 else \"Odd\"\nprint(result)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Even\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-short-hand-if-else\">What is Short Hand if&#8230;Else?<\/h2>\n\n\n\n<p>In Python, if there is only one <a href=\"https:\/\/www.skillvertex.com\/blog\/break-statement-in-c\/\" data-type=\"post\" data-id=\"2014\">statement<\/a> to run, for if and else statement. Hence, it is possible to put it on the same line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imagine we have a number 'x'\nx = 15\n\n# Shorthand if-else\nresult = \"Even\" if x % 2 == 0 else \"Odd\"\nprint(result)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>odd<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-and\">What Is And?<\/h2>\n\n\n\n<p>The and keyword is considered as a logical operator and is used to merge the conditional statements.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imagine we have two conditions related to a number 'x'\nx = 8\n\n# Using 'and' statement\nif x &gt; 0 and x % 2 == 0:\n    print(\"x is a positive even number.\")\nelse:\n    print(\"x is either not positive or not even.\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x is a positive even number.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-or\">What is Or?<\/h2>\n\n\n\n<p>The Or keyword is referred to as a<a href=\"https:\/\/www.skillvertex.com\/blog\/python-logical-operators\/\" data-type=\"post\" data-id=\"6978\"> logical operato<\/a>r and will be used to merge conditional statements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imagine we have two conditions related to a number 'y'\ny = -3\n\n# Using 'or' statement\nif y &lt; 0 or y % 2 == 0:\n    print(\"y is either negative or an even number.\")\nelse:\n    print(\"y is neither negative nor an even number.\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y is either negative or an even number.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-not\">What is Not ?<\/h2>\n\n\n\n<p> The <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-a-reserved-keyword-in-c-language\/\" data-type=\"post\" data-id=\"2829\">not keyword<\/a> is referred to as a logical operator and will reverse the result of the conditional statements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imagine we have a boolean variable 'is_sunny'\nis_sunny = False\n\n# Using 'not' keyword\nif not is_sunny:\n    print(\"It's not sunny today.\")\nelse:\n    print(\"It's a sunny day!\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>It's not sunny today.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-nested-if\">What is Nested if ?<\/h2>\n\n\n\n<p>The nested if statements are referred to as the if statements will be kept inside the if statements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imagine we have a number 'num'\nnum = 15\n\n# Nested if statements\nif num &gt; 0:\n    print(\"num is positive.\")\n    \n    if num % 2 == 0:\n        print(\"num is also even.\")\n    else:\n        print(\"num is odd.\")\nelse:\n    print(\"num is not positive.\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num is positive.\nnum is odd.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-pass-statement\">What is the Pass Statement<\/h2>\n\n\n\n<p>The if statements cannot be put as empty, whereas if the if statement is kept without any content, then it is necessary to pass the statements to ignore the error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In Python, nested if statements provide a way to make more intricate decisions in our programs. By placing one if statement inside another, we can create a hierarchy of conditions. <\/p>\n\n\n\n<p>Hence, this allows us to check multiple criteria and execute different code blocks based on those conditions. It&#8217;s like making decisions within decisions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-if-statement-fa-qs\">Python if &#8230; statement &#8211; 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-1707913496030\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a Python if statement?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The if statement will let us run a block code if the certain condition is true.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707913502843\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the syntax of an if statement?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  The syntax for an if statement is\u00a0the if keyword followed by a Boolean expression enclosed in parentheses<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707913510443\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What is a simple if statement in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The if in C is\u00a0a decision-making statement that is used to execute a block of code based on the value of the given expression<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python if statement is considered very similar to the other languages. Hence, if statements have a logical expression through which the data is compared and the decision is mostly made out of the comparison. Read the article to know more about Python&#8230; if statement. What are Python Conditions and if statements Some of the conditions &#8230; <a title=\"Python if &#8230; statement\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-if-statement\/\" aria-label=\"More on Python if &#8230; statement\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7151,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[909,57,866],"class_list":["post-7145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-if-statement","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\/7145","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=7145"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7145\/revisions"}],"predecessor-version":[{"id":8262,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7145\/revisions\/8262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7151"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}