首页 > 其他分享 >CS50P: 1. Conditionals

CS50P: 1. Conditionals

时间:2024-07-10 10:12:30浏览次数:14  
标签:case elif name else CS50P print than Conditionals

运算符

python中有 >=<= ,其余和C一样

python支持 90 <= score <= 100

C Python
|| or
& and

布尔运算

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

标签:case,elif,name,else,CS50P,print,than,Conditionals
From: https://www.cnblogs.com/chasetsai/p/18293141

相关文章

  • task 5-Conditionals
    条件控制if语句Python中用elif代替了elseif,所以if语句的关键字为:if–elif–else。每个条件后面要使用冒号:,表示接下来是满足条件后要执行的语句块。使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。嵌套if语句中,可以把if..elif..else结构放在另......
  • x ? : y Conditionals with Omitted Operands
      https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Conditionals.html  6.8ConditionalswithOmittedOperandsThemiddleoperandinaconditionalexpress......