Skip to content

Commit e897fb6

Browse files
authored
updated match-case notebook
1 parent 71cc10b commit e897fb6

File tree

1 file changed

+128
-26
lines changed

1 file changed

+128
-26
lines changed

match statements.ipynb

Lines changed: 128 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
"[documentation](https://www.python.org/dev/peps/pep-0622/)"
1212
]
1313
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"#### Can use integer for match variable..."
19+
]
20+
},
1421
{
1522
"cell_type": "code",
1623
"execution_count": 1,
@@ -36,6 +43,103 @@
3643
" print('large')"
3744
]
3845
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"#### ...or floating point..."
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 2,
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"name": "stdout",
60+
"output_type": "stream",
61+
"text": [
62+
"large\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"var = 1.5\n",
68+
"\n",
69+
"match var:\n",
70+
" case 1.3:\n",
71+
" print('small')\n",
72+
" case 1.4:\n",
73+
" print('medium')\n",
74+
" case 1.5:\n",
75+
" print('large')"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"#### ...or Tuple...\n",
83+
"Note here we also use a variable to receive *any* value."
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 3,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
"on x-axis\n"
96+
]
97+
}
98+
],
99+
"source": [
100+
"var = (8,0)\n",
101+
"\n",
102+
"match var:\n",
103+
" case (0,x):\n",
104+
" print('on y-axis')\n",
105+
" case (x,0):\n",
106+
" print('on x-axis')\n",
107+
" case (x,y):\n",
108+
" print('not on axis')"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"metadata": {},
114+
"source": [
115+
"#### ...or String"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 4,
121+
"metadata": {},
122+
"outputs": [
123+
{
124+
"name": "stdout",
125+
"output_type": "stream",
126+
"text": [
127+
"small\n"
128+
]
129+
}
130+
],
131+
"source": [
132+
"var = \"S\"\n",
133+
"\n",
134+
"match var:\n",
135+
" case \"S\":\n",
136+
" print('small')\n",
137+
" case \"Med\":\n",
138+
" print('medium')\n",
139+
" case \"Lg\":\n",
140+
" print('large')"
141+
]
142+
},
39143
{
40144
"cell_type": "markdown",
41145
"metadata": {},
@@ -46,7 +150,7 @@
46150
},
47151
{
48152
"cell_type": "code",
49-
"execution_count": 2,
153+
"execution_count": 5,
50154
"metadata": {},
51155
"outputs": [
52156
{
@@ -74,12 +178,12 @@
74178
"metadata": {},
75179
"source": [
76180
"#### Conditionals in case \n",
77-
"if statements and or (using bar) are supported in case statements."
181+
"*if* statements and *or* (using bar) are supported in case statements."
78182
]
79183
},
80184
{
81185
"cell_type": "code",
82-
"execution_count": 3,
186+
"execution_count": 6,
83187
"metadata": {},
84188
"outputs": [
85189
{
@@ -112,7 +216,7 @@
112216
},
113217
{
114218
"cell_type": "code",
115-
"execution_count": 4,
219+
"execution_count": 7,
116220
"metadata": {},
117221
"outputs": [
118222
{
@@ -153,41 +257,39 @@
153257
},
154258
{
155259
"cell_type": "code",
156-
"execution_count": 5,
260+
"execution_count": 8,
157261
"metadata": {},
158262
"outputs": [
159263
{
160264
"name": "stdout",
161265
"output_type": "stream",
162266
"text": [
163-
"welcome to the business program!\n",
164-
"welcome to the science program!\n"
267+
"medium\n",
268+
"Size XL is not recognized.\n"
165269
]
166270
}
167271
],
168272
"source": [
169-
"class Student:\n",
170-
" def __init__(self, n, i, m):\n",
171-
" self.name = n\n",
172-
" self.id = i\n",
173-
" self.major = m\n",
273+
"class T_shirt:\n",
274+
" def __init__(self, s):\n",
275+
" self.size = s\n",
174276
"\n",
175-
"def welcome(student):\n",
176-
" match student.major:\n",
177-
" case 'engineering':\n",
178-
" print('welcome to the engineering program!')\n",
179-
" case 'business':\n",
180-
" print('welcome to the business program!')\n",
181-
" case 'pharmacy':\n",
182-
" print('welcome to the pharmacy program!')\n",
183-
" case x:\n",
184-
" print(f'welcome to the {x} program!')\n",
277+
" def order(self):\n",
278+
" match self.size:\n",
279+
" case 'S' | 'Sm':\n",
280+
" print('small')\n",
281+
" case 'M' | 'Med':\n",
282+
" print('medium')\n",
283+
" case 'L' | 'Lg':\n",
284+
" print('large')\n",
285+
" case x:\n",
286+
" print(f'Size {x} is not recognized.')\n",
185287
" \n",
186-
"new_student = Student('Suresh', 5723, 'business')\n",
187-
"welcome(new_student)\n",
288+
"shirt1 = T_shirt('Med')\n",
289+
"shirt1.order()\n",
188290
"\n",
189-
"new_student = Student('Britney', 5724, 'science')\n",
190-
"welcome(new_student)"
291+
"shirt2 = T_shirt('XL')\n",
292+
"shirt2.order()"
191293
]
192294
},
193295
{

0 commit comments

Comments
 (0)