Python Learning Note Day 02
今天是Python学习第二天,主要学习内容是:列表、列表的遍历、列表操作中的缩进错误、range()和list()函数、切片、列表的复制等。
大纲
相关练习代码
# 利用for循环打印列表里的内容 magicians=['tom','larry','jim','timor'] print('The name list of magicians:') for magician in magicians: print('\t'+magician.title()+',we are looking forward your next show') print('\tGreat!') print('=====End=====') # 命名规则: # for cat in cats: # for dog in dogs: # for item in list_of_items: # 使用单数和复数式名称,可帮助你判断代码段处理的是单个列表元素还是整个列表。
magicians=['tom','larry','jim','timor'] print('The name list of magicians:') for magician in magicians: print('\t'+magician.title()+',we are looking forward your next show') print('\tGreat!') # # 以上代码会报如下错: # File "D:/PythonLearning/day02/2-suojin.py", line 4 # print('\t'+magician.title()+',we are looking forward your next show') # ^ # IndentationError: expected an indented block # 通常,将紧跟在for语句后面的代码行缩进,可消除这种缩进错误。 magicians=['tom','larry','jim','timor'] print('The name list of magicians:') for magician in magicians: print('\t'+magician.title()+',we are looking forward your next show') print('\tGreat!') # # 以上代码不会报错,但是原本个魔术师都应当得到一个great!,但是由于第二个print没有缩进, # 因此只有等待for循环完成后才打印。 message = "清渭技术小站:https://www.qingwei.tech" print(message) # # 以上代码是典型的不应该缩进的时候缩进,因为第二句并不属于第一句,此时Python会报错 # 为避免意外缩进错误,请只缩进需要缩进的代码。在前面编写的程序中,只有要在for循环中对每个元 # 素执行的代码需要缩进。
magicians=['tom','larry','jim','timor'] print('The name list of magicians:') for magician in magicians print('\t'+magician.title()+',we are looking forward your next show') print('\tGreat!') # # 此处漏掉一个: 将导致发生错误,因此要记住,for循环的格式是: # for item in items :
# 比萨:想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for # 循环将每种比萨的名称都打印出来。 # 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对 # 于每种比萨,都显示一行输出,如“I like pepperoni pizza”。 # 在程序末尾添加一行代码,它不在for 循环中,指出你有多喜欢比萨。输出应包 # 含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!”。 pizzas=['beef','apple','orange','onion'] for pizza in pizzas : print('I like '+pizza+' pizza') print('I really like pizza') # 动物:想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中, # 再使用for 循环将每种动物的名称都打印出来。 # 修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great # pet”。 # 在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these # animals would make a great pet!”这样的句子。 animals = ['dog','cat','monkey','penguin'] for animal in animals : print(animal) print('I really like a '+animal+' to be my pet !\n') print('I wolud like any of these anymals to be my pet !')
for number in range(1,5) : print(number) # 函数range(start,end)让Python从你指定的第一个值开始数,并在到达你指定的第二个值后停止, # 因此输出不包含第二个值(这里为5)。 # 这里如果想要打印1-5则range(1,6) for number in range(1,6) : print(number) print('------') # 要创建数字列表,可使用函数list()将range()的结果直接转换为列表。 # 如果将range()作为list()的参数,输出将为一个数字列表 num_list = list(range(1,6)) print(num_list) print('------') # 使用函数range()时,还可指定步长。 num_list = list(range(2,11,2)) print(num_list) num_list = list(range(1,11,2)) print(num_list) print('------') # 创建一个列表,其中包含前10个整数(即1~10)的平方 square = [] nums = list(range(1,11)) for num in nums : square.append(num ** 2) print(square) print('------')
num_list=[12,32,24.1,24,99,1,-0.23,-1.1] print('最小值是: ' + str(min(num_list))) print('最大值是: ' + str(max(num_list))) print('它们的和是: ' + str(sum(num_list))) # 几个函数 # min()求最小值 # max()求最大值 # sum()求和
# 列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。 # 要使用这种语法,首先指定一个描述性的列表名,如squares;然后,指定一个左方括号, # 并定义一个表达式,用于生成你要存储到列表中的值。在这个示例中,表达式为value**2, # 它计算平方值。接下来,编写一个for循环,用于给表达式提供值,再加上右方括号。 squares=[num ** 2 for num in range(1,11)] print(squares)
# 1.数到20:使用一个for 循环打印数字1~20(含) for num in range(1,21) : print(num) print('------End 1------') # 2.一百万:创建一个列表,其中包含数字1~1 000 000,再使用一个for 循环将这 # 些数字打印出来(如果输出的时间太长,按Ctrl + C 停止输出,或关闭输出窗口)。 for num in range(1,1000001) : print(num) print('------End 2------') # 3.计算1~1 000 000 的总和:创建一个列表,其中包含数字1~1 000 000,再使用 # min()和max()核实该列表确实是从1 开始,到1 000 000 结束的。另外,对这个列表调 # 用函数sum(),看看Python 将一百万个数字相加需要多长时间。 nums = list(range(1,1000001)) print('min:'+str(min(nums))) print('max:'+str(max(nums))) print('sum:'+str(sum(nums))) print('------End 3------') # 4.奇数:通过给函数range()指定第三个参数来创建一个列表,其中包含1~20 的奇数; # 再使用一个for 循环将这些数字都打印出来。 odds=list(range(1,21,2)) for odd in odds : print(odd) print('------End 4------') # 5. 3 的倍数:创建一个列表,其中包含3~30 内能被3 整除的数字; # 再使用一个for循环将这个列表中的数字都打印出来。 list_3=list(range(3,31,3)) for num in list_3 : print(num) # for num in range(3,31,3) : # print(num) print('------End 5------') # 6.立方:将同一个数字乘三次称为立方。例如,在Python 中,2 的立方用2**3表示。 # 请创建一个列表,其中包含前10 个整数(即1~10)的立方, # 再使用一个for 循环将这些立方数都打印出来。 # cubes=[num ** 3 for num in range(1,11)] cubes=[] nums=list(range(1,11)) for num in nums : cubes.append(num ** 3) for cube in cubes : print(cube) print('------End 6------') # 7.立方解析:使用列表解析生成一个列表,其中包含前10 个整数的立方。 cubes=[num ** 3 for num in range(1,11)] print(cubes)
# 处理列表的部分元素——Python称之为切片 players=['lindan','malong','lizongwei','chenglong'] print(players[1:3])# ['malong', 'lizongwei'] print(players[:3])# ['lindan', 'malong', 'lizongwei']不指定开始就从最开始开始 print(players[2:])# ['lizongwei', 'chenglong'] 不指定结束就到最后 print(players[-2:])# ['lizongwei', 'chenglong']从倒数第二个到最后 print(players[:-2])# ['lindan', 'malong'] 从第一个到倒数第二个 # 遍历切片 for player in players[:2] : print(player.title()) # scores=[100,65,34,78,66,25,98,76,53,99,27] temp=sorted(scores,reverse=True) print(temp) for score in temp[:3] : print(score)
fr_foods=['noodles','rice','bread','bannaner'] my_foods=fr_foods[:] print('my food :') print(my_foods) print('my friend food :') print(fr_foods) my_foods.append('mango') fr_foods.append('milk') print('my food :') print(my_foods) print('my friend food :') print(fr_foods) his_food=my_foods print('his') print(his_food) my_foods.append('watermelon') print(his_food)#['noodles', 'rice', 'bread', 'bannaner', 'mango', 'watermelon'] 这就是没有使用切片的情况
# 4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。 # 打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个 # 元素。 # 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中 # 间的三个元素。 # 打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三 # 个元素。 players=['lindan','malong','lizongwei','chenglong','guojinjin'] print('The first three items in the list are:') for player in players[:3] : print(player.title()) print('Three items from the middle of the list are:') for player in players[1:-1] : print(player.title()) print('The last three items in the list are:') for player in players[-3:] : print(player.title()) # 你的比萨和我的比萨:在你为完成练习4-1 而编写的程序中,创建比萨列表的 # 副本,并将其存储到变量friend_pizzas 中,再完成如下任务。 # 在原来的比萨列表中添加一种比萨。 # 在列表friend_pizzas 中添加另一种比萨。 # 核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使用一 # 个for 循环来打印第一个列表;打印消息“My friend’s favorite pizzas are:”,再使 # 用一个for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。 my_pizzas=['beef','poke','chicken','fish','fruits'] friend_pizzas=my_pizzas[:] my_pizzas.append('rice') friend_pizzas.append('mango') print('My favorite pizzas are:') for pizza in my_pizzas : print(pizza) print('My friend’s favorite pizzas are:') for pizza in friend_pizzas : print(pizza) # 4-12 使用多个循环:在本节中,为节省篇幅,程序foods.py 的每个版本都没有使用 # for 循环来打印列表。请选择一个版本的foods.py,在其中编写两个for 循环,将各个 # 食品列表都打印出来。 my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] my_foods.append('cannoli') friend_foods.append('ice cream') print("My favorite foods are:") # print(my_foods) for food in my_foods : print(food) print("\nMy friend's favorite foods are:") # print(friend_foods) for food in friend_foods : print(food)
版权声明:
作者:Kiwi
链接:https://www.qingwei.tech/programe-develops/884.html
来源:清渭技术小站
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
共有 0 条评论