CS50P: 1. Conditionals
运算符
python支持 90 <= score <= 100
布尔运算
True
or False
选择语句
if
| if x < y:
print("x is less than y")
if x > y:
print("x is greater than y")
if x == y:
print("x is equal to y")
|
:
....
四个缩进,表示只有当 if
为真,该语句才能被执行。有缩进是一个代码块
代码的逻辑:三个 if
都会被执行

elif
| if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")
|

else
| if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x is equal to y")
|

match
类似 switch
原:
| if name == "Harry" or name == "Hermione" or name == "Ron": #字符串 ==
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")
|
match:
| match name:
case "Harry" | "Hermione" | "Ron":
print("Gryffindor")
case "Draco":
print("Slytherin")
case _:
print("Who?")
|
case _:
表示else
|
在case中表示或
Pythonic Expressions
| def is_even(x):
return True if x % 2 == 0 else False
|
| def is_even(x):
return x % 2 == 0
|