Python Programming for Beginners Part -14
(What is Match Case Statement in Python?)
Switch — case is one of the powerful and useful statement in high-level programming languages like C, C++, Java, and PHP etc. In this article, we are going to explore Pythonic way to implement switch- case that is called match — case statement in python 3.10. Let’s pull up your socks to understand match-case.
Python is closely matched to human logical style while writing the codes. Python is well known for ease of use and simple coding style of syntax in comparison with other programming languages like C, C++, Java and PHP and so on.
What is a Switch Statement?
In high level programming languages, a switch statement provides programming decision construction in which one of several branches is executed based on a comparison to a single value. Let’s see a pseudo-code example:
case ‘F’ :
#Best of luck and try again
In the above pseudo code, based on the values of grade the case statement that matches shall be executed. The break statement is used to exit from the swith case and control shall be transferred to next statement after the switch-case statement.
Prior to Python 3.10, python using long if/elif/else chains to meet switch-case alternatively that is a bit cumbersome as well as not pythonic. But recently released Python 3.10 offers structural patter matching that offers a significant alternative of switch-case.
Structural Pattern Matching
It is one of the most expected features of Python 3.10. The new version of python introduces a switch/case-like feature that is available in most of the programming languages like C, C++, and java and so on to Python. Moreover, Python 3.10 Structural Pattern Matching gives a variety of added features, creating it even more powerful than the typical switch conditional statement in other programming languages. Python uses the match keyword instead of switch with the case keyword defining each different scenario. The generic syntax of pattern matching is:
Here last case _: is the wildcard_ expression and if an exact match is not found, it will be applied as the matching case. If an exact match is not found and the code does not have a wildcard case, the entire match block is a no-op.
case ‘F’ :
print(“Best of luck and try again”)
Structural Pattern Matching with Guards
Guards allow us to follow the top pattern, an if statement that evaluates the subsequent case statement if the guard evaluates to true. For example:
case [b, r] if b > NUM_OF_BALLS and r > NUM_OF_RUNS:
print(“Scored runs in number of balls”)
case r if r > NUM_OF_BALLS:
print(“Strike rate is more than 100%”)
case [b, r] if b == r:
print(“Scored run a ball”)
Python 3.10, also allows to use a pipe “|” to combine multiple cases into a single case. It is treated as “OR” relationship. For example,
case ‘A’ | ‘B’ | ‘C’ | ‘D’:
print(“You are entitled to receive degree”)
case ‘F’ :
print(“Best of luck. Try again”)
In Python 3.10, other pattern matching features includes classes, literals and variables, and nested patterns that allow us to produce complex conditional statements.
But there are a few key differences between switch — case and match -case that are listed below:
· In Python the block is called match and not the switch.
· In Python, it is not required to use break statement with match -case.
· The default case is represented by an underscore. It is also called a wildcard match.
In nutshell, we can say that pattern matching feature provides an efficient way to implement switch-case like pattern matching based decision making in Python 3.10. It includes classes, literals and variables, and nested patterns that allow us to handle complex conditional statements that empowers python programmers to write code in pythonic way.
On wrapping up notes, feel free to share your comments. Your claps and comments will surely help me to present contents in a better way. See you next week.