CS50P week1 学习笔记

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")

代码流程示意图如下:

image-20240201164825712

上述代码需要询问三次判断语句,可以修改代码以简化:

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")

代码流程示意图如下:

image-20240201164952922

还有一个可以改进的地方。注意最后一个 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")

代码流程示意图如下:

image-20240201165356138

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")

image-20240202104541400

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")

image-20240202113038747

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

ifelifelse 语句类似,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

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值