列表和元组操作.py
magicians = ['alice','david','carolina'] #for循环操作 for magician in magicians: #不要遗漏冒号 print(magician) print( magician.title()+', that was a great trick') print("I can't wait to see your next trick," + magician.title()+".\n") print('Thank you,everyone.That was a great magic show!') for value in range(1,5): #使用range 函数 print(value) numbers = list(range(1,6)) print(numbers) even_numbers = list(range(2,11,2)) print(even_numbers) squares =[] for value in range(1,11): square = value**2 squares.append(square) print(squares) squares = [value**2 for value in range(1,11)] print(squares) digits = [1,2,3,4,5,6,7,8,9,0] #简单统计计算 print(min(digits)) print(max(digits)) print(sum(digits)) players = ['charles','martina','michael','florence','eli']# 切片 print(players[0:3]) print(players[1:4]) print(players[:3]) print(players[2:]) print(players[-3:]) players = ['charles','martina','michael','florence','eli'] print("Here are the first three players on my team:") #for循环遍历切片 for player in players[:3]: print(player.title()) my_foods = ['pizza','falafel','carrot cake',] friend_foods = my_foods[:] my_foods.append('cannali') friend_foods.append('ice creame') print('My favourate foods are:') print(my_foods) print("\nMy friend's favourate foods are:") print(friend_foods) print("The first three iteams in the list are:") print(my_foods[0:4]) dimensions= (200,5) #遍历元组所有值 print(dimensions[0]) print(dimensions[1]) for dimension in dimensions: print(dimension) dimension = (200,50) #修改元组变量 print("Original dimensions:") for dimension in dimensions: print(dimension) dimensions = (400,10) print("\nModified dimensions:") for dimension in dimensions: print(dimension)