Skip to content

CS50P: 1. Conditionals

运算符

python支持 90 <= score <= 100

C Python
|| or
&& and

布尔运算

True or False

选择语句

if

1
2
3
4
5
6
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 都会被执行

截屏2024-07-10 08.42.30

elif

1
2
3
4
5
6
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")

截屏2024-07-10 08.49.55

else

1
2
3
4
5
6
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")

截屏2024-07-10 08.55.01

match

类似 switch

原:

1
2
3
4
5
6
if name == "Harry" or name == "Hermione" or name == "Ron":      #字符串 ==
    print("Gryffindor")
elif name == "Draco":
    print("Slytherin")
else:
    print("Who?")

match:

1
2
3
4
5
6
7
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