python branch structure

python branch structure

1. Single branch

# 单分支

# if一般用于判断/选择的场景
# 90以上优秀
score = 95
if score > 90:
    print('优秀')

2. Double branch (1)

# if...else
# 90以上优秀,90一下良好
score = 95
if score > 90:
    print('优秀')
else:
    print('良好')

2. Double branch (2)

score = 75
print('优秀') if score > 90 else print('良好')  # 单分支没有,多分支也没有
# 结果一            条件              结果二

3. The multi-branch (1)

## if...elif...elif...else
## 90以上优秀,90-70良好,70以下不及格
# score = 95
# if score > 90:
#     print('优秀')
# elif score > 70:
#     print('良好')
# else:
#     print('及格')
#

3. The multi-branch (2)

score = 95
if score > 90:
    print('优秀')
if score > 70 and score < 90:
    print('良好')
if score < 60:
    print('及格')

4. Logical Operators

# 逻辑运算符

## > >= < <= == !=

## and 两者都必须成立
## or 其中一个成立即可
## not 非

Guess you like

Origin www.cnblogs.com/chenziqing/p/11200447.html