⬅ 返回
1、分支
1.1、if-else分支
if 条件:
操作1
else:
操作2
age = input("请输入你的年龄:")
if int(age) >= 18:
print("你已经成年了")
else:
print("你还未成年")
1.2、if-elif分支
if 条件1:
操作1
elif 条件2:
操作2
elif 条件3:
操作3
if id==1:
print("hello world")
elif id==2:
print("goodbye world")
elif id==3:
print("welcome to my world")
1.3、if-elif-else分支
if 条件1:
操作1
elif 条件2:
操作2
else:
操作3
if id==1:
print("hello world")
elif id==2:
print("goodbye world")
else:
print("invalid id")
2、比较/逻辑运算符
| 运算符 | 符号 |
|---|
| 比较运算符 | ==、!=、>、<、>=、<= |
| 逻辑运算符 | and(与)、or(或)、not(非) |
| 三目运算(三元表达式) | 格式:操作1 if 条件 else 操作2 |
2.1、三目运算
为真结果 if 判断条件 else 为假结果
a,b=5,8
if(a<=b):
print("a <= b")
else:
print("a > b")
print("a<=b") if a<=b else print("a>b")