上一讲我们学习了 if 语句,这一样我们将要学习 if else 语句。

基本使用 

if condition:
    true_expressions
else:
    false_expressions

当 if 判断条件为 True,执行 true_expressions 语句; 如果为 False,将执行 else 的内部的 false_expressions

实例 

x = 1
y = 2
z = 3
if x > y:
    print('x is greater than y')
else:
    print('x is less or equal to y')

在这个例子中,因为 x > y 将会返回 False, 那么将执行 else 的分支内容。输出 x is less or equal to y

x = 4
y = 2
z = 3
if x > y:
    print('x is greater than y')
else:
    print('x is less or equal y')

在这里,因为 condition 条件为 True, 那么将会输出 

更多推荐

python菜鸟教程 | if else 判断