python从入门到实践-4章操作列表

magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
print(magician.title() + ",that was a great trick!")
print("I can't wait to see your nest trick, " + magician.title() +".\n")

print("Thank you, everyone, That was a great magic show!")
# 缩进非常重要
# 注意什么需要缩进 什么不需要
# 冒号 不要忘了

# 【range】
for value in range(1,5): # 不包含最后一个
print(value)

numbers = list(range(2,11,2))
print(numbers)

squares = []
for value in range(1,11):
# square = value**2
# squares.append(square)
squares.append(value**2)

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),max(digits),sum(digits))

# 【切片】
players = ['charles','martina','michael','florence','eli']
print(players[0:3]) # 我要最后一位
# 倒数[-3:-1]
# 切片遍历
for player in players[:3]:
print(player.title())

# 【复制列表】
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
print(friend_foods)
my_foods.append('cannoli')
friend_foods.append('ice cream')

# 内存地址不一样

# 【元组】不能更改的列表
dimensions = (200,50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)

# 修改元组(覆盖)
dimensions = (400,10)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)

posted @ 2018-10-30 22:13  alfred_hong  阅读(174)  评论(0编辑  收藏  举报