# encoding:utf-8
# 4 操作列表
# 遍历列表(for循环)
magicians = ['alice','dave','tom','jerry']
for magician in magicians: # for 变量 in 列表:
print(magician)
print(magician.title()+' is a good man !')
print('They are all good man !')
# for循环后缩进的句子都会重复执行,不缩进的句子不在for循环执行
# 数值列表
# range()
for value in range(1,5): # 到5停止,for循环
print(value)
numbers = list(range(1,6)) # 创建数值列表
print(numbers)
numbers = list(range(1,9,2)) #从1开始,每次加2,直到达到或超过9
print(numbers)
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
squares = []
for value in range(1,11):
squares.append(value**2) #省略变量square
print(squares)
# 处理数字列表的函数
value = min(squares) #最小值
print(value)
value = max(squares) #最大值
print(value)
value = sum(squares) #和值
print(value)
# 列表解析
squares = [value**2 for value in range(1,11)]
print(squares)
# 切片
print(squares[0:3]) #0:3制定开始结束索引
print(squares[:3]) #未制定开始索引默认从0
print(squares[3:]) #未制定结束索引默认到末尾
print(squares[-3:]) #负数索引返回离列表末尾距离元素
my_foods = ['pizza','cake','coffee','tea']
his_foods = my_foods[:] #通过创建整个列表切片来复制列表
print(my_foods)
print(his_foods)
# 元组
dimensions = (30,50)
print(dimensions[0]) #访问元组元素方法和访问列表元素相同
print(dimensions[1])
# dimensions[0] = 20 元组不能被修改