Python控制结构

条件测试

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

用lower()方法将需要比较的变量转化为小写

检查多个条件

用and(且)和or(或)

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

用in或 not in

确定列表不是空的

python在列表中至少包含一个元素时返回True
列表为空时返回False

while来处理列表和字典

在列表之间移动元素

假设有两个列表lists1 和 lists2 要将lists1中的所有元素移动到lists2中,代码如下:

lists1 = [1,2,3,4,5]
lists2 = []
while lists1:
	temp = lists1.pop()
	lists2.append(temp)

删除包含特定值的所有列表元素

例如:

lists = [1,2,2,2,2,3,4,1,4,5]
#移除列表中所有的2
while 2 in lists:
	lists.remove(2)

使用用户输入来填充字典

例如:

#定义标志
flag = True
#空字典
maps = {}

while flag:
	key = input("key is :")
	value = input("value is :")
	check = input("y/n")
	if check == 'n':
		flag = False
发布了49 篇原创文章 · 获赞 6 · 访问量 5058

猜你喜欢

转载自blog.csdn.net/qq_43959027/article/details/89525800