# 旭 2018-11-14
#访问列表元素
bicycles=['trek','cannondale','redline','specialized']
print(bicycles[0])
#修改、添加、删除元素
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
# motorcycles[0]='ducati' # 修改
# print(motorcycles)
# motorcycles.append('ducati') # 追加
# print(motorcycles)
# motorcycles.insert(1,'ducati') # 插入
# print(motorcycles)
# del motorcycles[0] # del语句删除元素(要知道删除元素在列表中的位置)
# print(motorcycles)
# popped_motorcycle=motorcycles.pop() # 删除尾末元素
# print(motorcycles)
# print(popped_motorcycle)
# motorcycles.pop(1) # 删除列表中任何位置处的元素
# print(motorcycles)
motorcycles.remove('yamaha') # 根据值删除元素
print(motorcycles)
#组织列表
cars=['bmw','audi','toyota','subaru']
# cars.sort() # 使用sort()对列表进行永久排序
# print(cars)
# cars.sort(reverse=True) # 传参reverse=True,倒序排列
# print(cars)
# print(sorted(cars)) # 使用sorted()对列表进行临时排序
# print(cars)
# cars.reverse() # 倒着打印列表
# print(cars)
len(cars) # 获取列表长度
#操作列表
magicians=['alice','david','carolina']
for magician in magicians: # 循环打印列表
print(magician)
print(magician) # 变量magician在最后一次变为了carolina
for value in range(1,5): # range()方法创建数字列表 注:从1开始,到5停止,不包括5
print(value)
even_numbers=list(range(2,11,2)) # range()方法从2开始数,不断+2,直到达到或者超过11
print(even_numbers)
squares=[value**3 for value in range(0,10)] # 列表解析 生成0-9的立方组成的列表
print(squares)
players=['charles','martina','michael','florence','eli']
print(players[0:3]) # 切片 处理列表中的部分元素 注:从起始索引开始至第二个索引前面的元素结束
print(players[:3]) # 注:若第一个索引没有,则从列表起始索引开始切片 若第二个索引没有,则到列表最后元素索切片结束
print(players[-3:]) # 若第一个索引为负数,则取列表末尾的元素
print(players[:-3]) # 若第二个索引为负数,则取到列表末尾对应倒数的元素
print(players[-4:-1]) # 从倒数第四个开始取,取到倒数第一个结束 若区间内无元素 则该切片为[]
for player in players[:3]: # 遍历切片
print(player)
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:] # 复制列表 注:my_foods=friend_foods 这种做法行不通 这样做实际上得到的还是同一张表
print(friend_foods)
# 元组 元组内包含的元素都是不可变的,是不可变的列表
dimensions=(200,50)
# dimensions[0]=100 # 这种写法程序会报错
print(dimensions)
dimensions=(100,100) # 虽然不能修改元组中的元素,但是可以给存储元组的变量赋值来重新定义整个元组
print(dimensions)