Instead of writing long if statements or switch cases to handle different scenarios, you can just use patterns.
Here’s an example:
# This script uses pattern matching to handle different scenarios based on the value of x.
# The "match" keyword is used to start the pattern matching statement.
# The value of x will be compared to the different cases listed below.
match x:
# The "case" keyword is used to specify a specific case to match against.
# In this case, if x is equal to 'apple', the following code will be executed.
case 'apple':
# The "print" function is used to display a message to the user.
print('You picked an apple')
# If x is equal to 'orange', the following code will be executed.
case 'orange':
print('You picked an orange')
# The "_" symbol is used as a wildcard, meaning it will match any value.
# This is used as a default case, in case x does not match any of the specified cases.
case _:
print('You didn\'t pick anything')
In this code, we’re using the `match` statement to check if our variable `x` matches a certain pattern. If it does, we execute the corresponding block of code.
The first two cases are pretty straightforward they match specific strings (‘apple’ and ‘orange’). But what about that third case? The underscore (_) is called a “wildcard” or “catch-all” pattern. It matches any value (except for `None`). So if our variable doesn’t have an apple or orange in it, we print out a message saying so.
You can also use patterns to extract values from your input:
# The following script uses pattern matching to extract values from input and print corresponding messages.
# First, we define a variable x that contains a tuple of two values.
x = ('apple', 'red')
# Then, we use the "match" statement to check if the first value in the tuple is 'apple' and the second value is 'red'.
# If both conditions are met, we print a message saying "You picked a red apple".
match x:
case ('apple', 'red'): print('You picked a red apple')
# If the first value is not 'apple' or the second value is not 'red', we move on to the next case.
# Here, we use the wildcard (_) to match any value for the first element in the tuple.
# The second element is assigned to the variable "color".
# This allows us to print a message saying "You picked an {color} fruit" for any fruit with any color.
# The underscore (_) acts as a placeholder for any value that we are not interested in.
# This is useful when we want to extract specific values from a larger set of data.
case (_, color): print(f'You picked an {color} fruit')
In this example, we have two cases. The first one matches a tuple with the values ‘apple’ and ‘red’. If our input is that exact tuple, we print out a message saying so.
The second case uses a wildcard pattern to match any value for the first item in the tuple (we don’t care what it is). But then we use another pattern to extract the color of the fruit. This pattern matches anything except `None` and assigns it to the variable `color`. So if our input is (‘banana’, ‘yellow’), the second case will be executed, and we’ll print out a message saying “You picked an yellow fruit”.
Overall, patterns are pretty cool. They can make your code more concise and easier to read. But they also have some downsides like learning curve and surface area (which means there’s a lot of stuff to learn). And sometimes you might want to do things the old-fashioned way with if statements or switch cases instead.
But hey, at least we can still whack those moles!