python学习四 if语句

1.一个简单的示例

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

for car in cars:
	if car =='bmw':
		print(car.upper())
	else:
		print(car.title())

结果:

Audi
BMW
Subaru
Toyota

代码意义:
循环首先检查当前的汽车名是否为‘bmw’,如果是,则以大写方式输出;否则以首字母大写输出。

2.条件测试

每条if语句的核心都是一个值为True或Flase的表达式,这种表达是式称为条件测试。

2.1检查是否相等

一个等号‘=’解读为赋值,两个等号‘==’可以理解为发问,及“变量car的值是bmw吗?”
在这里插入图片描述

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

python中两个大小写不相同的值会被视为不相等:
在这里插入图片描述
如果大小写无关紧要,只想检查变量的值,可以转换一下大小写,这样比较时也不会改变存储在变量car中的值:
在这里插入图片描述

2.3检查是否不相等

不相等用“!=”表示

request_topping='mushroom'
if request_topping !='anchovies':
	print("Hold the anchovies!")

结果:

Hold the anchovies!

2.4比较数字

检查数值比较简单,在此不举例,主要有等于“==”,小于“<”,小于等于"<=”,大于“>”,大于等于“>=”

2.5检查多个条件

多个条件之间用and或者or连接;and需要两边的表达式都为真才成立,or只需要其中之一即可。
在这里插入图片描述

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

要判断特定的值是否包含在列表中,可以使用关键字in
在这里插入图片描述

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

类似于包含,可以使用该关键字not in

2.8布尔表达式

条件测试的别名
布尔操作符: and,or和 not

3.if语句

3.1简单的if语句

最简单的if语句只有一个测试和一个操作
例如:

age = 19
if age >= 18:
	print("You are old enough to vote!")

结果:

You are old enough to vote!

3.2 if-esle 语句

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

结果:

Sorry , you are too young to vote.
Please register to vote as soon as you turn 18!

3.3if-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.

3.4使用多个elif代码块

age = 28
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 $10.

3.5省略else代码块

python 并不要求if-elif结构后面必须有 else 代码块。else 是一条包罗万象的语句,只要不满足 if 或 elif 里的语句,其中的代码就会执行,有可能引入无效或恶意数据。

age = 12
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) + ".")

3.6测试多个条件

如果只想执行一个代码块,就使用 if - elif - if 结构,如果要运行多个代码块,就使用一系列独立的if语句

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

if 'mushrooms' in request_topping:
	print("Adding mushrooms.")
if 'pepperoni' in request_topping:
	print("Adding pepperoni.")
if 'extra cheese' in request_topping:
	print("Adding extra cheese.")

print("\nFinished making your pizza!")

结果:

Finished making your pizza!

4.使用if语句处理列表

4.1检查特殊元素

request_toppings=['mushrooms','green peppers','extra cheese']
for request_topping in request_toppings:
	if request_topping == 'green peppers':
		print("Sorry, we are out of green pepper right now.")
	else:
		print("Adding " + request_topping + ".")

print("\nFinished making your pizza!")

结果:

Adding mushrooms.
Sorry, we are out of green pepper right now.
Adding extra cheese.

Finished making your pizza!

4.2确定列表不是空的

request_toppings=[]
if request_toppings:
	for request_topping in request_toppings:
		print("Adding " + request_topping + ".")
	print("\nFinished making your pizza!")		
else:
	print("Are you sure you want a plain pizza?")

结果:

Are you sure you want a plain pizza?

4.3使用多个列表

available_topppings = ['mushrooms','olives','green peppers',
						'pepperoni','pineapple','extra cheese']
request_toppings=['mushrooms','french fries','extra cheese']

for request_topping in request_toppings:
	if request_topping in available_topppings:
		print("Adding " + request_topping + ".")
	else:
		print("Sorry, we don't have " + request_topping + ".")

print("\nFinished making your pizza!")

结果:

Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.

Finished making your pizza!
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页