You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cheatsheet/control-flow.md
+131
Original file line number
Diff line number
Diff line change
@@ -206,6 +206,137 @@ Ternary operators can be chained:
206
206
# output: teen
207
207
```
208
208
209
+
## Python Switch-Case Statements
210
+
211
+
<base-disclaimer>
212
+
<base-disclaimer-title>
213
+
Switch-Case statements
214
+
</base-disclaimer-title>
215
+
<base-disclaimer-content>
216
+
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
217
+
</base-disclaimer-content>
218
+
</base-disclaimer>
219
+
220
+
The _Switch-Case statements_, or **Structural Pattern Matching**, was firstly introduced in 2020 via [PEP 622](https://peps.python.org/pep-0622/), and then officially released with **Python 3.10** in September 2022.
221
+
222
+
<base-disclaimer>
223
+
<base-disclaimer-title>
224
+
Official Tutorial
225
+
</base-disclaimer-title>
226
+
<base-disclaimer-content>
227
+
The <ahref="https://peps.python.org/pep-0636/"target="_blank">PEP 636</a> provides an official tutorial for the Python Pattern matching or Switch-Case statements.
228
+
</base-disclaimer-content>
229
+
</base-disclaimer>
230
+
231
+
The [PEP 636](https://peps.python.org/pep-0636/) provides an official tutorial for the Pattern matching or Switch-Case statement.
232
+
233
+
### Matching single values
234
+
235
+
```python
236
+
>>> response_code =201
237
+
>>> match response_code:
238
+
... case 200:
239
+
...print("OK")
240
+
... case 201:
241
+
...print("Created")
242
+
... case 300:
243
+
...print("Multiple Choices")
244
+
... case 307:
245
+
...print("Temporary Redirect")
246
+
... case 404:
247
+
...print("404 Not Found")
248
+
... case 500:
249
+
...print("Internal Server Error")
250
+
... case 502:
251
+
...print("502 Bad Gateway")
252
+
...
253
+
# Created
254
+
```
255
+
256
+
### Matching with the or Pattern
257
+
258
+
In this example, the pipe character (`|` or `or`) allows python to return the same response for two or more cases.
259
+
260
+
```python
261
+
>>> response_code =502
262
+
>>> match response_code:
263
+
... case 200|201:
264
+
...print("OK")
265
+
... case 300|307:
266
+
...print("Redirect")
267
+
... case 400|401:
268
+
...print("Bad Request")
269
+
... case 500|502:
270
+
...print("Internal Server Error")
271
+
...
272
+
# Internal Server Error
273
+
```
274
+
275
+
### Matching by the length of an Iterable
276
+
277
+
```python
278
+
>>> today_responses = [200, 300, 404, 500]
279
+
>>> match today_responses:
280
+
... case [a]:
281
+
...print(f"One response today: {a}")
282
+
... case [a, b]:
283
+
...print(f"Two responses today: {a} and {b}")
284
+
... case [a, b, *rest]:
285
+
...print(f"All responses: {a}, {b}, {rest}")
286
+
...
287
+
# All responses: 200, 300, [404, 500]
288
+
```
289
+
290
+
### Default value
291
+
292
+
The underscore symbol (`_`) is used to define a default case:
293
+
294
+
```python
295
+
>>> response_code =800
296
+
>>> match response_code:
297
+
... case 200|201:
298
+
...print("OK")
299
+
... case 300|307:
300
+
...print("Redirect")
301
+
... case 400|401:
302
+
...print("Bad Request")
303
+
... case 500|502:
304
+
...print("Internal Server Error")
305
+
... case _:
306
+
...print("Invalid Code")
307
+
...
308
+
# Invalid Code
309
+
```
310
+
311
+
### Matching Builtin Classes
312
+
313
+
```python
314
+
>>> response_code ="300"
315
+
>>> match response_code:
316
+
... case int():
317
+
...print('Code is a number')
318
+
... case str():
319
+
...print('Code is a string')
320
+
... case _:
321
+
...print('Code is neither a string nor a number')
322
+
...
323
+
# Code is a string
324
+
```
325
+
326
+
### Guarding Match-Case Statements
327
+
328
+
```python
329
+
>>> response_code =300
330
+
>>> match response_code:
331
+
... case int():
332
+
...if response_code >99and response_code <500:
333
+
...print('Code is a valid number')
334
+
... case _:
335
+
...print('Code is an invalid number')
336
+
...
337
+
# Code is a string
338
+
```
339
+
209
340
## while Loop Statements
210
341
211
342
The while statement is used for repeated execution as long as an expression is `True`:
0 commit comments