for aa in range(3,8): print(aa) ''' 3 4 5 6 7 ''' #创建一个数字列表: aa1 = list(range(10,20,3)) #10开始到20直接,每隔三个取一个值 print(aa1) #[10, 13, 16, 19] pingfang = [] for a in range(1,10): aa = a**2 pingfang.append(aa) print(pingfang) #[1, 4, 9, 16, 25, 36, 49, 64, 81] #对数字列表进行计算 nums = [2,4,1,3,4,7,5] print(max(nums)) #7 print(min(nums)) #1 print(sum(nums)) #26 #列表解析 p2 = [ n**2 for n in range(1,10) ] print(p2) #[1, 4, 9, 16, 25, 36, 49, 64, 81] #列表切片 print(p2) print(p2[3:8]) #p2 中下标为3的元素到下标为7的元素 print(p2[5:]) #p2 中下标为5的元素到最后的元素 print(p2[:4]) #p2 中下标为0的元素到下标为3的元素 print(p2[-3:]) #p2中离末尾三个元素 ''' [1, 4, 9, 16, 25, 36, 49, 64, 81] [16, 25, 36, 49, 64] [36, 49, 64, 81] [1, 4, 9, 16] [49, 64, 81] ''' #遍历切片 players = ['charles', 'martina', 'michael', 'florence', 'eli'] for player in players[-3:]: print(player.title()) ''' Michael Florence Eli ''' #复制列表 players0 = players[:] players1 = players[-3:] print(players0) print(players1) ''' ['charles', 'martina', 'michael', 'florence', 'eli'] ['michael', 'florence', 'eli'] '''
Python 数字列表相关
最新推荐文章于 2022-11-19 00:40:43 发布