1,遍历整个列表
magicians=['zhang','hua','tom'] for magician in magicians: print(magician.title())
定义临时变量magician,最好临时变量能够准确描述要表达的列表内容。
(合并拼接+)
magicians=['zhang','hua','tom'] for magician in magicians: print(magician.title()+','+'is good')
magicians=['zhang','hua','tom'] for magician in magicians: print(magician.title()+','+'is good') print('I am very like'+' '+magician.title()+'.\n')#\n表示每次循环一遍,都插入一个空行
print不缩进,表示对整个起作用。
magicians=['zhang','hua','tom'] for magician in magicians: print(magician.title()+','+'is good') print('I am very like'+' '+magician.title()+'.\n')#\n表示每次循环一遍,都插入一个空行 print('Thank you ')
应用:可以用for循环初始化游戏—遍历每个游戏角色,将每个角色都显示在屏幕上;再循环后面添加一个不缩进的代码块,在屏幕绘制完所有角色后显示一个play now 的按钮。
2,避免缩进错误
2.1忘记缩进
通常跟在for循环后面的,需要缩进。
magicians=['zhang','hua','tom'] for magician in magicians: print(magician.title()+','+'is good')
2.2忘记缩进额外的代码行
当期望某项操作将针对每个列表元素都执行一次,但却只执行了一次,检查逻辑错误。
magicians=['zhang','hua','tom'] for magician in magicians: print(magician.title()+','+'is good') print('I am very like'+' '+magician.title()+'.\n')#\n表示每次循环一遍,都插入一个空行
2.3不必要的缩进(未在for循环后,且不需要缩进)
2.4循环后不必要的缩进(和2.2的逻辑错误一样)
2.5遗忘了冒号for magician in magicians:
3,创建数值列表range()
for value in range(1,5):#只打印1,2,3,4 print(value)
#可以指定步长 even_numbers=list(range(1,11,2)) #[1, 3, 5, 7, 9]奇数 print(even_numbers) even_numbers=list(range(2,11,2)) #[2, 4, 6, 8, 10]偶数 print(even_numbers)
#创建一个列表,包含1-10的平方 squares=[]#建空列表 for value in range(1,11): square=value**2 #平方 squares.append(square)#按照顺序添加 print(squares)#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] #也可以不使用临时变量 squares=[]#建空列表 for value in range(1,11): squares.append(value**2 )#按照顺序添加 print(squares)#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4,对数字列表简单的统计计算
#min max sum many_numbers=range(1,1000) print(min(many_numbers))#1 print(max(many_numbers))#999 print(sum(many_numbers))#499500
5,列表解析
#创建一个列表,包含1-10的平方 squares=[value**2 for value in range(1,11)] print(squares)#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
6,列表切片
player=['hua','xi','liu','ha','oo'] print(player[0:2])#['hua', 'xi'] print(player[1:3])#['xi', 'liu'] print(player[:4])#['hua', 'xi', 'liu', 'ha'] print(player[2:])#['liu', 'ha', 'oo'] print(player[-2:])#['ha', 'oo'] print(player[-3:-1])#['liu', 'ha'] #遍历切片 players=['hua','xi','liu','ha','oo'] for player in players[:3]: print(player.title())
#复制列表 my_foods=['haha','hehe','hoho'] friend_foods=my_foods[:] print(my_foods) print(friend_foods) my_foods.append('coco') friend_foods.append('koko') print(my_foods) print(friend_foods) '''['haha', 'hehe', 'hoho'] ['haha', 'hehe', 'hoho'] ['haha', 'hehe', 'hoho', 'coco'] ['haha', 'hehe', 'hoho', 'koko']''' my_foods=['haha','hehe','hoho'] friend_foods=my_foods print(my_foods) print(friend_foods) my_foods.append('coco') friend_foods.append('koko') print(my_foods) print(friend_foods) ''' ['haha', 'hehe', 'hoho'] ['haha', 'hehe', 'hoho'] ['haha', 'hehe', 'hoho', 'coco', 'koko'] ['haha', 'hehe', 'hoho', 'coco', 'koko'] '''
remark:这种语法实际上是让python将新变量friend_foods关联到包含在my_foods中的列表,因此这两个列表都指向同一个列表。
元组():不可变的列表
#定义元组 dimensions=(10,20)#元组 圆括号 print(dimensions[0]) print(dimensions[1]) #dimensions[0]=13#不可修改元组内容 #print(dimensions) print('---------------------------------------------') #遍历元组 dimensions=(10,20,30,38) for dimension in dimensions: print(dimension) #不能直接修改元组元素,但是可以重新赋值 dimensions=(10,20,30,38) print('old:') for dimension in dimensions: print(dimension) dimensions=(10,22) print('new:') for dimension in dimensions: print(dimension)