【Python 1-10】Python手把手教程之——一篇讲透if语句以及if语句的特殊用法

2020.12.08 17:00 378浏览

if 简单示例

``````
car_list = ['bmw','audi','toyota','honda']

for car in car_list:

if car == 'bmw':

print(car.upper())

else:

print(car.title())

``````

``````
BMW

Audi

Toyota

Honda

``````

条件语句

检查是否相等

``````
car = 'bmw'

print(car == 'bmw')

True

``````

``````
car = 'audi'

print(car == 'bmw')

False

``````

使用if时不考虑大小写

``````
car = 'Audi'

print(car == 'audi')

False

``````

``````
car = 'Audi'

print(car.lower() == 'audi')

True

``````

``````
car = 'Audi'

print(car.lower() == 'audi')

True

print(car)

'Audi'

``````

检查不相等

``````
fruit = 'Apple'

if (fruit != 'Orange'):

print('do not need ' + fruit)

``````

`fruit`的值与`Orange`进行比较，如果它们不相等，Python 将返回`True`，进而执行紧跟在if语句后面的代码;如果这两个值相等，Python将返回`False`，因此 不执行紧跟在if语句后面的代码。

比较数字

``````
age = 18

print(age == 18)

True

``````

``````

``````

``````
age = 19

print(age < 21)

True

print(age <= 21)

True

print(age > 21)

False

print(age >= 21)

False

``````

检查多个条件

1. 使用and检查多个条件

``````
age_0 = 22

age_1 = 18

print(age_0 >= 21  and age_1 >= 21)

False

age_1 = 22

print(age_0 >= 21  and age_1 >= 21)

True

``````

``````
(age_0 >= 21) and (age_1 >= 21)

``````

2.使用or检查多个条件

``````
age_0 = 22

age_1 = 18

print(age_0 >= 21  or age_1 >= 21)

True

age_0 = 18

print(age_0 >= 21  or age_1 >= 21)

False

``````

检查特定值是否包含在列表中

``````
fruits = ['apple','banana','cherry']

print('apple'  in fruits)

True

print('orange'  in fruits)

False

``````

检查特定值是否不包含在列表中

``````
banned_users = ['andrew', 'carolina', 'david']

user = 'marie'

if user not  in banned_users:

print(user.title() + ", you can post a response if you wish.")

``````

`Marie, you can post a response if you wish.`

布尔表达式

``````
game_active = True

can_edit = False

``````

if语句

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!")

``````

if-elif-else 结构

1. 4岁以下免费;

2. 4-18岁收费5元;

3. 18岁(含z以上收费10元。

``````
age = 12

if age < 4:

elif age < 18:

else:

``````

``````

``````

``````
age = 12

if age < 4:

price = 0

elif age < 18:

price = 5

else:

price = 10

``````

使用多个elif代码块

``````
age = 12

if age < 4:

price = 0

elif age < 18:

price = 5

elif age < 65:

price = 10

else:

price = 5

``````

省略else代码块

Python并不要求`if-elif`结构后面必须有else代码块。在有些情况下，`else`代码块很有用,而在其他一些情况下，使用一条`elif`语句来处理特定的情形更清晰:

``````
age = 12

if age < 4:

price = 0

elif age < 18:

price = 5

elif age < 65:

price = 10

elif age >= 65:

price = 5

``````

`else`是一条包罗万象的语句，只要不满足任何`if``elif`中的条件判断，其中的代码就会执行，这可能会引入无效甚至恶意的数据。如果知道最终要的条件，应考虑使用一个`elif`代码块来代替`else`代码块。这样，你就可以肯定，仅当满足相应的条件时，你的代码才会执行。

判断多个条件

`if-elif-else`结构功能强大，但仅适合用于只有一个条件满足的情况，遇到通过了的判断后，Python就跳过余下的判断。这种行为很好，效率很高。

``````
requested_fruits = ['pear','banana']

if  'apple'  in requested_fruits:

if  'pear'  in requested_fruits:

if  'banana'  in requested_fruits:

``````

``````

``````

``````
requested_fruits = ['pear','banana']

if  'apple'  in requested_fruits:

elif  'pear'  in requested_fruits:

elif  'banana'  in requested_fruits:

``````

``````

``````

使用if语句处理列表

检查特殊元素

``````
requested_fruits = ['apple', 'pear', 'banana']

for fruit in requested_fruits:

print("Adding " + fruit + ".")

``````

``````

``````

``````
requested_fruits = ['apple', 'pear', 'banana']

for fruit in requested_fruits:

if fruit == 'apple':

print("Sorry, we are apple right now.")

else:

print("Adding " + fruit + ".")

``````

``````
Sorry, we are apple right now.

``````

确定列表不是空的

``````
requested_fruits = []

if requested_fruits:

for fruit in requested_fruits:

print("Adding " + fruit + ".")

else:

print("Are you sure you want some vegetables?")

``````

``````
Are you sure you want some vegetables?

``````

1人点赞

• 1
• 评论
• 收藏
• 共同学习，写下你的评论

0/150