Python Match-Case StatementIntroduction:In this tutorial, we are learning about the Match-Case Statement in Python. Python continues to be developed, and the match statement does not appear until Python < 3.10. The Python match statement was introduced in Python 3.10 and provides user-friendly, readable, and clean code. Something Python's famous elif else ladder statement does not do. Before version 3.10, Python did not have a variable function function like that found in C or C++. In Python 3.10, a pattern-matching method called match-case was introduced, which is similar to the switch-case function found in C/C++/Java, etc. What is a Match-Case Statement in Python?For developers in languages such as C/C++ or Java, there is an instruction called Switch Case. This Match-Case is Python's Switch Case, introduced in Python 3.10. Here, we need first to pass a parameter and then try to find which parameter is satisfied. If we find a match, we do one thing; if we do not find a match, then we do something else. The match statement starts with the match content, creates a block that accepts the index (here, its name is also a parameter), and then pulls various cases using keywords and patterns so that the pattern matches the parameter. The "_" is a wildcard and is escaped when there is no match. The pseudo code of the Match-Case Statement in Python is given below - Program Code 1: Here, we give a program code of the Match-Case Statement in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - February June April July Invalid number Program Code 2: Here, we give a program code of the Match-Case Statement in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - Please enter your username here: Priyanka Priyanka have accessed the database easily {'username': 'Priyanka', 'password': 'abc'} Please enter your username here: Rudra Rudra have not accessed the database easily Please enter your username here: a You do not have any accessed to the code Please enter your username here: Riya Riya have accessed the database easily {'username': 'Riya', 'password': 'abc'} Please enter your username here: _ You do not have any accessed to the code For match statements use the Python OR:The pipe operator is used in the match statement. Match terms are used only to match patterns and specific keywords. But you can use the operator for a or statement in the match statement. We simplified the code above by using the pipe operator in Python. Program Code: Here, we give a program code of the Match Statement using the Python OR. The code is given below - Output: Now, we run the above code and find the result. The result is given below - Please enter your username here: Riya You have accessed the database easily {'username': 'Riya', 'password': 'abc'} Please enter your username here: Priyanka You have not accessed the database easily Please enter your username here: Rudra You have not accessed the database easily Please enter your username here: a You do not have any accessed to the code Please enter your username here: _ You do not have any accessed to the code For match statements use the Python if:We can also use Python if the starting point condition is a model comparison expression. We pass to the guard condition when a case match (there must be a condition); if the condition statement changes to true, we select the block. Otherwise, we do not select the block. Program Code: Here, we give a program code of the Match Statement using the Python if. In this example, it checks the case for the match statement. If the case is Priyanka, it checks then for the users_dataBase list for Priyanka to exist in it. If she is there is the condition for the match statement is truth, then it will choose this block. After that, move to its part below and provide it access to the given database. The code is given below - Output: Now, we run the above code and find the result. The result is given below - Please enter your username here: Priyanka You have accessed the database easily {'username': 'Priyanka', 'password': 'abc'} Please enter your username here: _ You do not have any accessed the database easily Please enter your username here: Rudra You do not have any accessed the database easily The match sequence pattern in the match statement:Here, we will learn how to match the sequence in the match statement. This is very easy; just like matching strings and numbers, we can also use arguments to sort lists. The first case only matches names containing the single character "E". The second file matches the first character followed by * against other elements in the list. The * symbol is used to get all other values and other variables, and all other parameters are passed to the result. So, the first character matches a, and the rest are removed, and in the third, the last character is checked, s? Previous elements will be ignored or placed in first_element with different names. Program Code: Here, we give a program code to match the sequence in the match statement. The code is given below - Output: Now, we run the above code and find the result. The result is given below - The list is only containing E and it is a single list The 'E' is the first element in the list and ['a'] are the rest of the elements of this list No cases are matches The 's' is the last item of the list and ['b', 'h'] are the previous elements of this list For match statements use the Python class:We can also use Python class for match using the Python match statement. This will make the code very clean, uncomplicated, and, more importantly, easy to read. The first thought that comes to mind after seeing the matching word is that the Programmer and the Person in the matching word will create the status of their group, but this is not the case. It will check data with the given value. So, the case of the Programmer ("Priyanka," language = "Java," proficiency = "Advanced") does not create data. Still, it checks and then checks if the previous object named instance is actual data of the Programmer class. The instance named Object is actually an instance of the Programmer class. "Priyanka", the language is "Java" and the proficiency is "Advanced". This way, we can check the value in the data of each class. Program Code: Here, we give a program code of the Match Statement using the Python class. The code is given below - Output: Now, we run the above code and find the result. The result is given below - She is Priyanka and she is a Java programmer and her proficiency level is Advanced He is Rudra and he is a Python programmer She is Riya and her age is 28 and salary is 40000 He is a programmer, his name is Sneha, and he works in C and her proficiency level is Intermediate He is Shivam and his roll is 3 and age is 10 She is a student, her name is Nisha, and her roll is 5 and age is 18 For match statements use the Python dictionary:The first case here corresponds to the dictionary key and the key, and there is only one value and the dictionary. The second matches more than one key; the keys and values must be present in the dictionary. If there is an error or is a key that does not exist and does not match the dictionary and what is important, this will be thrown away. For the third part, we check the key name, language, and proficiency and save the changes with the same name created. You can easily change them, no problem; make them look like I'm using keys with the same name. Program Code: Here, we give a program code of the Match Statement using the Python dictionary. The code is given below - Output: Now, we run the above code and find the result. The result is given below - This only matches the key; i.e. blocks will be selected if their keys have a pair of keys. This matches multiple keys and values. The name of person is Rudra, and his known language is Python and his proficiency is Advanced. What are the advantages of the Match-Case statement?The advantages of the Match-Case statement in Python are given below -
Conclusion:In this tutorial, we are learning about the Match-Case Statement in Python. The match-case statement is also known as pattern matching. It is a special feature introduced in Python 3.10. It provides a simple and expressive way to create complex data structures such as tuples, lists, and arrays. It allows you to match the value of an expression to the structure of the structure and complete the matching process for the matching structure. Matching expressions are stronger and more meaningful than if-elif-else expressions. While the if-elif-else statement is based on Boolean expressions, the match-case statement can match patterns based on the pattern and value of the data. The match-case statements provide a simpler and more readable way to handle various situations and perform different tasks based on those situations. Next TopicPython string removeprefix method |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India