8000 Add match-case to Learning Python · utsahdevops/learning-python-UG@16e14c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 16e14c4

Browse files
committed
Add match-case to Learning Python
1 parent 019cea8 commit 16e14c4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Ch2 - Basics/conditionals_finished.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@ def main():
2121
result = "x is less than y" if (x < y) else "x is greater than or equal to y"
2222
print(result)
2323

24-
# Python does not have support for higher-order conditionals
25-
# like "switch-case" in other languages
26-
24+
# new in Python 3.10
25+
# the match-case construct can be used for multiple comparisons
26+
value = "one"
27+
match value:
28+
case "one":
29+
result = 1
30+
case "two":
31+
result = 2
32+
case "three" | "four":
33+
result = (3, 4)
34+
case _:
35+
result = -1
36+
print(result)
2737

2838
if __name__ == "__main__":
2939
main()

Ch2 - Basics/conditionals_start.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def main():
1212

1313
# conditional statements let you use "a if C else b"
1414

15+
# match-case makes it easy to compare multiple values
16+
1517

1618
if __name__ == "__main__":
1719
main()

0 commit comments

Comments
 (0)
0