加分练习
猜一猜 “if 语句” 是什么,他有什么作用。在做下一道题之前,试着用自己的话回答下面的问题:
你认为 if 对他下一行代码做了什么?
为什么 if 语句的下一行需要 4 个空格缩进?
如果不缩进,会发生什么事情?
把练习 27 中的其它布尔表达式放到 if 语句中会不会也可以运行呢?试一下。
如果把变量 people, cats 和 dogs 的初始值改掉,会发生什么事情?
people = 20
cats = 30
dogs = 15 if people < cats:
print("Too many cats! The world is doomed!") if people > cats:
print("Not many cats! The world is saved!") if people < dogs:
print("The world is drooled on!") if people > dogs:
print("The world is dry!") dogs += 5 if people >= dogs:
print("People are greater than or equal to dogs.") if people <= dogs:
print("People are less than or equal to dogs.") if people == dogs:
print("People are dogs.")
从打字上来说这一题挺简单的,不过重点在于理解 if 语句的使用。跑一下结果如下。
29.1 if 语句的作用
分析一下前四段 if 语句可以发现 if 语句的作用
if 语句会根据其中语句的布尔值(True、False)影响其下一行代码是否执行。
如果是真 (if something Ture),就执行下面的代码。否则不执行。
29.2 为什么 if 语句下面一行的代码需要 4 个空格? + 29.3 如果不缩进会怎样?
这和我们在函数里面遇到的情况一样,4 个空格表示了哪些代码属于此条 if 语句。
a = 1
b = 2
c = 3 if a < b:
print("这是第一行")
print("这是第二行")
if c < a:
print("这是第三行")
print("这是第四行")
print("这是第五行") print("-" * 10)
print("反过来条件试一下") if a > b:
print("这是第一行")
print("这是第二行")
if c > a:
print("这是第三行")
print("这是第四行")
print("这是第五行")
可以看到,没有缩进的第五行是不受 if 语句影响的,而在缩进中的部分是否执行则在于 if 语句的真伪。
29.4 把 27 题改 if 语句
print("Is 'not False' True?")
if not False:
print("Yes! is True!") print("\n------------------------")
print("Is 'not True' True?")
if not True:
print("Yes! is True!") print("\n------------------------")
print("Is 'True or True' True?")
if True or True:
print("Yes! is True!") print("\n------------------------")
print("Is 'True or False' True?")
if True or False:
print("Yes! is True!") print("\n------------------------")
print("Is 'False or True' True?")
if False or True:
print("Yes! is True!") print("\n------------------------")
print("Is 'False or False' True?")
if False or False:
print("Yes! is True!") print("\n------------------------")
print("Is 'True and True' True?")
if True and True:
print("Yes! is True!") print("\n------------------------")
print("Is 'True and False' True?")
if True and False:
print("Yes! is True!") print("\n------------------------")
print("Is 'False and True' True?")
if False and True:
print("Yes! is True!") print("\n------------------------")
print("Is 'False and False' True?")
if False and False:
print("Yes! is True!") print("\n------------------------")
print("Is 'not (True or True)' True?")
if not (True or True):
print("Yes! is True!") print("\n------------------------")
print("Is 'not (True or False)' True?")
if not (True or False):
print("Yes! is True!") print("\n------------------------")
print("Is 'not (False or True)' True?")
if not (False or True):
print("Yes! is True!") print("\n------------------------")
print("Is 'not (False or False)' True?")
if not (False or False):
print("Yes! is True!") print("\n------------------------")
print("Is 'not (True and True)' True?")
if not (True and True):
print("Yes! is True!") print("\n------------------------")
print("Is 'not (True and False)' True?")
if not (True and False):
print("Yes! is True!") print("\n------------------------")
print("Is 'not (False and True)' True?")
if not (False and True):
print("Yes! is True!") print("\n------------------------")
print("Is 'not (False and False)' True?")
if not (False and False):
print("Yes! is True!") print("\n------------------------")
print("Is '1 != 1' True?")
if 1 != 1:
print("Yes! is True!") print("\n------------------------")
print("Is '1 != 0' True?")
if 1 != 0:
print("Yes! is True!") print("\n------------------------")
print("Is '0 != 1' True?")
if 0 != 1:
print("Yes! is True!") print("\n------------------------")
print("Is '0 != 0' True?")
if 0 != 0:
print("Yes! is True!") print("\n------------------------")
print("Is '1 == 1' True?")
if 1 == 1:
print("Yes! is True!") print("\n------------------------")
print("Is '1 == 0' True?")
if 1 == 0:
print("Yes! is True!") print("\n------------------------")
print("Is '0 == 1' True?")
if 0 == 1:
print("Yes! is True!") print("\n------------------------")
print("Is '0 == 0' True?")
if 0 == 0:
print("Yes! is True!")