{"id":3666,"date":"2023-08-29T11:54:14","date_gmt":"2023-08-29T11:54:14","guid":{"rendered":"https:\/\/www.copahost.com\/blog\/?p=3666"},"modified":"2023-10-02T13:37:50","modified_gmt":"2023-10-02T13:37:50","slug":"elif-python","status":"publish","type":"post","link":"https:\/\/www.copahost.com\/blog\/elif-python\/","title":{"rendered":"Elif Python: Elif conditionals in Python"},"content":{"rendered":"\n<p>In the Python language, Elif creates <strong>conditional flow control structures<\/strong> in a program. This structure is very similar to the keyword  if, but with an important difference: while  if we test a condition is true or false,  elif we use it to test a condition is false and then execute a specific block of code.<br><br>In other words, if you use it to test and execute a block of code when a condition is true, and use &#8216;elif&#8217; to test and execute a block of code when a condition is false.<br><br>this article, We&#8217;ll explore how to use elif in Python, how to integrate it with other flow control frameworks like if and else, and how to apply it to practical projects. In addition, we&#8217;ll discuss the advantages and limitations of  elif, as well as some advice to avoid common mistakes when using elif in Python.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#Syntax\" title=\"Syntax\">Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#How_is_elif_different_from_if_in_Python\" title=\"How is elif different from if in Python?\">How is elif different from if in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#Examples_of_how_to_use_elif_in_Python\" title=\"Examples of how to use elif in Python\">Examples of how to use elif in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#Simple_examples_of_how_to_use_it_elif_in_Python\" title=\"Simple examples of how to use it&nbsp;&nbsp;elif&nbsp;in Python:\">Simple examples of how to use it&nbsp;&nbsp;elif&nbsp;in Python:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#Advanced_how-to_examples_elif_in_Python\" title=\"Advanced how-to examples&nbsp;&nbsp;elif&nbsp;in Python:\">Advanced how-to examples&nbsp;&nbsp;elif&nbsp;in Python:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#What_are_the_limitations_of_elif_in_Python\" title=\"What are the limitations of elif in Python?\">What are the limitations of elif in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#Avoid_common_mistakes_when_using_elif_in_Python\" title=\"Avoid common mistakes when using elif in Python\">Avoid common mistakes when using elif in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#How_can_we_use_elif_in_combination_with_other_flow_control_structures_in_Python_such_as_for_and_while\" title=\"How can we use elif in combination with other flow control structures in Python, such as for and while?\">How can we use elif in combination with other flow control structures in Python, such as for and while?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#How_can_we_use_elif_in_conjunction_with_sorting_and_sorting_functions_in_Python\" title=\"How can we use elif in conjunction with sorting and sorting functions in Python?\">How can we use elif in conjunction with sorting and sorting functions in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.copahost.com\/blog\/elif-python\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The syntax of&nbsp;&nbsp;<strong><code>elif<\/code>&nbsp;<\/strong>in Python is very similar to the syntax of&nbsp;&nbsp;<code><strong>if<\/strong><\/code>, with some important differences.&nbsp;Here&#8217;s an example of how to use o&nbsp;&nbsp;<strong><code>elif<\/code>&nbsp;<\/strong>in Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if condition1:\n    # block of code to execute if condition1 is true\n    \nelif condition2:\n    # block of code to execute if condition1 is false and condition2 is true\n    \nelif condition3:\n    # block of code to execute if condition1 and condition2 are false and condition3 is true\n    \nelse:\n    # block of code to be executed if all previous conditions are false<\/code><\/pre>\n\n\n\n<p>Note that the&nbsp;&nbsp;<code>elif<\/code>&nbsp;is used to test whether a condition is false and then execute a specific block of code.&nbsp;Each block of code that is associated with one&nbsp;&nbsp;<code>elif<\/code>&nbsp;is executed only if the corresponding condition is true.&nbsp;If all the previous conditions are false, the do code block&nbsp;&nbsp;<code>else<\/code>&nbsp;is executed.<\/p>\n\n\n\n<p>Furthermore, it is possible that we are using multiple&nbsp;&nbsp;<code>elif<\/code>&nbsp;analogues in a single flow control structure, as in the example above, where there are three&nbsp;&nbsp;<code>elif<\/code>&nbsp;different ones.&nbsp;This allows us to test multiple conditions and run different blocks of code for each of them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_is_elif_different_from_if_in_Python\"><\/span>How is elif different from if in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Although the syntax of do&nbsp;&nbsp;<code>elif<\/code>&nbsp;and do&nbsp;&nbsp;<code>if<\/code>&nbsp;in Python are similar, there are some important differences between them.&nbsp;Here are some of the main differences between&nbsp;&nbsp;<code>elif<\/code>&nbsp;and&nbsp;&nbsp;<code>if<\/code>&nbsp;in Python:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The&nbsp;&nbsp;<code>if<\/code>&nbsp;is used to test whether a condition is true and execute a block of code if it is true.&nbsp;It is already&nbsp;&nbsp;<code>elif<\/code>&nbsp;used to test if a condition is false and execute a block of code if it is false.&nbsp;See this example below:<\/li>\n\n\n\n<li>The&nbsp;&nbsp;<code>if<\/code>&nbsp;is used to test a single condition.&nbsp;We can already&nbsp;&nbsp;<code>elif<\/code> apply it to test multiple conditions in a single flow control structure.&nbsp;See this example below:<\/li>\n\n\n\n<li>We can apply it&nbsp;&nbsp;<code>if<\/code>&nbsp;alone in a flow control structure.&nbsp;We can only apply it&nbsp;&nbsp;<code>elif<\/code>&nbsp;in conjunction with&nbsp;&nbsp;<code>if<\/code>,&nbsp;&nbsp;<code>else<\/code>&nbsp;or in a nested flow control structure.<\/li>\n\n\n\n<li>When the&nbsp;&nbsp;<code>if<\/code>&nbsp;is used to test multiple conditions, we need it nested within the if itself.&nbsp;We can use it&nbsp;&nbsp;<code>elif<\/code>&nbsp;in conjunction with&nbsp;&nbsp;<code>if<\/code>&nbsp;or&nbsp;&nbsp;<code>else<\/code>&nbsp;to create a simpler flow control structure.<\/li>\n<\/ol>\n\n\n\n<p>In summary, although o&nbsp;&nbsp;<code>elif<\/code>&nbsp;and o&nbsp;&nbsp;<code>if<\/code>&nbsp;in Python have similar syntax, o&nbsp;&nbsp;<code>elif<\/code>&nbsp;is used to test false conditions, while o&nbsp;&nbsp;<code>if<\/code>&nbsp;is used to test true conditions.&nbsp;We can also apply&nbsp;&nbsp;<code>elif<\/code>&nbsp;to test multiple conditions in a single flow control structure, which makes the code cleaner and more organized.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Examples_of_how_to_use_elif_in_Python\"><\/span>Examples of how to use elif in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now let&#8217;s learn how to use elif with some practical examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Simple_examples_of_how_to_use_it_elif_in_Python\"><\/span>Simple examples of how to use it&nbsp;&nbsp;<code>elif<\/code>&nbsp;in Python:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 1: Determine the day of the week<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>day_oftheweek = input(\"What day of the week is it?\")\n\nif day_of_week == \"Sunday\":\n    print(\"Happy Sunday!\")\nelif day_of_week == \"monday\":\n    print(\"Happy Monday!\")\nelif week_day == \"Tuesday\":\n    print(\"Happy Tuesday!\")\nelif week_day ==\"Wednesday\":\n    print(\"Happy Wednesday!\")\nelif week_day == \"Thursday\":\n    print(\"Happy Thursday!\")\nelif week_day == \"Friday\":\n    print(\"Happy Friday!\")\nelif day_of_week == \"saturday\":\n    print(\"Happy Saturday!\")\nelse:\n    print(\"Sorry, but this is not a valid day of the week.\")<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 2: Determine the state of a coin<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>currency = input(\"What is the currency state? \")\n\nif coin ==\"level\":\n    print(\"The coin is at a good level.\")\ncoin elif ==\"weak\":\n    print(\"The currency is weak.\")\ncoin elif ==\"medium\":\n    print(\"The coin is at a medium level.\")\ncoin elif ==\"strong\":\n    print(\"The currency is strong.\")\nelse:\n    print(\"Sorry, but this is not a valid currency state.\")<\/code><\/pre>\n\n\n\n<p>In these examples, we are using it&nbsp;&nbsp;<strong><code>elif<\/code>&nbsp;<\/strong>to check several different conditions.&nbsp;In each case, we are checking whether a given condition is true and then executing a corresponding block of code.&nbsp;If none of the conditions are true, the code uses the clause&nbsp;&nbsp;<strong><code>else<\/code>&nbsp;<\/strong>to execute an additional block of code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advanced_how-to_examples_elif_in_Python\"><\/span>Advanced how-to examples&nbsp;&nbsp;<code>elif<\/code>&nbsp;in Python:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Example usage of&nbsp;&nbsp;<code>elif<\/code>&nbsp;with&nbsp;&nbsp;<a href=\"https:\/\/www.copahost.com\/blog\/null-python\/\"><code>None<\/code><\/a> in Python:<\/h4>\n\n\n\n<p>In the example below, if the user enters \u201cYes\u201d, the code will display the message \u201cYes, Python is an easy programming language!\u201d.&nbsp;If the user enters \u201cNo\u201d, the code displays the message \u201cNo, Python may be difficult for some people.\u201d.&nbsp;But if the user enters&nbsp;&nbsp;<code>None<\/code>, the code will use the clause&nbsp;&nbsp;<code>elif<\/code>&nbsp;to check if the answer is&nbsp;&nbsp;<code>None<\/code>.&nbsp;If so, the code will display the message \u201cSorry, but you did not enter a valid answer.\u201d.&nbsp;See below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>answer = input(\"Do you think Python is an easy programming language? (Yes\/No) \")\n\nif answer ==\"Yes\":\n    print(\"Yes, Python is an easy programming language!\")\nelif answer ==\"No\":\n    print(\"No, Python can be difficult for some people.\")\nelif response is None:\n    print(\"Sorry, but you did not enter a valid answer.\")\nelse:\n    print(\"Sorry, I didn't understand your answer.\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Example usage of&nbsp;&nbsp;<code>elif<\/code>&nbsp;with&nbsp;<a href=\"https:\/\/www.copahost.com\/blog\/append-python\/\">Append<\/a>&nbsp;in Python:<\/h4>\n\n\n\n<p>In this example, we&#8217;re looping through a list of fruits and checking whether each fruit is green, ripe, or ripe and red.&nbsp;If the fruit is green, we print &#8220;Fruit is green&#8221; and ignore the append.&nbsp;If the fruit is ripe, we print \u201cFruit is ripe\u201d and add the fruit to the list&nbsp;&nbsp;<code>frutas<\/code>&nbsp;using&nbsp;&nbsp;<code>append<\/code>.&nbsp;And finally, if the fruit is ripe and red, we just print \u201cFruit is ripe\u201d and add the fruit to the list&nbsp;&nbsp;<code>frutas<\/code>&nbsp;using&nbsp;&nbsp;<code>append<\/code>.&nbsp;That way, at the end we print the list&nbsp;&nbsp;<code>frutas<\/code>, which contains the ripe fruits added by&nbsp;&nbsp;<code>append<\/code>&nbsp;during the loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#091;]\n\nfor fruit in &#091;'apple', 'orange', 'banana', 'strawberry']:\n    if fruit == 'apple':\n        print('Fruit is green')\n    elif fruit == 'orange':\n        print('Fruit is ripe')\n        fruits.append(fruit)\n    elif fruit == 'banana':\n        print('Fruit is ripe')\n        fruits.append(fruit)\n    elif fruit == 'strawberry':\n        print('Fruit is ripe')\n        fruits.append(fruit)\n\nprint(fruits)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Example usage of&nbsp;&nbsp;<code>elif<\/code>&nbsp;and a set(&nbsp;<code><a href=\"https:\/\/www.copahost.com\/blog\/set-python\/\">set<\/a><\/code>) in Python:<\/h4>\n\n\n\n<p>In this example,&nbsp;<a href=\"https:\/\/www.copahost.com\/blog\/python-range\/\">we generate a sequence<\/a>&nbsp;from 1 to 11 and then loop through the numbers 1 to 10, checking whether each number is even or odd, or belongs to the sequence {1, 2, 3, 4, 6, 7, 8, 9}.&nbsp;If we find the number 5, we print &#8220;The number is 5&#8221;.&nbsp;Thus, if the number is even and not in the sequence, we print \u201cThe number is even and does not belong to the sequence of numbers\u201d.&nbsp;If the number is even, we add that number to the sum called &#8216;number&#8217;.&nbsp;Likewise, if the number is odd, we also add that number to the sum called &#8216;number&#8217;.<\/p>\n\n\n\n<p>At the end, we print the sum&nbsp;&nbsp;<code>numero<\/code>.&nbsp;Note that the sum value assumes the value of the sum of all even numbers in the sequence {2, 4, 6, 8, 10}.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number = 10\n\nfor i in range(1, 11):\n    if i == 5:\n        print('The number is 5')\n    elif i not in set(&#091;1, 2, 3, 4, 6, 7, 8, 9]):\n        print(f'The number {i} is even and is not one of the numbers in the sequence')\n    elif i % 2 == 0:\n        print(f'The number {i} is even')\n        number += i\n    else:\n        print(f'The number {i} is odd')\n        number += i\n\nprint(f'The result is {number}')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_are_the_limitations_of_elif_in_Python\"><\/span>What are the limitations of elif in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Although&nbsp;&nbsp;<code>elif<\/code>&nbsp;is a powerful flow control framework in Python, there are some limitations that you should consider when using it.&nbsp;Here are some of the main limitations of&nbsp;&nbsp;<code>elif<\/code>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We are only going to apply it&nbsp;&nbsp;<code>elif<\/code> together with&nbsp;&nbsp;<code>if<\/code>&nbsp;and&nbsp;&nbsp;<code>else<\/code>.&nbsp;This means that it will not be applied in conjunction with other flow control structures such as&nbsp;&nbsp;<code>while<\/code>&nbsp;or&nbsp;&nbsp;<code>for<\/code>.<\/li>\n\n\n\n<li>If there are multiple conditions&nbsp;&nbsp;<code>elif<\/code>, they are tested in order from first to last.&nbsp;In that sense, if a condition&nbsp;&nbsp;<code>elif<\/code>&nbsp;is true, subsequent conditions&nbsp;&nbsp;<code>elif<\/code>&nbsp;are not tested, even if they are true.<\/li>\n\n\n\n<li>does&nbsp;&nbsp;<code>elif<\/code>&nbsp;not allow the use of&nbsp;&nbsp;<code>else<\/code>&nbsp;as a separate code block.&nbsp;That way, if a condition&nbsp;&nbsp;<code>elif<\/code>&nbsp;is true, the block of code&nbsp;&nbsp;<code>else<\/code>&nbsp;is not executed, even if all&nbsp;&nbsp;<code>elif<\/code>&nbsp;previous conditions are false.<\/li>\n\n\n\n<li>does&nbsp;&nbsp;<code>elif<\/code>&nbsp;not allow the use of&nbsp;&nbsp;<code>break<\/code>&nbsp;or&nbsp;&nbsp;<code>continue<\/code>&nbsp;within a block of code&nbsp;&nbsp;<code>elif<\/code>.&nbsp;Therefore, if a condition&nbsp;&nbsp;<code>elif<\/code>&nbsp;is true, the program will continue to execute the subsequent code block, even if there is a break condition within the code block&nbsp;&nbsp;<code>elif<\/code>.<\/li>\n\n\n\n<li>We apply the&nbsp;&nbsp;<code>elif<\/code>&nbsp;just in conjunction with an expression or a truth test.&nbsp;In this sense, this means that we cannot use it with a list of values, as in a&nbsp;&nbsp;<code>if<\/code>&nbsp;regular one, unless each value is tested individually in a separate expression.<\/li>\n<\/ol>\n\n\n\n<p>Although there are some limitations in using&nbsp;&nbsp;<code>elif<\/code>, it is still a powerful flow control framework in Python and we can apply it to create more complex and efficient algorithms.&nbsp;Using the combination of&nbsp;&nbsp;<code>if<\/code>,&nbsp;&nbsp;<code>elif<\/code>&nbsp;and&nbsp;&nbsp;<code>else<\/code>&nbsp;allows you to create multiple test conditions for your code while keeping control structures clear and uncluttered.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Avoid_common_mistakes_when_using_elif_in_Python\"><\/span>Avoid common mistakes when using elif in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The&nbsp;&nbsp;<code>elif<\/code>&nbsp;is a keyword that is used in conjunction with&nbsp;&nbsp;<code>if<\/code>&nbsp;and&nbsp;&nbsp;<code>else<\/code>&nbsp;to execute different blocks of code depending on which conditions are true.&nbsp;However, it is possible to make some common mistakes when using&nbsp;&nbsp;<code>elif<\/code>&nbsp;in Python.&nbsp;Here are some tips to avoid them:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Make sure that all conditions in a block&nbsp;&nbsp;<code>elif<\/code>&nbsp;are only true for a single value.&nbsp;That way, when a condition is true for multiple values, it will affect the outcome of all subsequent conditions in the block&nbsp;&nbsp;<code>elif<\/code>.<\/li>\n\n\n\n<li>We will not apply&nbsp;&nbsp;<code>elif<\/code>&nbsp;with&nbsp;&nbsp;<code><strong>continue<\/strong><\/code>.&nbsp;When we need to keep running code for just a single value in a list, we use simply&nbsp;&nbsp;<code>if<\/code>&nbsp;instead of&nbsp;&nbsp;<code>elif<\/code>.<\/li>\n\n\n\n<li>We will not apply&nbsp;&nbsp;<code>elif<\/code>&nbsp;in conjunction with&nbsp;&nbsp;<code>and<\/code>.&nbsp;Thus, if we need to test two conditions that must be true at the same time, use&nbsp;&nbsp;<code>and<\/code>&nbsp;instead of&nbsp;&nbsp;<code>elif<\/code>.<\/li>\n\n\n\n<li>We will not apply&nbsp;&nbsp;<code>elif<\/code>&nbsp;with&nbsp;&nbsp;<code>not<\/code>.&nbsp;When we need to negate a condition in a block&nbsp;&nbsp;<code>elif<\/code>, use it&nbsp;&nbsp;<code>not<\/code>&nbsp;before the condition instead of making a new statement with&nbsp;&nbsp;<code>if not<\/code>.<\/li>\n\n\n\n<li>We will not apply&nbsp;&nbsp;<code>elif<\/code>&nbsp;without&nbsp;&nbsp;<code>if<\/code>&nbsp;or&nbsp;&nbsp;<code>else<\/code>.&nbsp;Therefore, if there is no condition in a block&nbsp;&nbsp;<code>elif<\/code>, it will not be executed.&nbsp;Make sure you always include at least one condition in every block&nbsp;&nbsp;<code>elif<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>Here is a code example that illustrates these tips:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list = &#091;1, 2, 3, 4, 5]\n\nif len(list) == 0:\n    print(\"The list is empty\")\nelif len(list) == 1:\n    print(\"The list has one element\")\nelif len(list) == 2:\n    print(\"The list has two elements\")\nelif len(list) == 3:\n    print(\"The list has three elements\")\nelif len(list) == 4:\n    print(\"The list has four elements\")\nelif len(list)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_can_we_use_elif_in_combination_with_other_flow_control_structures_in_Python_such_as_for_and_while\"><\/span>How can we use elif in combination with other flow control structures in Python, such as for and while?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The&nbsp;&nbsp;<code>elif<\/code>&nbsp;is a flow control structure in Python that is used to conditionally execute a block of code when a given test is true.&nbsp;The&nbsp;&nbsp;<code>elif<\/code>&nbsp;is often used in conjunction with the&nbsp;&nbsp;<code>if<\/code>&nbsp;and the&nbsp;&nbsp;<code>else<\/code>&nbsp;to create various conditions in a flow control structure.<\/p>\n\n\n\n<p>Although o&nbsp;&nbsp;<code>elif<\/code>&nbsp;is often used in conjunction with o&nbsp;&nbsp;<code>if<\/code>&nbsp;and o&nbsp;&nbsp;<code>else<\/code>, we can also apply it in conjunction with other flow control structures in Python, such as&nbsp;&nbsp;<code>for<\/code>&nbsp;e&nbsp;&nbsp;<code>while<\/code>.&nbsp;Here are some examples:<\/p>\n\n\n\n<p>With&nbsp;&nbsp;<code>for<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grades = &#091;7, 8, 6, 9, 5, 10]\n\nfor note in notes:\n    if grade &lt; 6:\n        print('Very low grade')\n    elif note &lt; 8:\n        print('Low Grade')\n    elif grade &lt; 10:\n        print('Average grade')\n    else:\n        print('High grade')<\/code><\/pre>\n\n\n\n<p>In this example, we are traversing the list of notes&nbsp;&nbsp;<code>notas<\/code>&nbsp;using a&nbsp;<a href=\"https:\/\/www.copahost.com\/blog\/for-python\/\">loop&nbsp;<code>for<\/code><\/a>&nbsp;.&nbsp;For each iteration of the loop, we check the grade value and print a corresponding message based on the grade value.&nbsp;So if the grade is less than 6, we print \u201cVery low grade\u201d.&nbsp;Therefore, if the grade is between 6 and 8, inclusive, we print \u201cBasic grade\u201d.&nbsp;If the grade is between 8 and 10, inclusive, we print \u201cAverage grade\u201d.&nbsp;If the grade is greater than 10, we print \u201cHigh grade\u201d.<\/p>\n\n\n\n<p>With&nbsp;&nbsp;<code>while<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter = 0\n\nwhile counter &lt; 5:\n    if counter == 0:\n        print('Starting counting')\n    elif counter == 1:\n        print('Counting by 1')\n    elif counter == 2:\n        print('Counting by 2')\n    elif counter == 3:\n        print('Counting by 3')\n    elif counter == 4:\n        print('Counting by 4')\n    else:\n        print('Completing count')\n    counter += 1<\/code><\/pre>\n\n\n\n<p>In this example, we are running a&nbsp;<a href=\"https:\/\/www.copahost.com\/blog\/while-python\/\">loop&nbsp;<code>while<\/code><\/a>&nbsp;&nbsp;that continues to execute the code inside the loop as long as the counter&nbsp;&nbsp;<code>contador<\/code>&nbsp;is less than 5. So, for each iteration of the loop, we check the value of the counter and print a corresponding message.<\/p>\n\n\n\n<p>In general,&nbsp;&nbsp;<code>elif<\/code>&nbsp;is a powerful flow control framework, and we can use it in conjunction with other flow control frameworks in Python to create more complex and powerful algorithms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_can_we_use_elif_in_conjunction_with_sorting_and_sorting_functions_in_Python\"><\/span>How can we use elif in conjunction with sorting and sorting functions in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can use\u00a0\u00a0<code>elif<\/code> in conjunction with sorting and sorting functions in Python to perform different actions according to the value returned by these functions.\u00a0Here are some examples:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Sort a list in ascending and descending order:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>list = &#091;3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]\n\nif len(list) == 0:\n    print(\"The list is empty\")\nelif sorted(list):\n    print(\"The list is sorted in ascending order\")\nelif reversed(list):\n    print(\"The list is sorted in descending order\")\nelse:\n    print(\"The list is not sorted\")<\/code><\/pre>\n\n\n\n<p>In this example, the function&nbsp;&nbsp;<strong><code>sorted<\/code>&nbsp;<\/strong>is used to sort the list in ascending order.&nbsp;Thus, if we keep the sorted list, the system will display the message &#8216;The list is sorted in ascending order.&nbsp;But, if the list has no order. The system will show the message \u201cThe list is not sorted\u201d.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>Sort a list of integers into multiple ranges:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>list = &#091;0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nif len(list) == 0:\n    print(\"The list is empty\")\nelif list&#091;:5]:\n    print(\"The numbers from 0 to 4 are present in the list\")\nelif list&#091;5:]:\n    print(\"The numbers from 5 to 9 are present in the list\")\nelif list&#091;:10]:\n    print(\"The numbers from 0 to 9 are present in the list\")\nelse:\n    print(\"No number is present in the list\")<\/code><\/pre>\n\n\n\n<p>In this example, we split the list into ranges using the slicing operator.&nbsp;Thus, if the list contains numbers from 0 to 4. The system will display the message \u201cThe list contains numbers from 0 to 4\u201d.&nbsp;But, if the list has numbers from 5 to 9. The system will show the message \u201cThe list contains numbers from 5 to 9\u201d.&nbsp;And if we include all numbers from 0 to 9 in the list, the system will display the message \u201cThe list contains numbers from 0 to 9\u201d.<\/p>\n\n\n\n<p>In summary, we can use elif in various ways in conjunction with sorting and sorting functions in Python to perform different actions according to the value these functions return.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In conclusion, the&nbsp;&nbsp;<code>elif<\/code>&nbsp;is a powerful keyword in Python that allows you to create more efficient and neat conditional flow control structures.&nbsp;Allowing you to test multiple conditions in a single flow control structure, which makes the code cleaner and easier to understand.<\/p>\n\n\n\n<p>Also,&nbsp;&nbsp;<code>elif<\/code>&nbsp;is very useful when we need to execute different blocks of code based on various conditions.&nbsp;This structure allows us to perform different conditions and execute different blocks of code for each of them.<\/p>\n\n\n\n<p>Although\u00a0\u00a0<code>elif<\/code>\u00a0and\u00a0\u00a0<code>if<\/code>\u00a0in Python have some important differences, both are fundamental to creating effective, well-structured Python programs.\u00a0So, when we start to learn Python or want to improve our programming skills. It is important to understand how we can use and\u00a0\u00a0<code>elif<\/code>\u00a0correctly\u00a0\u00a0<code>if<\/code>\u00a0 and <a href=\"https:\/\/www.copahost.com\/blog\/if-else-python\/\">if else<\/a> in our programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the Python language, Elif creates conditional flow control structures in a program. This structure is very similar to the keyword if, but with an important difference: while if we test a condition is true or false, elif we use it to test a condition is false and then execute a specific block of code. [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":3675,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[174],"tags":[],"class_list":["post-3666","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Elif Python: Elif conditionals in Python - Copahost<\/title>\n<meta name=\"description\" content=\"Learn now how to use elif in Python to create more efficient and neat conditional flow controls in your code.\" \/>\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.copahost.com\/blog\/elif-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Elif Python: Elif conditionals in Python - Copahost\" \/>\n<meta property=\"og:description\" content=\"Learn now how to use elif in Python to create more efficient and neat conditional flow controls in your code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.copahost.com\/blog\/elif-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Copahost\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-29T11:54:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-02T13:37:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1077\" \/>\n\t<meta property=\"og:image:height\" content=\"707\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Schenia T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Schenia T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/\"},\"author\":{\"name\":\"Schenia T\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\"},\"headline\":\"Elif Python: Elif conditionals in Python\",\"datePublished\":\"2023-08-29T11:54:14+00:00\",\"dateModified\":\"2023-10-02T13:37:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/\"},\"wordCount\":2341,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.copahost.com\/blog\/elif-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/elif-python\/\",\"name\":\"Elif Python: Elif conditionals in Python - Copahost\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png\",\"datePublished\":\"2023-08-29T11:54:14+00:00\",\"dateModified\":\"2023-10-02T13:37:50+00:00\",\"description\":\"Learn now how to use elif in Python to create more efficient and neat conditional flow controls in your code.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.copahost.com\/blog\/elif-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png\",\"width\":1077,\"height\":707,\"caption\":\"elif python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.copahost.com\/blog\/elif-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.copahost.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Elif Python: Elif conditionals in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"name\":\"Copahost\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.copahost.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\",\"name\":\"Copahost\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"width\":223,\"height\":40,\"caption\":\"Copahost\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\",\"name\":\"Schenia T\",\"description\":\"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!\",\"url\":\"https:\/\/www.copahost.com\/blog\/author\/schenia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Elif Python: Elif conditionals in Python - Copahost","description":"Learn now how to use elif in Python to create more efficient and neat conditional flow controls in your code.","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.copahost.com\/blog\/elif-python\/","og_locale":"en_US","og_type":"article","og_title":"Elif Python: Elif conditionals in Python - Copahost","og_description":"Learn now how to use elif in Python to create more efficient and neat conditional flow controls in your code.","og_url":"https:\/\/www.copahost.com\/blog\/elif-python\/","og_site_name":"Copahost","article_published_time":"2023-08-29T11:54:14+00:00","article_modified_time":"2023-10-02T13:37:50+00:00","og_image":[{"width":1077,"height":707,"url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png","type":"image\/png"}],"author":"Schenia T","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Schenia T","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.copahost.com\/blog\/elif-python\/#article","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/elif-python\/"},"author":{"name":"Schenia T","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f"},"headline":"Elif Python: Elif conditionals in Python","datePublished":"2023-08-29T11:54:14+00:00","dateModified":"2023-10-02T13:37:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/elif-python\/"},"wordCount":2341,"commentCount":0,"publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.copahost.com\/blog\/elif-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.copahost.com\/blog\/elif-python\/","url":"https:\/\/www.copahost.com\/blog\/elif-python\/","name":"Elif Python: Elif conditionals in Python - Copahost","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png","datePublished":"2023-08-29T11:54:14+00:00","dateModified":"2023-10-02T13:37:50+00:00","description":"Learn now how to use elif in Python to create more efficient and neat conditional flow controls in your code.","breadcrumb":{"@id":"https:\/\/www.copahost.com\/blog\/elif-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.copahost.com\/blog\/elif-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/elif-python\/#primaryimage","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/08\/elif-ingles.png","width":1077,"height":707,"caption":"elif python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.copahost.com\/blog\/elif-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.copahost.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Elif Python: Elif conditionals in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.copahost.com\/blog\/#website","url":"https:\/\/www.copahost.com\/blog\/","name":"Copahost","description":"","publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.copahost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.copahost.com\/blog\/#organization","name":"Copahost","url":"https:\/\/www.copahost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","width":223,"height":40,"caption":"Copahost"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f","name":"Schenia T","description":"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!","url":"https:\/\/www.copahost.com\/blog\/author\/schenia\/"}]}},"_links":{"self":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/comments?post=3666"}],"version-history":[{"count":5,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3666\/revisions"}],"predecessor-version":[{"id":3789,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3666\/revisions\/3789"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media\/3675"}],"wp:attachment":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media?parent=3666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/categories?post=3666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/tags?post=3666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}