{"id":3163,"date":"2022-11-04T20:38:44","date_gmt":"2022-11-04T20:38:44","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3163"},"modified":"2022-11-04T20:38:46","modified_gmt":"2022-11-04T20:38:46","slug":"python-structural-pattern-matching","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-structural-pattern-matching\/","title":{"rendered":"Python Structural Pattern Matching"},"content":{"rendered":"<p style=\"text-align: center;\">Python Structural Pattern Matching<\/p>\n<p>Now, we are going to learn about the concept of structural pattern matching. If you are familiar with some other programming language, like C, C++, or Java, then you might be familiar with something called switch case statements. In the simplest form, we can say that the structural pattern matching behaves like that switch case statements in other languages.<\/p>\n<p>So, we are now going to get into a brief introduction to structural pattern matching. But first of all, Let\u2019s have a look at the brief syntax that we are going to follow now.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3164 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON.jpg\" alt=\"\" width=\"1139\" height=\"246\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON.jpg 1139w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-300x65.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-1024x221.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-768x166.jpg 768w\" sizes=\"auto, (max-width: 1139px) 100vw, 1139px\" \/><\/p>\n<p>As you can see in the syntax above, we have the match keyword, and then we have the case keyword. The thing is that we are going to have some subject value following the match keyword, and we have some pattern following the case keyword. We can say that we are matching the pattern against the subject value.<\/p>\n<p>Let\u2019s have a look at a simple program, which gives a basic idea about using structural pattern matching in our programs. In the below program, we are going to take choice as an input from the user. It is basically going to be an integer, asking whether if you want milk, coffee, or tea. We would try to match the pattern according to the choice provided. Let\u2019s have a look at the below program now.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3165 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-1.jpg\" alt=\"\" width=\"1034\" height=\"219\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-1.jpg 1034w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-1-300x64.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-1-1024x217.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-1-768x163.jpg 768w\" sizes=\"auto, (max-width: 1034px) 100vw, 1034px\" \/><br \/>\nAs you can see in the above program, we have the basic program, to understand the structural pattern matching concept. We have taken the choice as a user input. Then we are trying to match the pattern against the choice. So, if the user enters 1, the first case matches, and then we are doing whatever specified. So, for a matching case, the code is written as shown in the above program. You can write more than one instruction as well.<\/p>\n<p>Let\u2019s try to execute the above program and have a look at the output, so that we can understand the concept.<\/p>\n<ol>\n<li>Milk<\/li>\n<li>Tea<\/li>\n<li>Coffee<\/li>\n<\/ol>\n<p>Please select your choice: 1<br \/>\nYou selected milk&#8230; here is your milk!<\/p>\n<p>As you can see in the output, we entered the choice as 1, and the matching case executed accordingly. Also, if you see in the program, the last case has an underscore. This is a wildcard, which simply means that we would do this, if no case matches. So, if we execute the same program again, and give some input that does not match any case. Let\u2019s try &#8211;<\/p>\n<ol>\n<li>Milk<\/li>\n<li>Tea<\/li>\n<li>Coffee<\/li>\n<\/ol>\n<p>Please select your choice: 5<br \/>\nSorry&#8230; this option is not recognized. only milk, tea and coffee is available!<\/p>\n<p>As you can see, we again executed the program and then put such a choice, which is not there. As the result, we get the wildcard case executed.<\/p>\n<p>Now, let\u2019s go more into different things, so that we can get more about what are some different things that we can do here. Let\u2019s have a look at one more program and let\u2019s try to come up with something that we can do here<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3166 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-2.jpg\" alt=\"\" width=\"1035\" height=\"244\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-2.jpg 1035w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-2-300x71.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-2-1024x241.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-2-768x181.jpg 768w\" sizes=\"auto, (max-width: 1035px) 100vw, 1035px\" \/><\/p>\n<p>As you can see in the above program, we have taken the status code as user input, and then we are matching the pattern against the given subject. In the above program, you can see that in one of the cases, we have used the &#8216;OR&#8217; thing. The &#8216;|&#8217; symbol in the patterns combines them as alternatives. We can call this as the OR pattern. So, as and when required, we can make use of this &#8216;|&#8217; symbol in our structural pattern matching.<\/p>\n<p>Now, there is something called as a guard as well, with which, we can add conditions to our case. At times, we would be required to so, that before executing the matched case, we would want to check some condition. This is like we are adding some constraints in addition to the pattern. So, a guard is an expression attached to a pattern, which must evaluate to True, for the pattern to succeed. Let\u2019s have a look at a simple program, which tries to demonstrate us, the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3167 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-3.jpg\" alt=\"\" width=\"1065\" height=\"162\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-3.jpg 1065w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-3-300x46.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-3-1024x156.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRUCTURAL-PATTERN-MATCHING-IN-PYTHON-3-768x117.jpg 768w\" sizes=\"auto, (max-width: 1065px) 100vw, 1065px\" \/><\/p>\n<p>In the above program, we have a simple scenario. Imagine that you visit an ATM machine, and at times, it may happen that you would be allowed to withdraw only the multiples of 500. So, the above program is a very simplified approach, in which we are just taking amount as user input, and then we are matching the pattern with subject. In the first case, we are matching the amount, and then we have a guard, which is a condition, which checks if the amount is divisible by 500. So, we have attached the guard to the pattern, which must evaluate to True, for the pattern to succeed.<br \/>\nSo, Let\u2019s try to execute the program and observe the output.<\/p>\n<p>Please enter amount to withdraw:1000<br \/>\nprocessing and verifying the withdrawal<\/p>\n<p>As you can see, the amount that we have entered is 1000, which is a multiple of 500, which is why the withdrawal is processed. On the other hand, Let\u2019s input some amount, which is not a multiple of 500.<\/p>\n<p>Please enter amount to withdraw:120<br \/>\nInvalid amount&#8230;only multiples of 500 are allowed! please try again!<\/p>\n<p>As you can see, the amount that we had input is not a multiple of 500, so the transaction is not allowed.<\/p>\n<p>So, in this way, we can make use of the structural pattern matching, as and when required in our programs. You can explore the concept in more depth and use as and when required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Structural Pattern Matching Now, we are going to learn about the concept of structural pattern matching. If you are familiar with some other programming language, like C, C++, or Java, then you might be familiar with something called switch case statements. In the simplest form, we can say that the structural pattern matching behaves [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3163","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3163"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3163\/revisions"}],"predecessor-version":[{"id":3970,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3163\/revisions\/3970"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}