{"id":755,"date":"2019-06-15T11:59:54","date_gmt":"2019-06-15T06:29:54","guid":{"rendered":"https:\/\/www.guru99.com\/if-loop-python-conditional-structures.html"},"modified":"2026-06-11T19:30:25","modified_gmt":"2026-06-11T14:00:25","slug":"if-loop-python-conditional-structures","status":"publish","type":"post","link":"https:\/\/www.guru99.com\/if-loop-python-conditional-structures.html","title":{"rendered":"Python Conditional Statements: IF&#8230;Else, ELIF &#038; Switch Case","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<style>\n.readmorecontainer {\n            max-width: 100%;\n            margin: 0 auto;\n            background-color: #e6f4fb;\n            border-radius: 8px;\n            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);\n            padding: 1rem;\n        }\n\n        .readmorecontainer strong {\n            font-size: 1.1em;\n        }\n\n        .expandable-content {\n            max-height: 100px;\n            overflow: hidden;\n            transition: max-height 0.3s ease-out;\n            position: relative;\n        }\n\n        .expandable-content::after {\n            content: \"\";\n            position: absolute;\n            bottom: 0;\n            left: 0;\n            right: 0;\n            height: 40px;\n            background: linear-gradient(to bottom, transparent, #f6fcff);\n        }\n\n        .expandable-content.expanded {\n            max-height: 1000px;\n            transition: max-height 0.5s ease-in;\n        }\n\n        .expandable-content.expanded::after {\n            display: none;\n        }\n\n        .expandable-content ul {\n            margin: 1rem 0;\n            padding-left: 1.5rem;\n        }\n\n        .expandable-content li {\n            margin-bottom: 0.5rem;\n            line-height: 1.6;\n        }\n\n        .chevron {\n            width: 20px;\n            height: 20px;\n            transition: transform 0.3s;\n            display: inline-block;\n            vertical-align: middle;\n        }\n\n        .chevron.rotated {\n            transform: rotate(180deg);\n        }\n\n        .btn-container {\n            text-align: center;\n        }\n\n        #readMoreBtn {\n            cursor: pointer;\n            text-decoration: none;\n            color: #0066cc;\n            font-weight: 600;\n            display: inline-flex;\n            align-items: center;\n            gap: 0.5rem;\n            background: none;\n            border: none;\n            font-size: 1.2rem;\n            padding: 0.5rem 1rem;\n        }\n\n        #readMoreBtn:hover {\n            color: #0052a3;\n        }\n<\/style>\n\n\n\n<div class=\"readmorecontainer\">\n<p><strong>\u26a1 Smart Summary<\/strong><\/p>\n\n\n<p>Python Conditional Statements direct program flow by executing specific code blocks only when Boolean expressions evaluate to true. They include if, else, elif, nested if, the ternary operator, and structural pattern matching for clean, decision-driven logic.<\/p>\n\n<div class=\"expandable-content\" id=\"expandableContent\">\n<ul style=\"list-style-type: none;\">\n<li><strong>Core Concept:<\/strong> An if statement runs its indented block only when the condition evaluates to true, forming the foundation of decision-making in Python.<\/li>\n<li><strong>Handle Alternatives:<\/strong> Add else for a fallback path and elif to test additional conditions in sequence without deep nesting.<\/li>\n<li><strong>Avoid Logic Errors:<\/strong> Use a single if-elif-else chain rather than independent if statements when exactly one outcome should apply.<\/li>\n<li><strong>Write Concise Code:<\/strong> Apply the ternary expression value_if_true if condition else value_if_false to assign a value in one readable line.<\/li>\n<li><strong>Modern Branching:<\/strong> Replace long elif chains with dictionary mapping or the match-case statement introduced in Python 3.10 for cleaner multiway selection.<\/li>\n<\/ul>\n<\/div>\n\n    <div class=\"btn-container\">\n        <a href=\"javascript:void(0);\" id=\"readMoreBtn\" data-lasso-id=\"99037436\">\n            <span id=\"buttonText\">Read More<\/span>\n            <svg class=\"chevron\" id=\"chevron\" fill=\"none\" stroke=\"currentColor\" viewbox=\"0 0 24 24\">\n                <path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M19 9l-7 7-7-7\"><\/path>\n            <\/svg>\n        <\/a>\n    <\/div>\n<\/div>\n\n\n<p style=\"text-align: center;padding-top: 15px;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/if-loop-python-conditional-structures.png\" alt=\"Python Conditional Statements\" width=\"600\" height=\"250\" class=\"\"><\/p>\n\n\n<h2 class=\"wp-block-heading\">What are Conditional Statements in Python?<\/h2>\n\n\n\n<p><strong>Conditional Statements in Python<\/strong> perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. They let a program make decisions and follow different paths instead of running every line in sequence. In Python, conditional statements are handled by the <strong>if<\/strong> statement and its companions: else, elif, and nested if.<\/p>\n\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);background-color:#edf2f7;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);max-width:450px;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-contents-title{font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-content-wrap .kb-table-of-content-list{font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-basiccircle .kb-table-of-contents-icon-trigger:after, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-basiccircle .kb-table-of-contents-icon-trigger:before, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-arrowcircle .kb-table-of-contents-icon-trigger:after, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-arrowcircle .kb-table-of-contents-icon-trigger:before, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-xclosecircle .kb-table-of-contents-icon-trigger:after, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-xclosecircle .kb-table-of-contents-icon-trigger:before{background-color:#edf2f7;}<\/style>\n\n<div class='code-block code-block-2' style='margin: 8px 0; clear: both;'>\n<style>\n.guru99_incontent_21 {\n\tmin-height: 280px !important;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n}\n<\/style>\n\n<div align=\"center\" id=\"guru99_mobile_display\" class=\"guru99_incontent_21\">\n    \n  <script>\n    googletag.cmd.push(function() { googletag.display('guru99_mobile_display'); });\n  <\/script>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What is Python If Statement?<\/h2>\n\n\n\n<p>The <strong>Python if statement<\/strong> is used for decision-making operations. It contains a body of code that runs only when the condition given in the if statement is true. If the condition is false, the optional else statement runs instead, holding the code for the else condition. When you want to act on one condition while another is not true, you use the Python if-else statement.<\/p>\n\n\n\n<p><strong>Python if Statement Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if expression:\n    statement\nelse:\n    statement<\/code><\/pre>\n\n\n\n<p><strong>Python if\u2026else Flowchart<\/strong><\/p>\n\n\n\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.guru99.com\/images\/2013\/04\/if_then_flowchart.png\" data-lasso-id=\"99037437\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/2013\/04\/if_then_flowchart.png\" alt=\"Python if\u2026else flowchart showing the true and false branches\" width=\"420\" height=\"378\"><\/a><\/p>\n\n\n\n<p>The flowchart above shows how control flows through an if-else statement. Let us see an example of the Python if-else statement in action:<\/p>\n\n\n\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.1.png\" data-lasso-id=\"99037438\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.1.png\" alt=\"Python if statement example output in the editor\" width=\"607\" height=\"437\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example file for working with conditional statements\ndef main():\n    x, y = 2, 8\n    if(x &lt; y):\n        st = \"x is less than y\"\n    print(st)\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Code Line 3:<\/strong> We define two variables x, y = 2, 8.<\/li>\n\n\n\n<li><strong>Code Line 4:<\/strong> The if statement checks the condition x &lt; y, which is <strong>True<\/strong> in this case.<\/li>\n\n\n\n<li><strong>Code Line 5:<\/strong> The variable st is set to &#8220;x is less than y.&#8221;<\/li>\n\n\n\n<li><strong>Code Line 6:<\/strong> The line print(st) outputs the value of st, which is &#8220;x is less than y.&#8221;<\/li>\n<\/ul>\n\n\n<div class='code-block code-block-3' style='margin: 8px 0; clear: both;'>\n<style>\n.guru99_incontent_21 {\n\tmin-height:280px !important;\n}\n<\/style>\n\n<!-- Tag ID: guru99_static_3 -->\n<div id='guru99_incontent_2' class=\"guru99_incontent_21\">\n  <script>\n    googletag.cmd.push(function() { googletag.display('guru99_incontent_2'); });\n  <\/script>\n<\/div>\n<div class='yarpp yarpp-related yarpp-related-shortcode yarpp-template-yarpp-template-custom'>\n<div class=\"custom-related-posts\">\n    <h3 class=\"related-title\">RELATED ARTICLES<\/h3>\n    <ul>\n            <li>\n            <a href=\"https:\/\/www.guru99.com\/variables-in-python.html\" rel=\"bookmark\" title=\"Python Variables: How to Define\/Declare String Variable Types\">\n                Python Variables: How to Define\/Declare String Variable Types            <\/a>\n        <\/li>\n            <li>\n            <a href=\"https:\/\/www.guru99.com\/python-vs-php.html\" rel=\"bookmark\" title=\"PHP Vs. Python: Key Difference Between Them\">\n                PHP Vs. Python: Key Difference Between Them            <\/a>\n        <\/li>\n            <li>\n            <a href=\"https:\/\/www.guru99.com\/python-ide-code-editor.html\" rel=\"bookmark\" title=\"10 BEST Python IDE &amp; Code Editors for Windows ()\">\n                10 BEST Python IDE &amp; Code Editors for Windows ()            <\/a>\n        <\/li>\n            <li>\n            <a href=\"https:\/\/www.guru99.com\/python-file-readline.html\" rel=\"bookmark\" title=\"Python readline() Method with Examples\">\n                Python readline() Method with Examples            <\/a>\n        <\/li>\n        <\/ul>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What Happens When the &#8220;if&#8221; Condition Is Not Met<\/h2>\n\n\n\n<p>Building on the previous example, let us see what happens when the if condition in Python is not met. When the condition is false and no else branch exists, the indented code is skipped entirely.<\/p>\n\n\n\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.2.png\" data-lasso-id=\"99037439\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.2.png\" alt=\"Python error when the if condition is not met\" width=\"807\" height=\"542\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example file for working with conditional statements\ndef main():\n    x, y = 8, 4\n    if(x &lt; y):\n        st = \"x is less than y\"\n    print(st)\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Code Line 3:<\/strong> We define two variables x, y = 8, 4.<\/li>\n\n\n\n<li><strong>Code Line 4:<\/strong> The if statement checks the condition x &lt; y, which is <strong>False<\/strong> in this case.<\/li>\n\n\n\n<li><strong>Code Line 5:<\/strong> The variable st is <strong>NOT<\/strong> set to &#8220;x is less than y.&#8221;<\/li>\n\n\n\n<li><strong>Code Line 6:<\/strong> The line print(st) tries to print a variable that was never declared, so Python raises a <strong>NameError<\/strong>.<\/li>\n<\/ul>\n\n\n\n<div style=\"background-color:#fff8e1;border-left:4px solid #ffb300;padding:12px 16px;margin:16px 0;border-radius:4px;\">\n<strong>\u26a0 Warning:<\/strong> Referencing a variable that is only assigned inside an if block is a common beginner mistake. Either provide an else branch or give the variable a default value before the if statement.\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use the &#8220;else&#8221; Condition<\/h2>\n\n\n\n<p>To avoid the error above, you can add an else condition. The else condition is used when you have to judge one statement on the basis of another. If one condition is false, the else block provides an alternative path so the program still produces a result.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.3.png\" data-lasso-id=\"99037440\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.3.png\" alt=\"Python if-else condition example\" width=\"699\" height=\"457\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example file for working with conditional statements\ndef main():\n    x, y = 8, 4\n    if(x &lt; y):\n        st = \"x is less than y\"\n    else:\n        st = \"x is greater than y\"\n    print(st)\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Code Line 3:<\/strong> We define two variables x, y = 8, 4.<\/li>\n\n\n\n<li><strong>Code Line 4:<\/strong> The if statement checks the condition x &lt; y, which is <strong>False<\/strong> in this case.<\/li>\n\n\n\n<li><strong>Code Line 6:<\/strong> The flow of program control goes to the else condition.<\/li>\n\n\n\n<li><strong>Code Line 7:<\/strong> The variable st is set to &#8220;x is <strong>greater<\/strong> than y.&#8221;<\/li>\n\n\n\n<li><strong>Code Line 8:<\/strong> The line print(st) outputs the value of st, which is &#8220;x is greater than y.&#8221;<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">When the &#8220;else&#8221; Condition Does Not Work<\/h2>\n\n\n\n<p>The else condition will not always give you the desired result. It can print the wrong output when there is a flaw in the program logic. This usually happens when you have to justify more than two statements or conditions in a program. An example will help you understand this concept.<\/p>\n\n\n\n<p>Here both variables are the same (8, 8), yet the program output is <strong>&#8220;x is greater than y,&#8221;<\/strong> which is <strong>WRONG<\/strong>. This happens because the program checks the first condition (the if condition), and when it fails, it prints the second condition (the else condition) as the default. In the next step, we will see how to correct this error.<\/p>\n\n\n\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.4.png\" data-lasso-id=\"99037441\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.4.png\" alt=\"Python else condition producing a wrong result\" width=\"647\" height=\"457\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example file for working with conditional statements\ndef main():\n    x, y = 8, 8\n    if(x &lt; y):\n        st = \"x is less than y\"\n    else:\n        st = \"x is greater than y\"\n    print(st)\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use the &#8220;elif&#8221; Condition<\/h2>\n\n\n\n<p>To correct the previous error made by the else condition, we can use the <strong>elif<\/strong> statement. By using the elif condition, you tell the program to test a third possibility when the first condition is false. You can chain multiple elif conditions to check for fourth, fifth, and further possibilities in your code.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.5.jpg\" data-lasso-id=\"99037442\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.5.jpg\" alt=\"Python elif condition example\" width=\"612\" height=\"555\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example file for working with conditional statements\ndef main():\n    x, y = 8, 8\n    if(x &lt; y):\n        st = \"x is less than y\"\n    elif(x == y):\n        st = \"x is same as y\"\n    else:\n        st = \"x is greater than y\"\n    print(st)\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Code Line 3:<\/strong> We define two variables x, y = 8, 8.<\/li>\n\n\n\n<li><strong>Code Line 4:<\/strong> The if statement checks the condition x &lt; y, which is <strong>False<\/strong> in this case.<\/li>\n\n\n\n<li><strong>Code Line 6:<\/strong> The flow of program control goes to the elif condition. It checks whether x == y, which is true.<\/li>\n\n\n\n<li><strong>Code Line 7:<\/strong> The variable st is set to &#8220;x is <strong>same as<\/strong> y.&#8221;<\/li>\n\n\n\n<li><strong>Code Line 10:<\/strong> The program control exits the if statement (it will not reach the else statement) and prints st. The output is &#8220;x is same as y,&#8221; which is correct.<\/li>\n<\/ul>\n\n\n<div class='code-block code-block-4' style='margin: 8px 0; clear: both;'>\n<style>\n.guru99_incontent_31 {\n\tmin-height:280px !important;\n}\n<\/style>\n\n<!-- Tag ID: guru99_static_4 -->\n<div id='guru99_incontent_3' class=\"guru99_incontent_31\">\n  <script>\n    googletag.cmd.push(function() { googletag.display('guru99_incontent_3'); });\n  <\/script>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to Execute a Conditional Statement With Minimal Code<\/h2>\n\n\n\n<p>Now that you can write full if-elif-else blocks, Python lets you condense a simple condition into a single line. Instead of writing separate code for each branch, you can use a <strong>ternary (conditional) expression<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>value_if_true if condition else value_if_false<\/code><\/pre>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.6.jpg\" data-lasso-id=\"99037443\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/Pythonnew\/Python11.6.jpg\" alt=\"Python one-line ternary conditional statement example\" width=\"899\" height=\"343\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    x, y = 10, 8\n    st = \"x is less than y\" if (x &lt; y) else \"x is greater than or equal to y\"\n    print(st)\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Code Line 2:<\/strong> We define two variables x, y = 10, 8.<\/li>\n\n\n\n<li><strong>Code Line 3:<\/strong> The variable st is set to &#8220;x is less than y&#8221; if x &lt; y; otherwise it becomes &#8220;x is greater than or equal to y.&#8221; Because x > y here, st becomes the second value.<\/li>\n\n\n\n<li><strong>Code Line 4:<\/strong> Prints the value of st and gives the correct output.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Python Nested if Statement<\/h2>\n\n\n\n<p>A nested if statement places one if statement inside another, which is useful when a decision depends on a second condition. The following example demonstrates a nested if statement in Python that calculates shipping cost based on country and order total.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total = 100\n# country = \"US\"\ncountry = \"AU\"\n\nif country == \"US\":\n    if total &lt;= 50:\n        print(\"Shipping Cost is $50\")\n    elif total &lt;= 100:\n        print(\"Shipping Cost is $25\")\n    elif total &lt;= 150:\n        print(\"Shipping Cost is $5\")\n    else:\n        print(\"FREE\")\n\nif country == \"AU\":\n    if total &lt;= 50:\n        print(\"Shipping Cost is $100\")\n    else:\n        print(\"FREE\")<\/code><\/pre>\n\n\n\n<p>Uncomment Line 2 in the code above, comment out Line 3, and run the code again to see how the output changes for a different country.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Switch Case Statement in Python<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is a Switch Statement?<\/h3>\n\n\n\n<p>A switch statement is a multiway branch statement that compares the value of a variable against the values specified in case statements. For many years, the Python language did not have a switch statement, so developers implemented the same behavior with <a href=\"https:\/\/www.guru99.com\/python-dictionary-beginners-tutorial.html\" data-lasso-id=\"99037444\">Python dictionary<\/a> mapping.<\/p>\n\n\n\n<p><strong>Example using dictionary mapping:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def switch_example(argument):\n    switcher = {\n        0: \"This is Case Zero\",\n        1: \"This is Case One\",\n        2: \"This is Case Two\",\n    }\n    return switcher.get(argument, \"nothing\")\n\nif __name__ == \"__main__\":\n    argument = 1\n    print(switch_example(argument))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Switch Case With match (Python 3.10 and Later)<\/h3>\n\n\n\n<p>Since <strong>Python 3.10<\/strong> (released in October 2021), Python provides a native <strong>match-case<\/strong> statement, known as structural pattern matching. It offers a clean, readable alternative to long elif chains and the dictionary approach shown above.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def switch_example(argument):\n    match argument:\n        case 0:\n            return \"This is Case Zero\"\n        case 1:\n            return \"This is Case One\"\n        case 2:\n            return \"This is Case Two\"\n        case _:\n            return \"nothing\"\n\nif __name__ == \"__main__\":\n    print(switch_example(1))<\/code><\/pre>\n\n\n\n<p>The underscore (_) acts as the default case, matching any value not handled by the earlier cases, just like the default in a traditional switch statement.<\/p>\n\n\n\n<div style=\"background-color:#e8f5e9;border-left:4px solid #43a047;padding:12px 16px;margin:16px 0;border-radius:4px;\">\n<strong>\u2705 Tip:<\/strong> Use match-case if you are on Python 3.10 or newer. For older versions, dictionary mapping remains the standard way to emulate a switch statement.\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">if vs elif vs else: Quick Comparison<\/h2>\n\n\n\n<p>Use this comparison to decide which conditional structure fits your decision-making logic.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Statement<\/th><th>Purpose<\/th><th>When to Use<\/th><\/tr><\/thead><tbody><tr><td><strong>if<\/strong><\/td><td>Runs a block when a condition is true<\/td><td>A single yes\/no decision<\/td><\/tr><tr><td><strong>if\u2026else<\/strong><\/td><td>Provides a fallback when the condition is false<\/td><td>Two mutually exclusive outcomes<\/td><\/tr><tr><td><strong>elif<\/strong><\/td><td>Tests additional conditions in sequence<\/td><td>Three or more possibilities<\/td><\/tr><tr><td><strong>Nested if<\/strong><\/td><td>Places an if inside another if<\/td><td>A decision that depends on a second condition<\/td><\/tr><tr><td><strong>Ternary<\/strong><\/td><td>Condenses if-else into one line<\/td><td>Simple value assignment<\/td><\/tr><tr><td><strong>match-case<\/strong><\/td><td>Matches a value against many patterns<\/td><td>Multiway branching (Python 3.10+)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"background-color:#fff8e1;border-left:4px solid #ffb300;padding:12px 16px;margin:16px 0;border-radius:4px;\">\n<strong>\u26a0 Note:<\/strong> Python 2 reached end-of-life on January 1, 2020, and all examples in this article use Python 3 syntax. In Python 2, print was a statement (for example, print st) rather than a function, so the code above will not run unchanged on Python 2.\n<\/div>\n\n\n<h2 class=\"toc-ignore\">FAQs<\/h2>\n\n<style>.kt-accordion-id_2eaf18-ec .kt-accordion-inner-wrap{column-gap:var(--global-kb-gap-md, 2rem);row-gap:10px;}.kt-accordion-id_2eaf18-ec .kt-accordion-panel-inner{border-top-width:0px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;background:#ffffff;padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);}.kt-accordion-id_2eaf18-ec > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header{border-top-color:#eeeeee;border-right-color:#eeeeee;border-bottom-color:#eeeeee;border-left-color:#eeeeee;border-top-width:2px;border-right-width:2px;border-bottom-width:2px;border-left-width:2px;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background:#ffffff;font-size:18px;line-height:24px;font-weight:bold;color:#444444;padding-top:14px;padding-right:16px;padding-bottom:14px;padding-left:16px;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap .kt-blocks-accordion-icon-trigger:before{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-icon-trigger{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-icon-trigger:before{background:#ffffff;}.kt-accordion-id_2eaf18-ec > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header:hover, \n\t\t\t\tbody:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec .kt-blocks-accordion-header:focus-visible{color:#444444;background:#ffffff;border-top-color:#d4d4d4;border-right-color:#d4d4d4;border-bottom-color:#d4d4d4;border-left-color:#d4d4d4;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:before, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-blocks-accordion--visible .kt-blocks-accordion-icon-trigger:after, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger:before{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:before, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger:after, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger:before{background:#ffffff;}.kt-accordion-id_2eaf18-ec .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible,\n\t\t\t\t.kt-accordion-id_2eaf18-ec > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header.kt-accordion-panel-active{color:#444444;background:#ffffff;border-top-color:#eeeeee;border-right-color:#eeeeee;border-bottom-color:#eeeeee;border-left-color:#0e9cd1;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:before{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:before{background:#ffffff;}@media all and (max-width: 767px){.kt-accordion-id_2eaf18-ec .kt-accordion-inner-wrap{display:block;}.kt-accordion-id_2eaf18-ec .kt-accordion-inner-wrap .kt-accordion-pane:not(:first-child){margin-top:10px;}}<\/style>\n<div class=\"wp-block-kadence-accordion alignnone\"><div class=\"kt-accordion-wrap kt-accordion-id_2eaf18-ec kt-accordion-has-5-panes kt-active-pane-0 kt-accordion-block kt-pane-header-alignment-left kt-accodion-icon-style-arrow kt-accodion-icon-side-right\" style=\"max-width:none\"><div class=\"kt-accordion-inner-wrap\" data-allow-multiple-open=\"true\" data-start-open=\"none\">\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-1 kt-pane_23d2b7-bd\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\u2753 What is the difference between if and elif in Python?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p>An if statement starts a decision and is always evaluated. An elif (else-if) is checked only when the preceding if or elif conditions are false, letting you test several possibilities in one chain while exactly one block runs.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-2 kt-pane_cce570-61\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83d\udd00 Does Python have a switch-case statement?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p>Yes. Since Python 3.10, the match-case statement provides native switch-style branching through structural pattern matching. On older versions, developers emulate a switch using dictionary mapping with the get() method to supply a default value.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-3 kt-pane_abc123-cd\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\u27a1\ufe0f How do you write an if-else statement in one line in Python?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p>Use a ternary expression: value_if_true if condition else value_if_false. For example, st = &#8220;low&#8221; if x &lt; 10 else &#8220;high&#8221; assigns a value in a single readable line without a full if-else block.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-4 kt-pane_def456-ef\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83e\udd16 Can AI tools help write Python conditional statements?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p>Yes. AI coding assistants can generate, explain, and debug if-elif-else logic from a plain-language prompt. They are helpful for learning, but you should still review the output to confirm the conditions and indentation match your intended logic.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-5 kt-pane_ghi789-gh\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83e\udde0 Are conditional statements used in AI and machine learning?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p>Yes. Conditional logic is fundamental to AI. Decision trees, rule-based systems, and data-preprocessing pipelines all rely on if-else branching to choose actions, filter data, and control how a model handles different inputs.<\/p>\n<\/div><\/div><\/div>\n<\/div><\/div><\/div>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u26a1 Smart Summary Python Conditional Statements direct program flow by executing specific code blocks only when Boolean expressions evaluate to true. They include if, else, elif, nested if, the ternary operator, and structural pattern matching for clean, decision-driven logic. Core Concept: An if statement runs its indented block only when the condition evaluates to true,&#8230;<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":61,"featured_media":167889,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[40],"tags":[154,146],"coauthors":[503],"class_list":["post-755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-convertbox-developer","tag-non-amp"],"taxonomy_info":{"category":[{"value":40,"label":"Python"}],"post_tag":[{"value":154,"label":"Convertbox-Developer"},{"value":146,"label":"Non AMP"}]},"featured_image_src_large":["https:\/\/www.guru99.com\/images\/if-loop-python-conditional-structures.png",600,250,false],"author_info":{"display_name":"Logan Young","author_link":"https:\/\/www.guru99.com\/author\/logan"},"comment_info":0,"category_info":[{"term_id":40,"name":"Python","slug":"python","term_group":0,"term_taxonomy_id":40,"taxonomy":"category","description":"","parent":0,"count":101,"filter":"raw","cat_ID":40,"category_count":101,"category_description":"","cat_name":"Python","category_nicename":"python","category_parent":0}],"tag_info":[{"term_id":154,"name":"Convertbox-Developer","slug":"convertbox-developer","term_group":0,"term_taxonomy_id":154,"taxonomy":"post_tag","description":"","parent":0,"count":834,"filter":"raw"},{"term_id":146,"name":"Non AMP","slug":"non-amp","term_group":0,"term_taxonomy_id":146,"taxonomy":"post_tag","description":"","parent":0,"count":1292,"filter":"raw"}],"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts\/755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/users\/61"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/comments?post=755"}],"version-history":[{"count":2,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts\/755\/revisions"}],"predecessor-version":[{"id":167914,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts\/755\/revisions\/167914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/media\/167889"}],"wp:attachment":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/media?parent=755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/categories?post=755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/tags?post=755"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/coauthors?post=755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}