两分支 If-Else
语句
1
2
3
4
if username == 'admin' and password == '123456':
print('身份验证成功!')
else:
print('身份验证失败!')
多分支 If-Elseif-Else
语句
1
2
3
4
5
6
7
x = float(input('x = '))
if x > 1:
y = 3 * x - 5
elif x >= -1:
y = x + 2
else:
y = 5 * x + 3
悬空Else分支
使用pass
命令执行“不执行任何操作”
1
2
3
4
5
x = float(input('x = '))
if x > 1:
print(x)
else
pass
Attention:尽量避免没有必要的嵌套,嵌套既影响可读性也增加bug出现概率
Attention: 同时注意,与Pascal不同,if和else语句末尾有冒号”:”
References
Author: Mike Lyou
Link: https://blog.mikelyou.com/2020/01/02/python-learning-03-branch-structure/
Liscense: Copyright Statement: This python learning series are posted only for personal studies and communication. The whole series are not allowed to be reproduced unles otherwise indicated. I do not own copyright of some of the content. If any post accidentally infringes your copyright, it will be removed shortly after being informed. View python-learning-readme for more information.