forked from Hesenius/SafariPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditional.py
38 lines (34 loc) · 981 Bytes
/
conditional.py
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
x = 99
# indentation matters, must be consistent
# PEP8 must be four spaces :)
if x == 99:
print("oh look, it's 99!")
print("still in the if part")
elif x == 98: # elif avoids excessive indentation
print("it's 98")
else:
print("really, I thought it would be 99")
# if x == 99:
# print("oh look, it's 99!")
# print("still in the if part")
# else:
# if x == 98:
# print("it's 98")
# else:
# print("really, I thought it would be 99")
print("all done with conditions")
y = "ninety nine" if x == 99 else "something else"
print(y)
# new with Python 3.10
# kinda "switch/case" in many other languages
# STATEMENT, not expression
x = 101
match x:
case 99: print("it's 99")
case 100: print("odd, wasn't expecting that")
# case _: matches anything else
case _: print("something else entirely")
# match can do "destructuring" or "extraction"
x = ("One", "Un")
match x:
case (a, b): print(f"tuple contains {a} and {b}")