PYTHON INSTALLATION 3.10
Before tackling the “the pattern matching” topic, let’s talk about installing Python 3.10. Download the program from the python.org site and install it either in the folder that is automatically proposed to you, or you can decide on a different installation directory. Once the program is installed, configure the system environment variable Path by setting the path of the Python 3.10 installation directory. Restart your computer and everything should be fine. Open a Python schell to see if the current version is 3.10.
PATTERN MATCHING
The pattern matching is the main feature added in Python 3.10. The meaning in general terms is as follows: “Verification of the presence of a pattern within a composite object“. By pattern we mean a general model applied to a certain number of particular cases. Looking at the figure, it can be seen that the pattern matching process begins with a subject with an associated value that guides the entire matching process. It is the value we want to compare against a series of patterns.
We have created a variable that points to a list of elements, then activate the match using the list as the subject and introduce a first pattern to compare with the subject itself. First of all, it is checked whether the subject has a certain structure which in this case is composed of two elements p and s. This means that the pattern is successful if the subject consists of two elements as in the example. Since there is the matching, the code block following the case is applied. The patterns are evaluated one at a time from top to bottom and when one is successful in matching the subject his code is executed, while all the other patterns are discarded. Returning to our example, the successful matching causes the binding of the two elements found in the list which are then printed with a print. The variable p is associated with the first element, while the variable s is associated with the second. In summary, we can say that a pattern compared with a subject is able to return two types of results, a matching and a binding.
THE PATTERN MATCHING – CAPTURE PATTERNS
The square brackets used to define the pattern can be replaced with round brackets or omitted by simply using variables separated by commas.
The shape of the first pattern has two elements and therefore the match fails, the second pattern makes a match as we have a shape with three elements. Let’s now add a new item to the list.
In this case the first two patterns fail, we add another pattern consisting simply of an underscore. This pattern is called a wildcard. If none of the previous patterns have been successful, this wild card is triggered, which in fact is always successful. It must always be entered as the last case.
LITERAL PATTERNS
While previously as a pattern we always indicated variables that were used to perform an automatic binding, in this case we have expressed literals, that is, values in the code. The pattern is compared to the subject value by value and if the shape and values satisfy the subject the pattern is successful and then it is executed. Now let’s modify the program.
The pattern is satisfied when the subject is a sequence that contains the number 1 as the first element, all subsequent elements will be bound via a variable.
THE PATTERN MATCHING – OR PATTERNS
We have introduced two patterns separated by a Logical OR consisting of a literal and a variable. The literal is a comparison with the corresponding value in the subject while the variable msg is a binding that is successful in the case in which the literal corresponds to the first element of the list.
Since the patterns in or between them could be satisfied at the same time, the variable name msg must not change.
AS PATTERN
We specify a pattern that starts with a structural constraint and then with the as keyword we make a binding on a variable because we want to know if it is spring or summer.
CONDITIONAL PATTERNS
The conditional structure is an integral part of the pattern. The question we ask ourselves is: What happens if we specify “even” then the number is odd? The answer is that no matching is successful, and the program executes the wildcard branch.
Leave A Comment