Python: Match Operator
Many programming languages, in addition to the if conditional statement, include a switch construct. With the release of Python 3.10, a similar functionality was introduced called the match operator. In this lesson, we will explore this operator.
The match operator is a specialized version of if, designed for specific situations. For instance, it is useful when dealing with a chain of if else statements that check for equality:
if status == 'processing':
# Do something for 'processing'
elif status == 'paid':
# Do something for 'paid'
elif status == 'new':
# Do something for 'new'
else:
# Do something for everything elseThe distinguishing feature of this composite check is that each branch corresponds to a check on the variable status. The match operator allows us to express this code more succinctly:
match status:
case 'processing': # status == 'processing'
# Do something for 'processing'
case 'paid': # status == 'paid'
# Do something for 'paid'
case 'new': # status == 'new'
# Do something for 'new'
case _: # else
# Do something for everything elseIn terms of elements, the match operator is a complex construct. It consists of the following:
- The outer description, which includes the keyword
match. This represents the variable whose values will determine the behavior chosen bymatch. caseconstructs inside, where we describe the behavior for different values of the considered variable. Eachcasecorresponds to anifin the example above. Thecase _is a special situation that corresponds to theelsebranch in conditional statements. Specifyingcase _is optional, similar to usingelse.
Inside match, only the syntax shown above is permitted. In other words, we can use case. However, inside each case, the situation is different. Here, we can execute any arbitrary code:
match count:
case 1:
# Do something useful
case 2:
# Do something useful
case _:
# Do somethingSometimes, the result obtained inside a case leads to the end of the function containing the match. In such cases, it needs to be returned somehow. There are two ways to handle this:
The first approach involves creating a variable before the match, filling it in case, and then returning the value of that variable at the end:
def count_items(count):
# Declare a variable
result = ''
# Fill it
match count:
case 1:
result = 'one'
case 2:
result = 'two'
case _:
result = None
# Return it
return resultThe second and simpler approach is to directly return from the function while working with case:
def count_items(count):
match count:
case 1:
return 'one'
case 2:
return 'two'
case _:
return NoneWhile the match operator is not strictly necessary in Python, it offers the advantage of better expressing the programmer's intent when checking specific variable values. Though the code may grow slightly in size, it becomes more readable compared to using elif blocks.
Instructions
Implement the function calculate_delivery_cost(), which takes a delivery country and package weight in kilograms. The function should return the delivery cost.
Each country has two rates: one for packages up to and including 1 kg, and one for heavier packages:
'canada': 600 for packages up to 1 kg, 900 for the rest'usa': 800 for packages up to 1 kg, 1200 for the rest'germany': 700 for packages up to 1 kg, 1000 for the rest
If the country is unknown, the function should return None.
Function call examples:
calculate_delivery_cost('canada', 0.5) # 600
calculate_delivery_cost('canada', 2) # 900
calculate_delivery_cost('usa', 1) # 800
calculate_delivery_cost('france', 1) # NoneTips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import solution
def test1():
assert solution.calculate_delivery_cost("canada", 0.5) == 600
assert solution.calculate_delivery_cost("canada", 1) == 600
assert solution.calculate_delivery_cost("canada", 2) == 900
assert solution.calculate_delivery_cost("usa", 1) == 800
assert solution.calculate_delivery_cost("usa", 3) == 1200
assert solution.calculate_delivery_cost("germany", 0.3) == 700
assert solution.calculate_delivery_cost("germany", 1.5) == 1000
assert solution.calculate_delivery_cost("france", 1) is NoneTeacher's solution will be available in:
20:00
