/
description.en.yml
110 lines (85 loc) · 4.04 KB
/
description.en.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
name: Match Operator
theory: |
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:
```python
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 else
```
The 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:
```python
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 else
```
In 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 by `match`.
* `case` constructs inside, where we describe the behavior for different values of the considered variable. Each `case` corresponds to an `if` in the example above. The `case _` is a special situation that corresponds to the `else` branch in conditional statements. Specifying `case _` is optional, similar to using `else`.
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:
```python
match count:
case 1:
# Do something useful
case 2:
# Do something useful
case _:
# Do something
```
Sometimes, 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:
```python
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 result
```
The second and simpler approach is to directly return from the function while working with `case`:
```python
def count_items(count):
match count:
case 1:
return 'one'
case 2:
return 'two'
case _:
return None
```
While 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 `get_number_explanation()`, which takes a number as input and returns an explanation for that number. If there is no explanation for the number, return `just a number`. Explanations are available only for the following numbers:
* 666 - devil number
* 42 - answer for everything
* 7 - prime number
Function call examples:
```python
get_number_explanation(8) # just a number
get_number_explanation(666) # devil number
get_number_explanation(42) # answer for everything
get_number_explanation(7) # prime number
```
tips:
- |
[Match](https://docs.python.org/3/reference/compound_stmts.html#the-match-statement)
definitions: []