Python_list(五)列表if进阶

/ Python / 没有评论 / 650浏览

检查是否相等时不考虑大小写 lower()

此处是把需要的字符转换成小写来对比(重点理解)

>>> car = 'BIGBEN'    
>>> car.lower() == 'bigben'
True

检查是否不相等 (!=)

示范

[root@pa1 lijinghua]#python fish.py
oh I like xioahuangyu
[root@pa1 lijinghua]#cat  fish.py
#!/usr/bin/python
fish='xiaohuangyu'
if fish != 'jingyu':
	print ("oh I like xioahuangyu")

检查特定值是否包含在列表中 (关键字 in)

示范

>>> name=['lili','yuxi','congcong']
>>> "lili" in name
True
>>> "jinghua" in name
False

检查特定值是否不包含在列表中(not in)

示范

 [root@pa1 lijinghua]#python not_in.py
Congcong,you can not !
[root@pa1 lijinghua]#cat  not_in.py
#!/usr/local/python
names=['lili','jinghua','yuxi']
user='congcong'
if user not in names:
	print(user.title() +",you can not !")

if语句进阶

在fi语句中,缩进的作用与for循环相同,如果测试通过了,将执行if语句后面所有缩进的代码行,否则将忽略他们

if-else语句

经常需要在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作,可以使用if-else语句

举个栗子

[root@pa1 lijinghua]#python if_else,py
print enter your age:12
She/He is teenager
[root@pa1 lijinghua]#cat if_else,py
#!/usr/bin/python
age = input('print enter your age:')
#print (name)
if age >= 18:
	print (r'She/He is adult')
else:
	print (r'She/He is teenager')

if-elif-else结构

经常需要检查超过两个的情形,为此可使用if-elif-else结构 Python只执行if-elif-else结构中的一个代码块,它依次检查每个条件测试,知道遇到通过了的条件测试,测试通过后,Python将执行紧跟在后面的代码,并跳过余下的测试

示范

[root@pa1 lijinghua]#python if_elif_else.py
print enter your age:2
She/He is kid
[root@pa1 lijinghua]#cat if_elif_else.py
	#!/usr/bin/python
age = input('print enter your age:')
#print (name)
if age >= 18:
	print (r'She/He is adult')
elif age >= 6:
	print (r'She/He is teenager')
else:
	print (r'She/He is kid' )

使用多个elif代码块

示范

[root@pa1 lijinghua]#python elifs.py
Your adimission cost is $5.
[root@pa1 lijinghua]#cat elifs.py
#!/usr/local/python
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语句来处理特定的情形更清晰

此处省略示例

测试多个条件

较原始

[root@pa1 lijinghua]#python ifs.py
lili is ok
jinghua is ok 
congcong is ok

your are good ~
[root@pa1 lijinghua]#cat ifs.py
#!/usr/local/python
names ={'lili','jinghua','congcong'}
if 'lili' in names:
	print ("lili is ok")
if 'jinghua' in names:
	print ("jinghua is ok ")
if 'congcong' in names :
	print ("congcong is ok")
print ( "\nyour are good ~")

使用if语句处理列表

示范1

[root@pa1 lijinghua]#python if_list.py
sorry,xigua not enouge now !
Adding lili.
Adding congcong.

Finished making your food!
[root@pa1 lijinghua]#cat if_list.py
#!/usr/local/python
names=['xigua','lili','congcong']
for name in names:
	if name == 'xigua':
		print("sorry,xigua not enouge now !")
	else:
		print("Adding "+ name + ".")
print ("\nFinished making your food!")

确定列表不是空的

假若不是空的
[root@pa1 lijinghua]#python food.py
Addinglili.

Finished making your food!
[root@pa1 lijinghua]#cat food.py
#!/usr/local/python
foods=['lili']
if foods:
	for food in foods:
		print("Adding" +food+"." )
	print("\nFinished making your food!")
else:
	print("Are you want a plain food?")

	假若是空的
	
	[root@pa1 lijinghua]#python food.py
Are you want a plain food?
[root@pa1 lijinghua]#cat food.py
#!/usr/local/python
# -*- coding: utf-8 -*-
foods=[]            # 注意此行的变化
if foods:
	for food in foods:
		print("Adding" +food+"." )
	print("\nFinished making your food!")
else:
	print("Are you want a plain food?")

使用多个列表

示范

[root@pa1 lijinghua]#python lists.py
Adding xigua
Adding qiezi
sorry! we no enough qiezi!
Finished making your food~
[root@pa1 lijinghua]#cat lists.py
#!/usr/local/python
# -*- coding: utf-8 -*-
names=['lili','xigua','qiezi']
foods=['lizi','xigua','qiezi'] #把lili 换成了lizi
for food in foods:
	if food in names:
		print("Adding "+food)
else:
	print("sorry! "+"we no enough "+food+"!")
print("Finished making your food~")