{"id":13334,"date":"2021-06-11T08:27:38","date_gmt":"2021-06-11T02:57:38","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=13334"},"modified":"2022-01-09T07:54:29","modified_gmt":"2022-01-09T02:24:29","slug":"match-case-python","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/match-case-python\/","title":{"rendered":"Match Case Python: New Addition to the Language"},"content":{"rendered":"\n<p>Till now, switch-case statements were not present in python. Using if-else statements and dictionary statements were some ways of implementing the switch case structure in python. But with that, the code wasn\u2019t manageable, and even the construction was tedious. That is when the python match case comes in handy. <\/p>\n\n\n\n<p><strong>With the introduction of Python 3.10, several new features were introduced, and one of them was &#8211; python match case. The Python match case is similar to the switch case statement, which is recognized as structural pattern matching in python.&nbsp; &nbsp;<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><span class=\"has-inline-color has-vivid-red-color\">NOTE: The match case structure is available for python version 3.10 and onwards.<\/span><\/strong><\/p><\/blockquote>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Switch_Case_Structure\" >Switch Case Structure<\/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.pythonpool.com\/match-case-python\/#Match_Case_Statement\" >Match Case Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Syntax_of_Match_Case_in_Python\" >Syntax of Match Case in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Example_of_a_Match_Case_Python_Statement\" >Example of a Match Case Python Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Match_Case_Python_for_Function_Overloading\" >Match Case Python for Function Overloading<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Pattern_Values\" >Pattern Values<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Wildcard_Pattern\" >Wildcard Pattern<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Multiple_pattern_values_using_OR_operator\" >Multiple pattern values using OR operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Checking_for_a_collection_of_values\" >Checking for a collection of values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Named_constants_as_patterns\" >Named constants as patterns<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#Inline_if_statement_in_match_case_format\" >Inline if statement in match case format<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#FAQs_on_match_case_python\" >FAQ&#8217;s on match case python<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-switch-case-structure\"><span class=\"ez-toc-section\" id=\"Switch_Case_Structure\"><\/span>Switch Case Structure<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The switch control block has similar functionality as that of an if-else ladder. However, writing multiple if statements are not the most effective way of doing so. Instead, by using the switch case statement, we can simply combine them into a single structure.<\/p>\n\n\n\n<p><strong>The switch case statement in a C\/C++ program would look something like this:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nswitch (variable to be evaluated):\n{\n     case value1 : \/\/statement 1\n                   break;\n\n     case value2 : \/\/statement 2\n                   break;\n\n     case value_n : \/\/statement n\n                    break;\n\n     default:  \/\/default statement\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-match-case-statement\"><span class=\"ez-toc-section\" id=\"Match_Case_Statement\"><\/span>Match Case Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To implement switch-case like characteristics and if-else functionalities, we use a <strong>match case in python. A match statement will compare a given variable\u2019s value to different shapes, also referred to as the pattern. <\/strong>The main idea is to keep on comparing the variable with all the present patterns until it fits into one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-syntax-of-match-case-in-python\"><span class=\"ez-toc-section\" id=\"Syntax_of_Match_Case_in_Python\"><\/span>Syntax of Match Case in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>The match case consists of three main entities :<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>The match keyword<\/strong><\/li><li><strong>One or more case clauses<\/strong><\/li><li><strong>Expression for each case<\/strong><\/li><\/ul>\n\n\n\n<p>The case clause consists of a pattern to be matched to the variable, a condition to be evaluated if the pattern matches, and a set of statements to be executed if the pattern matches. <\/p>\n\n\n\n<p>We can write multiple case statements for multiple possibilities for a given variable. Each case statement has a pattern that has to be matched.<\/p>\n\n\n\n<p><strong>The syntax looks something like this:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmatch variable_name:\n            case \u2018pattern1\u2019 : \/\/statement1\n            case \u2018pattern2\u2019 : \/\/statement2\n            \u2026            \n            case \u2018pattern n\u2019 : \/\/statement n\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-of-a-match-case-python-statement\"><span class=\"ez-toc-section\" id=\"Example_of_a_Match_Case_Python_Statement\"><\/span>Example of a Match Case Python Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Let us further understand the Match Case statement by taking a very simple example.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nquit_flag = False\nmatch quit_flag:\n    case True:\n        print(&quot;Quitting&quot;)\n        exit()\n    case False:\n        print(&quot;System is on&quot;) \n<\/pre><\/div>\n\n\n<p>Here, we have a variable named quit_flag which we have assigned a Boolean value of False by default. Thus, we have a match case that will compare the patterns with the <em>\u2018quit_flag\u2019 <\/em>variable. <\/p>\n\n\n\n<p>We have two cases for two possible values of quit_flag \u2013 True and False. For the first case, if the variable is True, then it will print &#8216;Quittting&#8217; and execute the exit() function to end the program. In case if it is false, it will just print a statement saying that <em>\u2018System is on.\u2019<\/em><\/p>\n\n\n\n<p><strong>When the quit_flag value if False, the match case will print <em>\u2018System is on\u2019<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"659\" height=\"265\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/image-26.png\" alt=\"match case python\" class=\"wp-image-13335\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/image-26.png 659w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/image-26-300x121.png 300w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<p><strong>If the quit variable is assigned the value<em> \u2018True\u2019<\/em>, then it will print <em>\u2018Quitting\u2019 <\/em>and end the program<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"605\" height=\"401\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/image-27.png\" alt=\"example match case python\" class=\"wp-image-13336\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/image-27.png 605w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/image-27-300x199.png 300w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/figure>\n\n\n\n<p>If we would have used if-else, then it would be a series of the if-else ladder. However, since this match case only has two cases, using if-else would not be too tedious. But what if instead of two cases, we had 20? Then the match case would look more readable and manageable than the if-else ladder.<\/p>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">Also Read |<\/span> <a href=\"http:\/\/www.pythonpool.com\/python-check-if-string-is-integer\/\" rel=\"noreferrer noopener\" target=\"_blank\"><span style=\"text-decoration: underline;\">5 Ways to Check if a String is Integer in Python<\/span><\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-match-case-python-for-function-overloading\"><span class=\"ez-toc-section\" id=\"Match_Case_Python_for_Function_Overloading\"><\/span>Match Case Python for Function Overloading<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can also use structural pattern matching for function overloading in python. With function overloading, we have multiple functions with the same name but with a different signature. <\/p>\n\n\n\n<p>Depending on the case condition, we can access the same function name with different signature values. With function overloading, we can improve the readability of the code. <\/p>\n\n\n\n<p>As of now, python doesn&#8217;t support function overloading. The following example can be used to verify it &#8211;<\/p>\n\n\n\n<p>Here, we shall be creating a user-defined function name <em>calc() <\/em>which accepts one argument. If the argument type is an integer, then we will return the square of the number. Else if the argument type is float, then we will return the cube of a number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef calc(n:int):\n\treturn (n**2)\n\ndef calc(n:float):\n\treturn (n**3)  \nn = 9.5\nis_int = isinstance(n, int)\n\nmatch is_int:\n\tcase True : print(&quot;Square is :&quot;,calc(n))\n\tcase False : print(&quot;Cube is:&quot;, calc(n))\n<\/pre><\/div>\n\n\n<p>Since the number n is a floating-point number, it will print the cube of the variable.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Cube is: 857.375<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nn = 10\nmatch is_int:\n\tcase True : print(&quot;Square is :&quot;,calc(n))\n\tcase False : print(&quot;Cube is:&quot;, calc(n))\n<\/pre><\/div>\n\n\n<p><strong>Output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Square is : 1000<\/pre>\n\n\n\n<p>Here you can check that the expected value was 100 while, 1000 was returned. That means function overloading is not supported.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-pattern-values\"><span class=\"ez-toc-section\" id=\"Pattern_Values\"><\/span>Pattern Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The match case in python allows a number, string,<em> \u2018None\u2019<\/em> value, <em>\u2018True\u2019<\/em> value, and <em>\u2018False\u2019 <\/em>value as the pattern. Thus, different types of patterns are possible, and we will be looking into some of the possible patterns. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-wildcard-pattern\"><span class=\"ez-toc-section\" id=\"Wildcard_Pattern\"><\/span>Wildcard Pattern<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can have a wildcard pattern too. A wildcard pattern is executed when none of the other pattern values are matched.<\/p>\n\n\n\n<p><strong>Following shows an example of a wildcard pattern.<\/strong><\/p>\n\n\n\n<p>We will take the same example of quit. But here, instead of passing a Boolean value to the variable, we will pass an integer value to the<strong> <em>\u2018quit_flag\u2019<\/em> <\/strong>variable.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nquit_flag = 4\n\nmatch quit_flag:\n    case True:\n        print(&quot;Quitting&quot;)\n        exit()\n    case False:\n        print(&quot;System is on&quot;)\n    case _:\n        print(&quot;Boolean Value was not passed&quot;)    \n\n<\/pre><\/div>\n\n\n<p><strong>The output will be:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Boolean Value was not passed<\/pre>\n\n\n\n<p>Here the underscore has been used as a wildcard pattern. It will not bind the \u2018quit_flag\u2019 variable\u2019s value to anything but match the variable\u2019s value to the statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-multiple-pattern-values-using-or-operator\"><span class=\"ez-toc-section\" id=\"Multiple_pattern_values_using_OR_operator\"><\/span>Multiple pattern values using OR operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can also have optional values for a given case. Using the OR operator, we can execute a single expression for multiple possibilities of pattern for the given variable.<\/p>\n\n\n\n<p>In the below code, we have a variable name<em> \u2018sample\u2019<\/em>. If the variable&#8217;s value is of Boolean type, then it will execute a common statement for both the conditions. Else, for <a href=\"http:\/\/www.pythonpool.com\/python-any\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">any<\/span> <\/a>other value, it will print <em>\u2018Not a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Boolean\">Boolean<\/a> value\u2019<\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsample = True\n\nmatch sample:\n\tcase (True|False):\n\t\tprint(&quot;It is a boolean value&quot;)\n\tcase _ :\n\t\tprint(&quot;Not a boolean value&quot;)\n\n<\/pre><\/div>\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">It is a boolean value<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-checking-for-a-collection-of-values\"><span class=\"ez-toc-section\" id=\"Checking_for_a_collection_of_values\"><\/span>Checking for a collection of values<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The pattern can also be a collection of values. It will match the pattern against the entire collection. Let us take a list<em> &#8216;list1&#8217;<\/em> as an example. We will take the entire list collection as the pattern.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist1 = &#x5B;'a', 'b', 'c', 'd']\n\nmatch list1:\n\tcase &#x5B;'e','f'] : print(&quot;e,f present&quot;)\n\tcase &#x5B;'a','b','c','d'] : print(&quot;a,b,c,d present&quot;)\n\n<\/pre><\/div>\n\n\n<p><strong>The output will be:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a,b,c,d present<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-named-constants-as-patterns\"><span class=\"ez-toc-section\" id=\"Named_constants_as_patterns\"><\/span>Named constants as patterns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can have a named constant as a pattern for match case statements. The constant should be a qualified name addressed by a dot operator. It works like a literal, but it never minds. <\/p>\n\n\n\n<p><strong>An example of named constant is:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass switch:\n\ton = 1\n\toff = 0\n\nstatus = 0\n\nmatch status:\n\tcase switch.on :\n\t\tprint('Switch is on')\n\tcase switch.off :\n\t\tprint('Switch is off')\n<\/pre><\/div>\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Switch is off<\/pre>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">Also Read |<\/span> <span style=\"text-decoration: underline;\"><a href=\"http:\/\/www.pythonpool.com\/python-exit\/\" rel=\"noreferrer noopener\" target=\"_blank\">4 Ways of Exiting the Program with Python Exit Function<\/a><\/span><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-inline-if-statement-in-match-case-format\"><span class=\"ez-toc-section\" id=\"Inline_if_statement_in_match_case_format\"><\/span>Inline if statement in match case format<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can also add an if statement to a pattern for a match case in python. That if the condition is also known as &#8216;guard&#8217;. The expression for a given pattern would be evaluated only if the guard is True. If the guard is False, it does not execute that pattern&#8217;s statement.<\/p>\n\n\n\n<p>Let us take an example for the same. We have a variable &#8216;n&#8217; will is assigned a numerical value. We have three cases here &#8211; n is negative, n is zero, and n is positive. The match case will check the guard and accordingly print the statement. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nn = 0\nmatch n:\n\tcase n if n &lt; 0:\n\t\tprint(&quot;Number is negative&quot;)\n\tcase n if n == 0:\n\t\tprint(&quot;Number is zero&quot;)\n\tcase n if n &gt; 0:\n\t\tprint(&quot;Number is positive&quot;)\n\n<\/pre><\/div>\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Number is zero<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faq-s-on-match-case-python\"><span class=\"ez-toc-section\" id=\"FAQs_on_match_case_python\"><\/span>FAQ&#8217;s on match case python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1623316216636\"><strong class=\"schema-faq-question\">Why does the python match case cause an invalid error?<br\/><\/strong> <p class=\"schema-faq-answer\">Implementing a python match case may cause an invalid error if you are using older versions of python. It is because the match case is present only for versions 3.10 and onwards<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1623339768723\"><strong class=\"schema-faq-question\">Why does python not have a switch till version 3.9?<br\/><\/strong> <p class=\"schema-faq-answer\">Python does not have switch-case statements before version 3.10 because there were no ways to include that in a manner blended with python&#8217;s syntax and coding pattern. There had been several proposals, but none were accepted.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1623339941707\"><strong class=\"schema-faq-question\">Which is better &#8211; match case or if-else statement?<br\/><\/strong> <p class=\"schema-faq-answer\">The reason why a match case should be preferred over if-else is that it is computationally faster when compared to an if-else ladder. Also, with a match case, the code becomes more readable and manageable. These advantages are more pre-dominant when the number of conditions is significantly large.<\/p> <\/div> <\/div>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p>Python match case is a new feature, and it will take some time for everyone to get adapted to it. But once you become used to it, it will definitely come in handy.<\/p>\n\n\n\n<p>That was all for Python Match Case. If you have any questions, leave them below in the comments. Stay Tuned for more such python content.<\/p>\n\n\n\n<p><em>Until then, Keep Learning!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Till now, switch-case statements were not present in python. Using if-else statements and dictionary statements were some ways of implementing the switch case structure in &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Match Case Python: New Addition to the Language\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/match-case-python\/#more-13334\" aria-label=\"More on Match Case Python: New Addition to the Language\">Read more<\/a><\/p>\n","protected":false},"author":20,"featured_media":13363,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4200,4201,4197,4199],"class_list":["post-13334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-python-3-10-match-case","tag-switch-case-alternative","tag-what-is-match-case","tag-whats-new-is-python-3-10","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Match Case Python: New Addition to the Language - Python Pool<\/title>\n<meta name=\"description\" content=\"A match case in python will compare a given variable\u2019s value to all the different patterns present until it fits into one.\" \/>\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.pythonpool.com\/match-case-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Match Case Python: New Addition to the Language\" \/>\n<meta property=\"og:description\" content=\"Till now, switch-case statements were not present in python. Using if-else statements and dictionary statements were some ways of implementing the switch\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/match-case-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-11T02:57:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-09T02:24:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dhruvi Vikma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dhruvi Vikma\" \/>\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.pythonpool.com\/match-case-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/\"},\"author\":{\"name\":\"Dhruvi Vikma\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\"},\"headline\":\"Match Case Python: New Addition to the Language\",\"datePublished\":\"2021-06-11T02:57:38+00:00\",\"dateModified\":\"2022-01-09T02:24:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/\"},\"wordCount\":1329,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg\",\"keywords\":[\"python 3.10 match case\",\"switch case alternative\",\"what is match case\",\"what&#039;s new is python 3.10\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/match-case-python\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/\",\"url\":\"https:\/\/www.pythonpool.com\/match-case-python\/\",\"name\":\"Match Case Python: New Addition to the Language - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg\",\"datePublished\":\"2021-06-11T02:57:38+00:00\",\"dateModified\":\"2022-01-09T02:24:29+00:00\",\"description\":\"A match case in python will compare a given variable\u2019s value to all the different patterns present until it fits into one.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623316216636\"},{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339768723\"},{\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339941707\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/match-case-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg\",\"width\":1350,\"height\":650,\"caption\":\"Match Case Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Match Case Python: New Addition to the Language\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\",\"name\":\"Dhruvi Vikma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"caption\":\"Dhruvi Vikma\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623316216636\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623316216636\",\"name\":\"Why does the python match case cause an invalid error?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Implementing a python match case may cause an invalid error if you are using older versions of python. It is because the match case is present only for versions 3.10 and onwards\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339768723\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339768723\",\"name\":\"Why does python not have a switch till version 3.9?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Python does not have switch-case statements before version 3.10 because there were no ways to include that in a manner blended with python's syntax and coding pattern. There had been several proposals, but none were accepted.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339941707\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339941707\",\"name\":\"Which is better - match case or if-else statement?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The reason why a match case should be preferred over if-else is that it is computationally faster when compared to an if-else ladder. Also, with a match case, the code becomes more readable and manageable. These advantages are more pre-dominant when the number of conditions is significantly large.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Match Case Python: New Addition to the Language - Python Pool","description":"A match case in python will compare a given variable\u2019s value to all the different patterns present until it fits into one.","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.pythonpool.com\/match-case-python\/","og_locale":"en_US","og_type":"article","og_title":"Match Case Python: New Addition to the Language","og_description":"Till now, switch-case statements were not present in python. Using if-else statements and dictionary statements were some ways of implementing the switch","og_url":"https:\/\/www.pythonpool.com\/match-case-python\/","og_site_name":"Python Pool","article_published_time":"2021-06-11T02:57:38+00:00","article_modified_time":"2022-01-09T02:24:29+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg","type":"image\/jpeg"}],"author":"Dhruvi Vikma","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Dhruvi Vikma","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/match-case-python\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/"},"author":{"name":"Dhruvi Vikma","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23"},"headline":"Match Case Python: New Addition to the Language","datePublished":"2021-06-11T02:57:38+00:00","dateModified":"2022-01-09T02:24:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/"},"wordCount":1329,"commentCount":6,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg","keywords":["python 3.10 match case","switch case alternative","what is match case","what&#039;s new is python 3.10"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/match-case-python\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/match-case-python\/","url":"https:\/\/www.pythonpool.com\/match-case-python\/","name":"Match Case Python: New Addition to the Language - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg","datePublished":"2021-06-11T02:57:38+00:00","dateModified":"2022-01-09T02:24:29+00:00","description":"A match case in python will compare a given variable\u2019s value to all the different patterns present until it fits into one.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623316216636"},{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339768723"},{"@id":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339941707"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/match-case-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/match-case-python\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Match-Case-Python-New-Addition-to-the-Language.jpg","width":1350,"height":650,"caption":"Match Case Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/match-case-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Match Case Python: New Addition to the Language"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23","name":"Dhruvi Vikma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","caption":"Dhruvi Vikma"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623316216636","position":1,"url":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623316216636","name":"Why does the python match case cause an invalid error?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Implementing a python match case may cause an invalid error if you are using older versions of python. It is because the match case is present only for versions 3.10 and onwards","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339768723","position":2,"url":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339768723","name":"Why does python not have a switch till version 3.9?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Python does not have switch-case statements before version 3.10 because there were no ways to include that in a manner blended with python's syntax and coding pattern. There had been several proposals, but none were accepted.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339941707","position":3,"url":"https:\/\/www.pythonpool.com\/match-case-python\/#faq-question-1623339941707","name":"Which is better - match case or if-else statement?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"The reason why a match case should be preferred over if-else is that it is computationally faster when compared to an if-else ladder. Also, with a match case, the code becomes more readable and manageable. These advantages are more pre-dominant when the number of conditions is significantly large.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=13334"}],"version-history":[{"count":26,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13334\/revisions"}],"predecessor-version":[{"id":19019,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13334\/revisions\/19019"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/13363"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=13334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=13334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=13334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}