Skip to content

Commit

Permalink
add case example
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Feb 13, 2025
1 parent 4cde5e1 commit 7ba31ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions python/basic2.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,13 @@ There is no is_int, we just need to try to convert and catch the exception, if t

In other languages this is the [?:](https://en.wikipedia.org/wiki/%3F:) construct.

## Case or Switch in Python
## Case or Switch in Python: match pattern matching
{id: case-switch}
{i: case}
{i: switch}
{i: match}

* There is no case or switch statement in Python.

![](examples/basics/switch.py)

## Exercise: Rectangle
{id: exercise-rectangle}
Expand Down
17 changes: 17 additions & 0 deletions python/examples/basics/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys

if len(sys.argv) != 2:
print("Usage: python switch.py <status_code>")
sys.exit(1)

status_code = int(sys.argv[1])

match status_code:
case 401 | 302:
print("401 or 302")
case 200:
print("200")
case 200:
print("200 again")
case _:
print("other")

0 comments on commit 7ba31ba

Please sign in to comment.