除了常见的循环控制语句,Python 中还有 ifif else 和 if elif 等判断语句,本讲将简单介绍 if 语句

基本使用 

与其他编程语言中的 if 语句一样,使用方法如下

if condition:
    expressions

如果 condition 的值为 True,将会执行 expressions 语句的内容,否则将跳过该语句往下执行。

实例 

x = 1
y = 2
z = 3
if x < y:
    print('x is less than y')

上述代码中,if 语句的条件为 x < y 为 True, 那么将执行条件内部语句,程序将输出 x is less than y。 当我们将代码修改为一下

if x < y < z:
    print('x is less than y, and y is less than z')

在这里的条件变成了 x < y < z, 其相当于 x < y and y < z, 如果 and 两边的条件都为 True 那么才会返回 True

更多推荐

python菜鸟教程 | if 判断