首页 > 代码库 > 列表和元组操作.py
列表和元组操作.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)
列表和元组操作.py
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。