目录
一、第一种语法
-
if condition: statement1 statement2 statement3 ......
就相当于
if true: print() if false: print()
如果if后面的条件成立或者为true,才执行if中的语句,如果不成立,则不执行。
-
corn = 2000 prince = 1600 if corn >= prince: print("购买成功")
- 判断成绩等级
score = 82 if 85 <= score <= 100: print("A") if 75 <= score < 85: print("B")
二、第二种语法
- if condition:
statement1 else: statement2
如果条件成立,执行if中的语句,如果条件不成立或其他情况,则执行else里的语句。
-
corn = 1000 prince = 1600 if corn >= prince: print("购买成功") else: print("余额不足")
三、第三种语法
-
if condition1: statement1 elif condition2: statement2 elif condition3: statement3 ...... elif conditionN: statementN
elif=else if