python 怎么用if语句(Python学习笔记if语句)

# _*_ coding:utf-8 _*_##########if语句############,今天小编就来聊一聊关于python 怎么用if语句?接下来我们就一起去研究一下吧!

python 怎么用if语句(Python学习笔记if语句)

python 怎么用if语句

# _*_ coding:utf-8 _*_

##########if语句############

#一个简单的例子

cars = ['audi', 'BMW', 'Subaru', 'toyota']

for car in cars:

if car == 'bmw':

print(car.upper())

else:

print(car.title())

-------------------------------------------

Audi

BMW

Subaru

Toyota

-------------------------------------------

#条件测试

answer = 17

if answer!= 42:

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

-------------------------------------------

That is not the correct answer.please try again!

-------------------------------------------

#检查多个条件

#使用and检查多个条件

age_0 = 22

age_1 = 18

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

age_1 = 21

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

-------------------------------------------

False

True

-------------------------------------------

#使用or检查多个条件

age_0 =22

age_1 = 18

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

age_0 = 18

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

-------------------------------------------

True

False

-------------------------------------------

#使用in检查特定值是否包含在列表中

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

print('mushrooms' in requested_toppings)

print('pepperoni' in requested_toppings)

-------------------------------------------

True

False

-------------------------------------------

#使用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.")

-------------------------------------------

Marie,you can post a response if you wish.

-------------------------------------------

#布尔表达式(布尔表达式的结果要么为True,要么为False)

game_active = True

can_edit = False

#if 语句

#简单的if语句

age = 19

if age >=18:

print("You are old enough to votel.")

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

-------------------------------------------

You are old enough to votel.

Have you registered to vote yet?

-------------------------------------------

#if-else 语句

age = 17

if age>= 18:

print("You are old enough to votel.")

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

-------------------------------------------

Sorry, you are too young to vote.

Please register to vote as soon as you turn 18!

-------------------------------------------

#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.")

-------------------------------------------

Your admission cost is $5.

-------------------------------------------

if age < 4:

price = 0

elif age < 18:

price = 5

else:

price = 10

print("Your admission cost is $" str(price) ".")

-------------------------------------------

Your admission cost is $5.

-------------------------------------------

#使用多个elif代码块

if age < 4:

price = 0

elif age < 18:

price = 5

elif age < 65:

price = 10

else:

price = 5

print("Your admission cost is $" str(price) ".")

-------------------------------------------

Your admission cost is $5.

-------------------------------------------

#省略else代码块

if age < 4:

price = 0

elif age < 18:

price = 5

elif age < 65:

price = 10

elif age >= 65:

price = 5

print("Your admission cost is $" str(price) ".")

-------------------------------------------

Your admission cost is $5.

-------------------------------------------

#测试多个条件

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

if 'mushrooms' in requested_toppings:

print("Adding mushrooms.")

if 'extra cheese' in requested_toppings:

print("Adding extra cheese.")

if 'pepperoni' in requested_toppings:

print("Adding pepperoni.")

print("\nFinished making your pizza!")

-------------------------------------------

Adding mushrooms.

Adding extra cheese.

Finished making your pizza!

-------------------------------------------

#使用if语句处理列表

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

-------------------------------------------

Adding mushrooms.

Sorry,we are out of green peppers right now.

Adding extra cheese.

Finished making your pizza!

-------------------------------------------

#确定列表不是空的

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

-------------------------------------------

Are you sure you want a plain pizza?

-------------------------------------------

#使用多个列表

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

-------------------------------------------

Adding mushrooms.

Sorry, we don't have french fries.

Adding extra cheese.

Finished making your pizza.

-------------------------------------------

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页