Pythonの分岐構造

Pythonの分岐構造

1.シングルブランチ

# 单分支

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

2.ダブルブランチ(1)

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

2.ダブルブランチ(2)

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

3.マルチブランチ(1)

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

3.多分岐(2)

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

4.論理演算子

# 逻辑运算符

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

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

おすすめ

転載: www.cnblogs.com/chenziqing/p/11200447.html