Week 1 Conditionals
Conditionals
< | 小于 |
---|---|
> | 大于 |
<= | 小于等于 |
>= | 大于等于 |
== | 等于 |
!= | 不等 |
if Statements
在 python 中,使用 if 语句来进行判断。if
语句使用 bool
或者是布尔值 (true or false) 来决定是否要执行下面的语句。示例代码如下:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y: # 当这个语句为真时,才执行下面的语句
print("x is less than y")
Control Flow, elif, and else
可以将上述代码进行更改:
x = int(input("What's x? "))
y = int(input("What's y? "))
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")
代码流程示意图如下:
上述代码需要询问三次判断语句,可以修改代码以简化:
x = int(input("What's x? "))
y = int(input("What's y? "))
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")
代码流程示意图如下:
还有一个可以改进的地方。注意最后一个 elif x == y
并不是必需的,毕竟除了大于和小于这个数之外就只有等于了,可以修改代码如下:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x > y:
print("x is greater than y")
elif x < y:
print("x is less than y")
else:
print("x is equal to y")
代码流程示意图如下:
or
or
允许程序在多个方案之中做选择,只要有一个满足就为真。修改上述代码如下:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x > y or x < y:
print("x is not equal to y")
else:
print("x is equal to y")
可以对上述代码做进一步修改,变得更加简洁:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x != y:
print("x is not equal to y")
else:
print("x is equal to y")
或者这样写:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x == y:
print("x is equal to y")
else:
print("x is not equal to y")
and
and
要求条件全部满足才为真。新建代码 code grade.py
代码如下:
score = int(input("Score: "))
if score >= 90 and score <= 100:
print("Grade: A")
elif score >=80 and score < 90:
print("Grade: B")
elif score >=70 and score < 80:
print("Grade: C")
elif score >=60 and score < 70:
print("Grade: D")
else:
print("Grade: F")
可以修改上述代码,变得更加简洁:
score = int(input("Score: "))
if 90 <= score <= 100:
print("Grade: A")
elif 80 <= score < 90:
print("Grade: B")
elif 70 <= score < 80:
print("Grade: C")
elif 60 <= score < 70:
print("Grade: D")
else:
print("Grade: F")
可以更进一步:
score = int(input("Score: "))
if score >= 90:
print("Grade: A")
elif score >=80:
print("Grade: B")
elif score >=70:
print("Grade: C")
elif score >=60:
print("Grade: D")
else:
print("Grade: F")
这样写必须用 elif
,用 if
会出现问题
Modulo
编程中的模 %
运算符允许人们查看两个数字是否整除或相除后的余数,示例代码如下:
x = int(input("What's x? "))
if x % 2 == 0:
print("Even")
else:
print("Odd")
Creating Our Own Parity Function
def main():
x = int(input("What's x? "))
if is_even(x):
print("Even")
else:
print("Odd")
def is_even(n):
if n % 2 == 0:
return True
else:
return False
main()
Pythonic
pythonic
是一种只用在 python 语言的编程方式,可以将上述代码简化:
def is_even(n):
return True if n % 2 == 0 else False
我们可以注意到上述代码中 n % 2 == 0
返回的本来就是布尔类型的值,故可以修改上述代码:
def is_even(n):
return n % 2 == 0
match
与 if
、elif
和 else
语句类似,match
语句可用于有条件地运行与某些值匹配的代码。
在 house.py
里有如下代码:
name = input("What's your name? ")
if name == "Harry":
print("Gryffindor")
elif name == "Hermione":
print("Gryffindor")
elif name == "Ron":
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")
可以将上述代码变简洁,增加可读性:
name = input("What's your name? ")
if name == "Harry" or name == "Hermione" or name == "Ron":
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")
上面是使用 if
语句,还可以使用 match
语句达到相同的效果:
name = input("What's your name? ")
match name:
case "Harry" | "Hermione" | "Ron":
print("Gryffindor")
case "Draco":
print("Slytherin")
case _:
print("Who?")
match
语句中使用 |
代表 or
;最后一句中使用 case _
代表其它的所有输入,即 else