# In[1]标签:Chapter,dimensions,列表,players,foods,print,操作,元组 From: https://www.cnblogs.com/IT-QiuYe/p/17008757.html
magicians = ['alice', 'david', 'carolina']
for i in magicians:
print(i)
'''4.1.1 深入地研究循环'''
a = list(range(1, 10, 2))
print(a)
print(sum(a))
'''----------4.3.4 列表解析----------- '''
# value = list(range[0,11,2])
squares = [value ** 2 for value in range(1, 11)]
print(squares)
# 作业2 计算 1~1 000 000 的总和
numbers = list(range(1, 1000001))
sumNum = 0
for i in numbers:
sumNum += i
result = sum(numbers)
print(type(numbers))
print(result, sumNum)
''' 4.4 使用列表的一部分 --=-=-=-=-=- 切片 '''
# 你可以生成列表的任何子集,例如,如果你要提取列表的第2~4个元素,可将起始索引指定为 1,并将终止索引指定为 4:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
print(players[2:])
print(players[:2])
print(players[-3:]) # 倒数第三个到最后
''' P75 4.4.2 遍历切片'''
players = ['charles', 'martina', 'michael', 'florence', 'eli']
for i in players[:3]:
print(i)
''' 4.4.3 复制列表 '''
my_foods = ['pizza', 'falafel', 'carrot cake']
# friend_foods = my_foods[:]
# VS
friend_foods = my_foods # 并非把副本存储到 friend_foods
my_foods.append('cannoli')
print(friend_foods)
'''
列表总结:
列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的,这对处理网
站的用户列表或游戏中的角色列表至关重要。
'''
# In[2]
"""========4.5 元组========="""
# 不可变的列表被称为元组
''' 4.5.1 定义元组 '''
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
# dimensions[0] = 250 # TypeError: 'tuple' object does not support item assignment
''' 4.5.3 修改元组变量 '''
dimensions = (200, 50)
dimensions = (20, 20, "123", 10)
print(dimensions)