玩命加载中 . . .

python-if语句


​ 5.1 条件测试

In [1]:

cars = ['audi', 'bmw', 'subaru', 'toyota']

for car in cars:

    if car == 'bmw':

        print(car.upper())

    else:

        print(car.title())
Audi
BMW
Subaru
Toyota

5.1.1 检查是否相等

In [2]:

car = 'bmw'

car == 'bmw'

Out[2]:

True

In [3]:

car = 'audi'
car == 'bmw'

Out[3]:

False

5.1.2 检查是否相等时不考虑大小写

In [4]:

# 两个大小写不同的值 会被视为不想等
car = 'audi'
car == 'Audi'

Out[4]:

False

In [5]:

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

Out[5]:

'Audi'

5.1.3 检查是否不相等

In [6]:

requested_topping = 'mushrooms'

if requested_topping != 'anchovies':

    print("Hold the anchovies!")
Hold the anchovies!

5.1.4 比较数字

In [8]:

age = 18

age ==18

Out[8]:

True

In [9]:

answer = 17

if answer !=42:

    print("That is not the correct answer. Please try again.")
That is not the correct answer. Please try again.

5.1.5 使用多个条件检查

1.使用and检查多个条件

In [10]:

#两个条件同时满足
age_0 = 18

age_1 = 22

age_0 >= 21 and age_1 >= 21

age_0 = 21

age_0 >= 21 and age_1 >= 21

Out[10]:

True

2.使用or检查多个条件

In [11]:

#满足其中1个条件即可通过测试
#所有条件都没通过测试时,才会返回false
age_0 = 18

age_1 = 22

age_0 >= 21 or age_1 >= 21

Out[11]:

True

In [12]:

age_0 = 18

age_1 = 22

age_1 = 18

age_0 >= 21 or age_1 >= 21

Out[12]:

False

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

In [13]:

requested_toppings = ['mushroom', 'onions', 'pineapple']

'mushroom' in requested_toppings

Out[13]:

True

In [14]:

requested_toppings = ['mushroom', 'onions', 'pineapple']

'pipned' in requested_toppings

Out[15]:

False

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

In [16]:

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.

5.2 if语句

5.2.1 简单的if语句

In [17 ]:

if conditional_test:

    do something

In [18]:

age = 19

if age >= 18:

    print("You are age enough to vote !")
You are age enough to vote !

5.2.2 if-else语句

In [19]:

age = 17

if age >=18:

    print("You are age enough to vote !")

    print("Have you registered to vote yet ?")

else:

    print("Sorry, you have too young to vote !")

    print("Please register to vote as soon as you turn 18 .")
Sorry, you have too young to vote !

Please register to vote as soon as you turn 18 .

5.2.3 if-elif-else 语句

In [20]:

#根据年龄段收费的游乐场
#4岁以下免费
#4-18岁收费$5
#18岁以上收费$10
age=12

if age<4:

    print("Your adimission cost is $0.")

elif age <18:

    print("Your adimission cost is $5.")

else:

    print("Your adimission cost is $10.")
Your adimission cost is $5.

5.2.4 使用多个elif代码块

In [21]:

age = 12 

if age < 4:

    price = 0

elif age < 18:

    price = 5

elif age < 65:

    price = 10

else:

    price = 5

print("Your adimission cost is $" + str(price) + ".")
Your adimission cost is $5.

5.2.5 省略else代码块

In [22]:

age = 12 

if age < 4:

    price = 0

elif age < 18:

    price = 5

elif age < 65:

    price = 10

elif age >= 65:

    price = 5

print("Your adimission cost is $" + str(price) + ".")
Your adimission cost is $5.

5.2.6 测试多个条件

In [23]:

#如果顾客点了两种配料,就需要确保在其比萨中包含这些配料

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("\nFinsh making your pizza !")
Adding mushrooms .
Adding extra cheese .

Finsh making your pizza !

5.3.1 检查特殊元素

In [24]:

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:

    print("Adding " + requested_topping + ".")

print("\nFinsh making your pizza !")
Adding mushrooms.
Adding green peppers.
Adding extra cheese.

Finsh making your pizza !

In [25]:

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("\nFinsh making your pizza !")
Adding mushrooms.
Sorry,we are out of green peppers right now .
Adding extra cheese.

Finsh making your pizza !

5.3.2 确定列表不是空的

In [26]:

requested_toppings = []

if requested_toppings:

    for requested_topping in requested_toppings:

        print("Adding " + requested_topping + ".")

    print("\nFinsh making your pizza !")

else:

    print("Are you sure you wannt a plain pizza ?")
Are you sure you wannt a plain pizza ?

5.3.3 使用多个列表

In [27]:

available_toppings = ['mushrooms', 'olives', 'greenpeppers', '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("\nFinsh making your pizza !")
Adding mushrooms.
Sorry,we don't have french fries.
Adding extra cheese.

Finsh making your pizza !

文章作者: Edgar
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Edgar !
评论
  目录