标签:dimen list lin 缩进 创建序列 处理 alice 引用 格式
4.1 遍历整个列表:for循环
注意:使用单数和复数式名称, 可帮助你判断代码段处理的是单个列表元素还是整个列表。
循环语句的冒号不可省略,循环体部分需要统一缩进
01 # ex1 02 magicians = [‘alice‘, ‘david‘, ‘carolina‘] 03 for magician in magicians: 04 print(magician) 05 print("\n") 06 07 # ex2 08 for magician in magicians: 09 print(magician.title()+",that was a great trick!"); 10 print("I can‘t wait to see you next trick,"+magician.title()+".\n"); >>> alice david carolina ? ? ? ? Alice,that was a great trick! I can‘t wait to see you next trick,Alice. ? ? David,that was a great trick! I can‘t wait to see you next trick,David. ? ? Carolina,that was a great trick! I can‘t wait to see you next trick,Carolina. |
? ?
4.2 创建数值列表
函数range() 让Python从你指定的第一个值开始数, 并在到达你指定的第二个值后停止, 因此输出不包含第二个值
使用函数list() 将range() 的结果直接转换为列表。 如果将range() 作为list() 的参数, 输出将为一个数字列表
01 # ex3 02 for value in range(1,6): 03 print(value); 04 05 number=list(range(1,6)); 06 print(number); 07 08 09 number1=list(range(2,11,2)); 10 print(number1); 11 12 # squares.py 13 squares=[]; 14 for value in range(1,11): 15 square=value**2 16 squares.append(square) 17 print(squares) 18 19 # squares.py/ another tips 20 squares=[] 21 for value in range(1,11): 22 squares.append(value**2); 23 print(squares); ? ? >>> 1 2 3 4 5 [1, 2, 3, 4, 5] [2, 4, 6, 8, 10] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] |
4.3 对数字列表进行简单的统计计算
01 # ex4.3 02 num=list(range(0,10)); 03 print(num);#test 04 del num[0]; 05 num.append(0); 06 print(num);#over test 07 a=min(num); 08 b=max(num); 09 c=sum(num); 10 print("the minmum number is "+str(a)+"\nthe maxmum number is "+str(b)); 11 print("the sumarise number is "+str(c)); 12 13 #ex 4.3.3 14 squares1=[value**2 for value in range(1,11)]; 15 print(squares1); >>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] the minmum number is 0 the maxmum number is 9 the sumarise number is 45 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] |
4.4 使用列表的一部分
01 # foods.py 02 my_foods=["pizza","falafel","carrot cake"]; 03 friend_foods=my_foods[:]; 04 my_foods.append("cannoli"); 05 friend_foods.append("ice cream"); 06 print("my favorite foods are "); 07 print(my_foods); 08 print("my favorite foods are "); 09 print(friend_foods); 10 11 # 非切片 12 print("\n "); 14 my_foods=["pizza","falafel","carrot cake"]; 15 friend_foods=my_foods; 16 my_foods.append("cannoli"); 17 friend_foods.append("ice cream"); 18 print("my favorite foods are "); 19 print(my_foods); 20 print("my favorite foods are "); 21 print(friend_foods); >>> my favorite foods are [‘pizza‘, ‘falafel‘, ‘carrot cake‘, ‘cannoli‘] my favorite foods are [‘pizza‘, ‘falafel‘, ‘carrot cake‘, ‘ice cream‘] ? ? ? ? my favorite foods are [‘pizza‘, ‘falafel‘, ‘carrot cake‘, ‘cannoli‘, ‘ice cream‘] my favorite foods are [‘pizza‘, ‘falafel‘, ‘carrot cake‘, ‘cannoli‘, ‘ice cream‘] |
? ?
4.5 元组
目的:创建一系列不可修改的元素,但是可以对整个元组重新进行定义。
创建方式:各个元素用()包括取代列表的[]。
01 dimentions=(200,50); 02 print("orginal dimention"); 03 for dimention in dimentions: 04 print(dimention); >>> orginal dimention 200 50 |
4.6 设置代码格式:PEP8
标签:dimen list lin 缩进 创建序列 处理 alice 引用 格式
原文地址:https://www.cnblogs.com/lovely-bones/p/10976745.html