pathon笔记——第5章 if语句

1、条件测试
(1)检查多个条件
   1)使用 and 检查多个条件 2)使用 or 检查多个条件
      注意:pathon里没有&&和||运算符,取而代之的是and和or
(2)检查特定值是否包含在列表中——使用关键字 in 
例:
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False
(3)检查特定值是否不包含在列表中——使用关键字 not in
例:
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
    print(user.title() + ", you can post a response if you wish.")
(4)布尔表达式
布尔表达式的结果要么为 True ,要么为 False 。   布尔值通常用于记录条件。

2、if 语句
(1)简单if语句
例:
age = 19
if age >= 18:
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")
如果测试通过了,将执行 if 语句后面所有缩进的代码行,否则将忽略它们。
在紧跟在 if 语句后面的代码块中,可根据需要包含任意数量的代码行。
(2) if-else 语句
如果需要在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作;在这种情况下,可使用 Python 提供的 if-else 语句
例:
age = 17
if age >= 18:
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")
else:
    print("Sorry, you are too young to vote.")
    print("Please register to vote as soon as you turn 18!")
(3)if-elif-else 结构
如果需要检查超过两个的情形,为此可使用 Python 提供的 if-elif-else 结构
例:
age = 12
if age < 4:
    print("Your admission cost is $0.")
elif age < 18:
    print("Your admission cost is $5.")
else:
    print("Your admission cost is $10.")
注:1)根据需要使用任意数量的 elif 代码块。
    2)if-elif 结构后面可以没有 else 代码块。
(4)测试多个条件
在这种情况下,应使用一系列不包含 elif 和 else 代码块的简单 if 语句。
例:
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
    print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
    print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
    print("Adding extra cheese.")
print("\nFinished making your pizza!")
注意:
如果像下面这样转而使用 if-elif-else 结构,代码将不能正确地运行,因为有一个测试通过后,就会跳过余下的测试:
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
elif 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
elif 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")

3、使用if语句处理列表
(1)检查特殊元素
检查列表中的特殊值,并对其做合适的处理。
例:
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
    if requested_topping == 'green peppers':
        print("Sorry, we are out of green peppers right now.")
    else:
        print("Adding " + requested_topping + ".")
    print("\nFinished making your pizza!")
解释:
如果比萨店的青椒用完了,这里在比萨中添加每种配料前都进行检查。
if处的代码检查顾客点的是否是青椒,如果是,就显示一条消息,指出不能点青椒的原因。
else处的代码确保其他配料都将添加到比萨中。
(2)确定列表不是空的
例:
requested_toppings = []
if requested_toppings:
    for requested_topping in requested_toppings:
        print("Adding " + requested_topping + ".")
    print("\nFinished making your pizza!")
else:
    print("Are you sure you want a plain pizza?")
解释:
在制作比萨前检查顾客点的配料列表是否为空。如果列表是空的,就向顾客确认他是否要点普通比萨;如果列表不为空,就像前面的示例那样制作比萨。
(3)使用多个列表
例:
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print("Adding " + requested_topping + ".")
    else:
        print("Sorry, we don't have " + requested_topping + ".")
    print("\nFinished making your pizza!")

5、if 语句的格式
PEP 8 提供的唯一建议是,在诸如 == 、 >= 和 <= 等比较运算符两边各添加一个空格,例如, if age < 4: 要比 if age<4: 好。
这样的空格不会影响 Python 对代码的解读,而只是让代码阅读起来更容易。

猜你喜欢

转载自blog.csdn.net/qq_40581789/article/details/83067630