{"id":7169,"date":"2024-03-19T06:44:06","date_gmt":"2024-03-19T06:44:06","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7169"},"modified":"2024-03-19T06:44:06","modified_gmt":"2024-03-19T06:44:06","slug":"python-match-case-statement","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-match-case-statement\/","title":{"rendered":"Python Match-Case Statement"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-a-python-match-case-statement\">What is a Python match-case Statement?<\/a><\/li><li ><a href=\"#combined-cases-in-python\">Combined Cases in Python<\/a><\/li><li ><a href=\"#list-as-the-argument-in-python\">List as the Argument in Python<\/a><\/li><li ><a href=\"#use-of-if-in-case-clause-in-python\">Use of &#8220;if&#8221; in the &#8220;Case&#8221; Clause in Python<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-match-case-statement-fa-qs\">Python Match-Case Statement-FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python match statement began in Python 3.10 and offers user experience, good readability, and cleanliness in the <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-work-with-unicode-in-python\/\" data-type=\"post\" data-id=\"6901\">code.<\/a> The pattern matching technique in <a href=\"https:\/\/www.skillvertex.com\/blog\/python-decision-making\/\" data-type=\"post\" data-id=\"7139\">Python <\/a>3.10 is known as match-case.<\/p>\n\n\n\n<p> This works similarly to the case construct.   Read this article to learn more about Python Match-Case Statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-python-match-case-statement\">What is a Python match-case Statement?<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.skillvertex.com\/blog\/conditional-or-ternary-operator-in-c\/\" data-type=\"post\" data-id=\"2262\">conditional statemen<\/a>t is referred to as the Switch Case. Further, the match case is the switch case of Python and was brought in  <a href=\"https:\/\/www.skillvertex.com\/blog\/python-history\/\" data-type=\"post\" data-id=\"6864\">Python 3.10.<\/a><\/p>\n\n\n\n<p>Therefore, the first pattern that is matched will be run. This also allows to extraction of components.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.skillvertex.com\/blog\/python-syntax\/\" data-type=\"post\" data-id=\"6924\">Syntax<\/a><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>match variable_name:\n   case 'pattern 1' : statement 1\n   case 'pattern 2' : statement 2\n   ...\n   case 'pattern n' : statement n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<p>The code that is provided below, consists of a<a href=\"https:\/\/www.skillvertex.com\/blog\/c-library-math-h-functions\/\" data-type=\"post\" data-id=\"2596\"> function <\/a>known as a weekday.  Further, it will receive an<a href=\"https:\/\/www.skillvertex.com\/blog\/generics-keyword-in-c\/\" data-type=\"post\" data-id=\"3708\"> integer <\/a>argument, then it needs to be matched with all the possible weekday number<a href=\"https:\/\/www.skillvertex.com\/blog\/how-can-i-return-multiple-values-from-a-function\/\" data-type=\"post\" data-id=\"2071\"> values<\/a> and then return with the corresponding name of the day.<\/p>\n\n\n<div class=\"gb-container gb-container-9b2cefc0\">\n\n<pre class=\"wp-block-code\"><code>\ndef weekday_action(day):\n    match day:\n        case \"Monday\":\n            print(\"It's the beginning of the week.\")\n        case \"Tuesday\":\n            print(\"It's still early in the week.\")\n        case \"Wednesday\":\n            print(\"It's the middle of the week.\")\n        case \"Thursday\":\n            print(\"It's almost Friday.\")\n        case \"Friday\":\n            print(\"It's the end of the workweek. TGIF!\")\n        case \"Saturday\":\n            print(\"It's the weekend!\")\n        case \"Sunday\":\n            print(\"It's the weekend, but it's almost over.\")\n        case _:\n            print(\"Invalid day entered.\")\n\n# Example usage:\nweekday_action(\"Wednesday\")\nweekday_action(\"Sunday\")\nweekday_action(\"InvalidDay\")\n<\/code><\/pre>\n\n<\/div>\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example usage:\nweekday_action(\"Wednesday\")\nweekday_action(\"Sunday\")\nweekday_action(\"InvalidDay\")\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"combined-cases-in-python\">Combined Cases in Python<\/h2>\n\n\n\n<p>It is possible to combine cases with the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-arithmetic-operators\/\" data-type=\"post\" data-id=\"6958\">OR Operator<\/a> and will be represented by the &#8220;|&#8221; symbol. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example cases\ncase_1 = True\ncase_2 = False\ncase_3 = True\n\n# Combine cases using the OR operator (|)\nresult = case_1 | case_2 | case_3\n\n# Display the result\nprint(\"Combined result:\", result)\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/what-will-be-the-output-of-the-following-program\/\" data-type=\"post\" data-id=\"3081\">Output<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Combined result: True\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"list-as-the-argument-in-python\">List as the Argument in Python<\/h2>\n\n\n\n<p>Python will match the <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-a-legal-expression-in-sql\/\" data-type=\"post\" data-id=\"1856\">expression<\/a> against any<a href=\"https:\/\/www.skillvertex.com\/blog\/python-literals\/\" data-type=\"post\" data-id=\"6953\"> literal<\/a> and use the list as a case value. Hence, it can divide the variable number of items in the list into the sequence with the &#8216;*&#8217; operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greeting(details):\n   match details:\n      case &#91;time, name]:\n         return f'Good {time} {name}!'\n      case &#91;time, *names]:\n         msg=''\n         for name in names:\n            msg+=f'Good {time} {name}!\\n'\n         return msg\n\nprint (greeting(&#91;\"Evening\", \"John\"]))\nprint (greeting(&#91;\"Morning\",\"Guest\"]))\nprint (greeting(&#91;\"Afternoon\", \"Albert\", \"Nick\", \"Rohan\"]))<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Good Evening John!\nGood Morning Guest!\nGood Afternoon Albert!\nGood Afternoon Nick!\nGood Afternoon Rohan!\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-of-if-in-case-clause-in-python\">Use of &#8220;if&#8221; in &#8220;Case&#8221; Clause in Python<\/h2>\n\n\n\n<p>In Python, there&#8217;s a<a href=\"https:\/\/www.skillvertex.com\/blog\/features-of-python\/\" data-type=\"post\" data-id=\"6868\"> feature<\/a> called match that helps us check different cases based on a value. Suppose, let&#8217;s assume it is a set of instructions for the <a href=\"https:\/\/www.skillvertex.com\/blog\/computer-science-engineering-salary\/\" data-type=\"post\" data-id=\"1598\">computer<\/a> to follow depending on what value we give it.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<p>Let us look at the <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-examples\/\" data-type=\"post\" data-id=\"4337\">example <\/a>of the Use of  &#8220;if&#8221; in a case clause in Python given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def process_data(value):\n    match value:\n        case 1:\n            print(\"Processing data for case 1\")\n        case 2 if value &gt; 0:\n            print(\"Processing data for case 2 with a positive value\")\n        case 2:\n            print(\"Processing data for case 2 with a non-positive value\")\n        case _:\n            print(\"Processing data for other cases\")\n\n# Example usage\nprocess_data(1)\nprocess_data(2)\nprocess_data(-1)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Processing data for case 1\nProcessing data for case 2 with a positive value\nProcessing data for case 2 with a non-positive value\nProcessing data for other cases<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>Python match case will provide the instruction to a computer according to the different situations. Therefore, the student can improve their knowledge and will be able to understand the topics clearly through the examples provided in the article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-match-case-statement-fa-qs\">Python Match-Case Statement-FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707999875748\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a match case statement in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. It is referred to as Pattern Matching and this feature was put in Python 3.10.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707999883735\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is match () in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The match function will analyze the string that needs to be matched for the required pattern in the RegEx Regular Expression, then will give the output as the first occurrence of such pattern match.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707999888777\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How does match case compare values in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The match statement will enable you to run the pattern matching on values.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python match statement began in Python 3.10 and offers user experience, good readability, and cleanliness in the code. The pattern matching technique in Python 3.10 is known as match-case. This works similarly to the case construct. Read this article to learn more about Python Match-Case Statements. What is a Python match-case Statement? The conditional statement &#8230; <a title=\"Python Match-Case Statement\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-match-case-statement\/\" aria-label=\"More on Python Match-Case Statement\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[913,72,57,866],"class_list":["post-7169","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-match-case-statement","tag-programming","tag-python","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7169"}],"version-history":[{"count":6,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7169\/revisions"}],"predecessor-version":[{"id":8265,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7169\/revisions\/8265"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7171"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}