{"id":751,"date":"2024-09-11T18:49:01","date_gmt":"2024-09-11T18:49:01","guid":{"rendered":"https:\/\/www.zframez.com\/articles\/?p=751"},"modified":"2024-10-16T15:51:04","modified_gmt":"2024-10-16T15:51:04","slug":"chapter-5-python-conditional-statements-if-else-and-elif","status":"publish","type":"post","link":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif","title":{"rendered":"Chapter 5:Python Conditional Statements: if, else, and elif"},"content":{"rendered":"<body>\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-4-mastering-the-print-function-in-python\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n\n\n\n\n<div style=\"background: linear-gradient(to right, #ADD8E6, #8A2BE2); padding: 10px; border-radius: 10px; font-weight: 600; color: black;\">\n  <h3 style=\"text-align: center; margin-top: 0;\">Table of Contents<\/h3>\n  <ul style=\"padding-left: 20px;\">\n    <li><a href=\"#Introduction-to-Python-if-and-else\" style=\"color: black;\">Introduction to Python `if` and `else`<\/a><\/li>\n    <li><a href=\"#Understanding-Python-if-Syntax\" style=\"color: black;\">Understanding Python `if` Syntax<\/a><\/li>\n    <li><a href=\"#Multiple-Lines-of-Code-in-an-if-Statement\" style=\"color: black;\">Multiple Lines of Code in an `if` Statement<\/a><\/li>\n    <li><a href=\"#Using-else-with-if\" style=\"color: black;\">Using `else` with `if`<\/a><\/li>\n    <li><a href=\"#How-does-Python-know-when-the-if-code-block-is-finished\" style=\"color: black;\">How does Python know when the `if` code block is finished?<\/a><\/li>\n    <li><a href=\"#Using-the-pass-Statement\" style=\"color: black;\">Using the `pass` Statement<\/a><\/li>\n    <li><a href=\"#Checking-Odd-and-Even-Numbers-with-if-and-else\" style=\"color: black;\">Checking Odd and Even Numbers with `if` and `else`<\/a><\/li>\n    <li><a href=\"#Combining-conditions-with-and-and-or\" style=\"color: black;\">Combining conditions with `and` and `or`<\/a><\/li>\n    <li><a href=\"#Nested-if-statements-in-Python\" style=\"color: black;\">Nested `if` statements in Python<\/a><\/li>\n    <li><a href=\"#Using-elif-for-Multiple-Conditions\" style=\"color: black;\">Using `elif` for Multiple Conditions<\/a><\/li>\n    <li><a href=\"#Programming-Exercises\" style=\"color: black;\">Programming Exercises<\/a><\/li>\n  <\/ul>\n<\/div>\n\n\n\n<h1 class=\"wp-block-heading has-large-font-size\" id=\"Introduction-to-Python-if-and-else\"><strong>Introduction to Python <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>if<\/code> <\/mark>and <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code><\/strong><\/h1>\n\n\n\n<p>In the last chapter, we learned different ways to use the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print<\/mark><\/code> function. Now, let\u2019s try to understand the use of conditional statements,  <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> and <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code>.<\/p>\n\n\n\n<p>You can use the <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>if<\/code> <\/mark>statement when you want to execute a part of the code only if a certain condition is satisfied. Let\u2019s go through a simple example to understand the syntax of the <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>if<\/code> <\/mark>statement:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-364a737869f3cc6542546625d53f220b\"><code>a = 5\nif a &lt; 10:\n    print(\"Hello World!\")<\/code><\/pre>\n\n\n\n<p>In the above code, the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print(\"Hello World!\")<\/mark><\/code> statement will execute based on the value of the variable <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>a\"<\/code><\/mark>. Here, the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition is checking whether the value of <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>a<\/code>\u201c<\/mark> is less than 10. If that\u2019s <mark style=\"background-color:#ffffff\" class=\"has-inline-color\">true<\/mark>, the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print<\/mark><\/code> statement runs. So, if you run this code, you\u2019ll see the output \u201cHello World!\u201d You can try setting the value of the variable <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>a\"<\/code><\/mark> to a number like 15 (greater than 10), and you\u2019ll notice that it doesn\u2019t produce any output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Understanding-Python-if-Syntax\"><strong>Understanding Python <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> Syntax:<\/strong><\/h2>\n\n\n\n<p>Now, about the syntax. Unlike other programming languages that use curly brackets (or flower brackets), Python groups the code for an <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition using indentation. In the example above, notice that the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print<\/mark><\/code> function isn\u2019t written directly below the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition. Instead, it\u2019s indented after a few spaces. You can add one or more lines of code indented this way, and they will all be treated as part of the code under the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition.<\/p>\n\n\n\n<p><strong>Try the code yourself:<\/strong><\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python3\/242541c05afb\" width=\"100%\" height=\"250\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Multiple-Lines-of-Code-in-an-if-Statement\"><strong>Multiple Lines of Code in an <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> Statement<\/strong><\/h2>\n\n\n\n<p>Let\u2019s check an example with multiple lines of code in one <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-c27a4ce6042abd925ad224123e192ae7\"><code>a = 5\nif a &lt; 10:\n    print(\"Hello World!\")\n    print(\"Python is Fun!\")\n    print(\"Keep Coding!\")<\/code><\/pre>\n\n\n\n<p>Notice that all the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print<\/mark><\/code> functions are aligned with the same indentation to keep them under the same <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition. Try running the code after adding one or more spaces at the beginning of the second or third <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print<\/mark><\/code> function to see how Python raises an indentation error.<\/p>\n\n\n\n<p><strong>Try the code:<\/strong><\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python3\/208e5a92cbeb\" width=\"100%\" height=\"250\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<p>You can also place the code along with the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition on the same line. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-c4920a5c5a2af929c6a2bb08e5083fd5\"><code>a = 5\nif a &lt; 10: print(\"Hello World!\")<\/code><\/pre>\n\n\n\n<p>If you have multiple lines of code that you want to keep under the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition on the same line, use a semicolon (<code>;<\/code>) to separate them. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-876af59784c20aeecb092f8a501a0683\"><code>a = 5\nif a &lt; 10: print(\"Hello World!\"); print(\"Python is Fun!\"); print(\"Keep Coding!\")<\/code><\/pre>\n\n\n\n<p>As you may have already figured out, when we use an <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition, it expects two possible responses: <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>True<\/code> <\/mark>or <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code>. In our previous examples, the expression <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">a &lt; 10<\/mark><\/code> returned <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>True<\/code> <\/mark>because the value of the variable  <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>a\"<\/code><\/mark> was set to 5. However, the same expression can return <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code><mark style=\"background-color:#ffffff\" class=\"has-inline-color\"> <\/mark>if the value of <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>a<\/code>\u201c<\/mark> is not less than 10.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Using-else-with-if\"><strong>Using <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> with <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code><\/strong>:<\/h2>\n\n\n\n<p>Now, let\u2019s try a code with the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> part, which will be executed when the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> expression is <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-0d3cca1cf3b74f86234ec92c8f556ae6\"><code>a = 50\nif a &lt; 10:\n    print(\"Hello World\")\nelse:\n    print(\"Python is Fun\")<\/code><\/pre>\n\n\n\n<p>In this code, the expression <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">a &lt; 10<\/mark><\/code> will return <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code>, so the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> part will be executed. Therefore, the output will be \u201cPython is Fun.\u201d<\/p>\n\n\n\n<p>You need to follow the same indentation rules for <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> as you do for <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code>. You can have multiple lines of code in the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> part, and just like with <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code>, they should all be aligned with the same indentation.<\/p>\n\n\n\n<p><strong>Try the code yourself:<\/strong><\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python3\/4d3cde25f591\" width=\"100%\" height=\"250\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"How-does-Python-know-when-the-if-code-block-is-finished\"><strong>How does Python know when the \u201c<code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark>\"<\/code> code block is finished?<\/strong><\/h2>\n\n\n\n<p>Python uses indentation to define which lines belong to the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition. All the lines that are indented after the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement are considered part of that block. If you write any line of code directly below the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> without indentation, it will be treated as outside the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> block. It\u2019s important to remember that at least one line of code must be indented under the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition, or you\u2019ll get an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Using-the-pass-Statement\"><strong>Using the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">pass<\/mark><\/code> Statement<\/strong><\/h2>\n\n\n\n<p>In some cases, you may not have any code to place under the indented block of an <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> or <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>else<\/code> <\/mark>statement. However, Python does not allow an empty <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> or <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> block without any code. In such situations, you can use the keyword <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">pass<\/mark><\/code> with indentation to avoid a syntax error.<\/p>\n\n\n\n<p>For example, if you don\u2019t have anything to print or execute when the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition is satisfied, you can use <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">pass<\/mark><\/code> as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-99b2b0316c37a7625c56d8427b5f6d3b\"><code>a = 50\nif a &lt; 10:\n    pass\nelse:\n    print(\"Hello World\")<\/code><\/pre>\n\n\n\n<p>You can use the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">pass<\/mark><\/code> statement wherever an indented block is required, such as in a <code>while<\/code> loop, a function definition, a class definition, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Checking-Odd-and-Even-Numbers-with-if-and-else\"><strong>Checking Odd and Even Numbers with <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> and <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code><\/strong><\/h2>\n\n\n\n<p>Here\u2019s another classic example using <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> and <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-bee7b46936e35802421cb3885361abcb\"><code>a = int(input(\"Enter a number: \"))\nif a % 2 == 1:\n    print(f\"{a} is an odd number\")\nelse:\n    print(f\"{a} is an even number\")\n<\/code><\/pre>\n\n\n\n<p>The above program will ask the user for a number and print whether it is odd or even. The expression <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">a % 2<\/mark><\/code> is used to get the remainder, and we are checking whether the remainder is 1 or 0 to determine if the given number is odd or even.<\/p>\n\n\n\n<p>In Python, an expression used with <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> is considered <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>True<\/code> <\/mark>if it evaluates to any non-zero value, and <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"><code>False<\/code> <\/mark>if it evaluates to <code>0<\/code>, <code>None<\/code>, <code>False<\/code>, or an empty collection (like an empty string, list, or dictionary).<\/p>\n\n\n\n<p>So, in this case, since <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">a % 2<\/mark><\/code> will return 1 when the variable <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>a\"<\/code><\/mark> is odd, you don\u2019t necessarily need to write the full expression<mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\"> <code>a % 2 == 1<\/code><\/mark>. Instead, just <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">a % 2<\/mark><\/code> is enough.<\/p>\n\n\n\n<p><strong>Try the code yourself:<\/strong><\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python3\/b50a2b8fe8bd\" width=\"100%\" height=\"250\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Combining-conditions-with-and-and-or\"><strong>Combining conditions with <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\"and\"<\/mark><\/code> and \u201c<code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">or\"<\/mark><\/code> <\/strong><\/h2>\n\n\n\n<p>You can combine multiple expressions for evaluation using the <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code><strong>and<\/strong><\/code>\u201c<\/mark> and <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code><strong>or<\/strong>\"<\/code><\/mark> operators.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-ef1483cd94eb32d267aecc1e29e6a5ab\"><code>a, b = 10, 20\nif a &lt; 50 and b &lt; 100:\n    print(\"Both the conditions are True\")\nif a &lt; 5 or b &lt; 100:\n    print(\"Either both or one of the conditions is True\")\n<\/code><\/pre>\n\n\n\n<p>In the first <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement, both conditions must be <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">True<\/mark><\/code> for the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print<\/mark><\/code> function to execute. In the second <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement, the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">print<\/mark><\/code> function will execute if one of the conditions is <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">True<\/mark><\/code>.<\/p>\n\n\n\n<p>Though we can check multiple conditions using the <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code><strong>and<\/strong>\"<\/code><\/mark> operator, there\u2019s a limitation: you won\u2019t be able to tell which specific expression returned <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code> because there\u2019s only one <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> statement for all the conditions.<\/p>\n\n\n\n<p>If you need to check multiple conditions and want a separate <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> section for each, you can use a nested <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> structure instead. This way, each condition can have its own corresponding <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> block, allowing you to identify exactly which condition was not met.<\/p>\n\n\n\n<p><strong>Try the code yourself:<\/strong><\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python3\/f3942691885f\" width=\"100%\" height=\"250\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Nested-if-statements-in-Python\"><strong>Nested <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statements in Python<\/strong><\/h2>\n\n\n\n<p>You can place one <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement inside the indented block of another <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement, which is called a nested <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code>.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-87a6a527a1fb0a45d755ec9df0e95090\"><code>a, b = 10, 20\nif a &lt; 50:\n    if b &lt; 100:\n        print(\"a is less than 50 and b is less than 100\")\n    else:\n        print(\"a is less than 50 but b is not less than 100\")\nelse:\n    print(\"a is not less than 50\")\n<\/code><\/pre>\n\n\n\n<p>In the code editor below, experiment with different values for the variables <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>a\"<\/code><\/mark> and <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">\u201c<code>b\"<\/code><\/mark>. Change the values and see how the nested <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statements behave. Pay attention to which blocks of code get executed depending on the conditions. This will help you understand how nested <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statements work in Python.<\/p>\n\n\n\n<p>For example: Set <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">a = <\/mark><\/code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">40<\/mark> and <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">b = 120<\/mark><\/code> to see how the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> statements are triggered.<\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python3\/053df87812c2\" width=\"100%\" height=\"250\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Using-elif-for-Multiple-Conditions\"><strong>Using <mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">elif<\/mark> for Multiple Conditions<\/strong><\/h2>\n\n\n\n<p><code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">elif<\/mark><\/code> is short for \u201celse if\u201d and is used to check another condition if the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement returns <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code>. In an <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if-else<\/mark><\/code> structure, the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> part gets executed if the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement fails without checking any further conditions. However, in an <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if-elif<\/mark><\/code> structure, the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">elif<\/mark><\/code> part gets executed only after checking an additional condition if the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition is <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-cyan-blue-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-2a80b32f71043e307931e44054c6ff76\"><code>score = int(input(\"Enter the student's score: \"))\nif score &gt;= 90:\n    print(\"Grade: A\")\nelif score &gt;= 80:\n    print(\"Grade: B\")\nelif score &gt;= 70:\n    print(\"Grade: C\")\nelif score &gt;= 60:\n    print(\"Grade: D\")\nelse:\n    print(\"Grade: F\")\n<\/code><\/pre>\n\n\n\n<p>In this example, we\u2019re using <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">elif<\/mark><\/code> to check additional conditions after the initial <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> statement. If the <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">if<\/mark><\/code> condition (score &gt;= 90) is <code>False<\/code>, the program moves on to check if the score is greater than or equal to 80 with <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">elif<\/mark><\/code>. If that condition is also <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">False<\/mark><\/code>, it continues to check the next condition (score &gt;= 70), and so on. The <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">else<\/mark><\/code> block at the end ensures that if none of the previous conditions are met, it will print \u201cGrade: F.\u201d This way, <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">elif<\/mark><\/code> allows us to handle multiple conditions in a clean and readable way<\/p>\n\n\n\n<p>Note that only one of these blocks will execute, whichever condition becomes <code><mark style=\"background-color:#ffe2c7\" class=\"has-inline-color\">True<\/mark><\/code> first will trigger its corresponding block, and the rest of the statements below it will be ignored.<\/p>\n\n\n\n<p><strong>Try the code yourself:<\/strong><\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python3\/d4fde47561cb\" width=\"100%\" height=\"250\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Programming-Exercises\"><strong>Programming Exercises : <\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Write a Python program to swap two numbers using a third variable.<\/li>\n\n\n\n<li>Write a Python program to swap two numbers without using a third variable.<\/li>\n\n\n\n<li>Write a Python program to read two numbers and find the sum of their cubes.<\/li>\n\n\n\n<li>Write a Python program to read three numbers, and if any two numbers are equal, print that number.<\/li>\n\n\n\n<li>Write a Python program to read three numbers and find the smallest among them.<\/li>\n\n\n\n<li>Write a Python program to read three numbers and print them in ascending order (without using the sort function).<\/li>\n\n\n\n<li>Write a Python program to read the radius of a circle and print the area.<\/li>\n\n\n\n<li>Write a Python program to read four numbers (representing the four octets of an IP) and check whether they are all in the range between 0 and 255.<\/li>\n\n\n\n<li>In the above program, if all numbers are in the valid range, print the class of IP.<\/li>\n\n\n\n<li>Write a Python program to read a number. If it is an even number, print the square of that number; otherwise, print the cube.<\/li>\n\n\n\n<li>Write a Python program to read three numbers and determine whether they form a valid triangle (sum of any two sides should be greater than the third side). If they form a valid triangle, print \u201cValid Triangle\u201d; otherwise, print \u201cInvalid Triangle.\u201d<\/li>\n\n\n\n<li>Write a Python program to read a character from the user and check if it is a vowel or a consonant. If the character is a vowel, print \u201cVowel\u201d; otherwise, print \u201cConsonant.\u201d<\/li>\n\n\n\n<li>Write a Python program to read a year and check if it is a leap year. If it is a leap year, print \u201cLeap Year\u201d; otherwise, print \u201cNot a Leap Year.\u201d<\/li>\n\n\n\n<li>Write a Python program to read an integer and print whether it is positive, negative, or zero.<\/li>\n\n\n\n<li>Write a Python program to read two numbers and check if one is a multiple of the other. If one is a multiple, print \u201cMultiple\u201d; otherwise, print \u201cNot a Multiple.\u201d<\/li>\n\n\n\n<li>Write a Python program to read a number and check if it is divisible by 5 and 11. If it is divisible by both, print \u201cDivisible by 5 and 11\u201d; otherwise, print \u201cNot Divisible by 5 and 11.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\">\n\n\n\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-4-mastering-the-print-function-in-python\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n\n\n\n\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>\u25c0 Previous Python Tutorials Next \u25b6 Table of Contents Introduction to Python `if` and `else` Understanding Python `if` Syntax Multiple Lines of Code in an `if` Statement Using `else` with `if` How does Python know when the `if` code block is finished? Using the `pass` Statement Checking Odd and Even Numbers with `if` and `else` [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":756,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-post-with-sidebar2","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[46],"tags":[267,271,262,268,266,261,49,269,263,264,273,265,270,238,275,274,272,50,47],"class_list":["post-751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-beginner-python","tag-coding-tutorials","tag-conditional-statements","tag-control-flow-python","tag-elif-python","tag-if-else-python","tag-learn-python","tag-programming-basics","tag-python","tag-python-code-examples","tag-python-coding","tag-python-conditionals","tag-python-elif-statement","tag-python-for-beginners","tag-python-functions","tag-python-if-statement","tag-python-logic","tag-python-programming","tag-python-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Chapter 5:Python Conditional Statements: if, else, and elif - Tutorials<\/title>\n<meta name=\"description\" content=\"Learn how to use Python&#039;s if, else, and elif statements to control the flow of your programs. This guide covers conditionals with examples .\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Chapter 5:Python Conditional Statements: if, else, and elif - Tutorials\" \/>\n<meta property=\"og:description\" content=\"Learn how to use Python&#039;s if, else, and elif statements to control the flow of your programs. This guide covers conditionals with examples .\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif\" \/>\n<meta property=\"og:site_name\" content=\"Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/zframez\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-11T18:49:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-16T15:51:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"sajith achipra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@zframez\" \/>\n<meta name=\"twitter:site\" content=\"@zframez\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"sajith achipra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif\"},\"author\":{\"name\":\"sajith achipra\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\"},\"headline\":\"Chapter 5:Python Conditional Statements: if, else, and elif\",\"datePublished\":\"2024-09-11T18:49:01+00:00\",\"dateModified\":\"2024-10-16T15:51:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif\"},\"wordCount\":1571,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1\",\"keywords\":[\"beginner Python\",\"coding tutorials\",\"conditional statements\",\"control flow Python\",\"elif Python\",\"if else Python\",\"learn python\",\"programming basics\",\"Python\",\"Python code examples\",\"Python coding\",\"Python conditionals\",\"Python elif statement\",\"Python for Beginners\",\"Python functions\",\"Python if statement\",\"Python logic\",\"python programming\",\"python tutorial\"],\"articleSection\":[\"python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif\",\"name\":\"Chapter 5:Python Conditional Statements: if, else, and elif - Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1\",\"datePublished\":\"2024-09-11T18:49:01+00:00\",\"dateModified\":\"2024-10-16T15:51:04+00:00\",\"description\":\"Learn how to use Python's if, else, and elif statements to control the flow of your programs. This guide covers conditionals with examples .\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1\",\"width\":1280,\"height\":720,\"caption\":\"Visual representation of a Python if statement and code block.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-5-python-conditional-statements-if-else-and-elif#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/www.zframez.com\\\/articles\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Chapter 5:Python Conditional Statements: if, else, and elif\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"name\":\"zframez tutorials\",\"description\":\"Learn networking bit by bit\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\",\"name\":\"zframez technologies\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"width\":864,\"height\":864,\"caption\":\"zframez technologies\"},\"image\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/zframez\\\/\",\"https:\\\/\\\/x.com\\\/zframez\",\"https:\\\/\\\/www.instagram.com\\\/zframez_technologies\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\",\"name\":\"sajith achipra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"caption\":\"sajith achipra\"},\"sameAs\":[\"http:\\\/\\\/www.zframez.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Chapter 5:Python Conditional Statements: if, else, and elif - Tutorials","description":"Learn how to use Python's if, else, and elif statements to control the flow of your programs. This guide covers conditionals with examples .","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif","og_locale":"en_US","og_type":"article","og_title":"Chapter 5:Python Conditional Statements: if, else, and elif - Tutorials","og_description":"Learn how to use Python's if, else, and elif statements to control the flow of your programs. This guide covers conditionals with examples .","og_url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif","og_site_name":"Tutorials","article_publisher":"https:\/\/www.facebook.com\/zframez\/","article_published_time":"2024-09-11T18:49:01+00:00","article_modified_time":"2024-10-16T15:51:04+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg","type":"image\/jpeg"}],"author":"sajith achipra","twitter_card":"summary_large_image","twitter_creator":"@zframez","twitter_site":"@zframez","twitter_misc":{"Written by":"sajith achipra","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#article","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif"},"author":{"name":"sajith achipra","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41"},"headline":"Chapter 5:Python Conditional Statements: if, else, and elif","datePublished":"2024-09-11T18:49:01+00:00","dateModified":"2024-10-16T15:51:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif"},"wordCount":1571,"commentCount":0,"publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"image":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1","keywords":["beginner Python","coding tutorials","conditional statements","control flow Python","elif Python","if else Python","learn python","programming basics","Python","Python code examples","Python coding","Python conditionals","Python elif statement","Python for Beginners","Python functions","Python if statement","Python logic","python programming","python tutorial"],"articleSection":["python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif","url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif","name":"Chapter 5:Python Conditional Statements: if, else, and elif - Tutorials","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage"},"image":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1","datePublished":"2024-09-11T18:49:01+00:00","dateModified":"2024-10-16T15:51:04+00:00","description":"Learn how to use Python's if, else, and elif statements to control the flow of your programs. This guide covers conditionals with examples .","breadcrumb":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#primaryimage","url":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1","width":1280,"height":720,"caption":"Visual representation of a Python if statement and code block."},{"@type":"BreadcrumbList","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Tutorials","item":"https:\/\/www.zframez.com\/articles"},{"@type":"ListItem","position":2,"name":"Chapter 5:Python Conditional Statements: if, else, and elif"}]},{"@type":"WebSite","@id":"https:\/\/www.zframez.com\/articles\/#website","url":"https:\/\/www.zframez.com\/articles\/","name":"zframez tutorials","description":"Learn networking bit by bit","publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.zframez.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.zframez.com\/articles\/#organization","name":"zframez technologies","url":"https:\/\/www.zframez.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","width":864,"height":864,"caption":"zframez technologies"},"image":{"@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/zframez\/","https:\/\/x.com\/zframez","https:\/\/www.instagram.com\/zframez_technologies\/"]},{"@type":"Person","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41","name":"sajith achipra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","caption":"sajith achipra"},"sameAs":["http:\/\/www.zframez.com"]}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1280%2C720&ssl=1","jetpack-related-posts":[{"id":83,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-1-introduction-to-python-programming","url_meta":{"origin":751,"position":0},"title":"Chapter 1: Introduction to Python Programming","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In this Python tutorial, we'll explore Python's origins, key milestones, and its standout features and benefits. Whether you're new to programming or looking to expand your skills, this guide will provide you with a solid foundation in Python. If you want to understand why\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Table filled with Python-related materials, including a computer displaying Python code, a Python textbook, and notes about Python","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":904,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-7-working-with-strings-in-python","url_meta":{"origin":751,"position":1},"title":"Chapter 7: Working with Strings in Python","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Python String Manipulation: Methods and Examples In Chapter 7, we\u2019ll be looking at how to work with strings in Python. Strings are used everywhere in programming, and Python provides a lot of methods to make string manipulation easy. In this chapter, we\u2019ll explore methods\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":901,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","url_meta":{"origin":751,"position":2},"title":"Chapter 6: Python While and For Loops: Examples and Explanations","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Python While and For Loops In this chapter of our Python tutorial series, we will explore two essential looping structures in Python: while loops and for loops. Loops are a fundamental part of programming, allowing you to execute a block of code repeatedly under\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":98,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-3-working-with-variables-in-python","url_meta":{"origin":751,"position":3},"title":"Chapter 3: Working with Variables in Python","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In Python, variables are one of the fundamental concepts you'll work with. They allow you to store data that your programs can manipulate and use. In this chapter, we\u2019ll explore what variables are, how to check the Python version you\u2019re using, and the different\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Computer displaying a program, with a book and pen on the table, representing learning and coding in Python.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":90,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-2-setting-up-python","url_meta":{"origin":751,"position":4},"title":"Chapter 2: Setting Up Python","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Before you can start writing and running Python code, you\u2019ll need to set up Python on your computer. In this chapter, we\u2019ll guide you through the process of installing Python on Windows, macOS, and Linux. We\u2019ll also explain what an Integrated Development Environment (IDE)\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Screenshot of the official Python download page, highlighting the process of setting up Python.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":908,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-8-python-lists-and-tuples-operations-and-methods","url_meta":{"origin":751,"position":5},"title":"Chapter 8: Python Lists and Tuples -Operations and Methods","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Working with Lists and Tuples in Python In this chapter, we\u2019ll explore two important data structures in Python: lists and tuples. Lists allow you to store a collection of items that can be modified, while tuples are immutable, meaning their elements cannot be changed.\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/comments?post=751"}],"version-history":[{"count":5,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/751\/revisions"}],"predecessor-version":[{"id":1081,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/751\/revisions\/1081"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/media\/756"}],"wp:attachment":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/media?parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/categories?post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/tags?post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}