列表和元组操作.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列表和元组操作.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的主要内容,如果未能解决你的问题,请参考以下文章

初识Python

列表和元组操作

Python 内置数据结构列表和元组

Python 内置数据结构列表和元组

#9 Python列表和元组

python列表和元组的操作