bicycles = ["trek", "cannondale", "redline"] print(bicycles[0]) print(bicycles[0].title()) # 索引为-1,返回最后一个元素 print(bicycles[-1]) print(bicycles[-1].title()) motorcycles = ["honda", "yamaha", "suzuki"] print(motorcycles) # 修改第一个元素 motorcycles[0] = 'ducati' print(motorcycles) # 追加元素 motorcycles.append("honda") print(motorcycles) # 在指定位置插入元素 motorcycles.insert(0, "insert") print(motorcycles) # 删除指定位置元素 del motorcycles[0] print(motorcycles) # 删除最后一个元素 popped_motorcycle = motorcycles.pop() print(motorcycles) print(popped_motorcycle) # 删除第一个ducati元素 motorcycles.remove("ducati") print(motorcycles) # sort永久排序 list.sort() cars = ["bmw", "audi", "toyouta", "subaru"] cars.sort() print(cars) # 排序相反顺序 reverse=True cars.sort(reverse=True) print(cars) # sorted临时排序 sorted(list) cars = ["bmw", "audi", "toyouta", "subaru"] print(sorted(cars)) print(cars) # 逆序 永久修改 cars.reverse() print(cars) # 列表长度 print(len(cars)) # 遍历列表 magicians = ["alick", "david", "caroline"] for magician in magicians: print(magician.title() + 'is a mangician.') print("i can't wait to see your next trick," + magician.title() + '\n') print("thank you".title()) for val in range(1, 5): print(val) number = list(range(1, 5)) print(number) # 步进2 even_number = list(range(2, 11, 2)) print(even_number) # **表示乘方运算,输出1~10的平方 squares = [] for value in range(1, 11): square = value**2 squares.append(square) print(squares) digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] print(min(digits)) print(max(digits)) print(sum(digits)) # 列表解析,以下示例中,表达式为value**2,它计算平方值,接下来编写一个for循环,用于给表达式提供值,再加上方括号。 squares = [value**2 for value in range(1, 11)] print(squares) squares = [value**3 for value in range(1, 11)] print(squares) count_list = list(range(1, 1000001)) print(min(count_list)) print(max(count_list)) print(sum(count_list)) # 切片 list[m:n] players = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg'] print(players[0:3]) print(players[1:4]) # 从列表开头开始 print(players[:3]) # 切片终止于列表末尾 print(players[4:]) # 负数索引返回离列表末尾相应距离的元素 print(players[-3:]) for player in players[:3]: print(player.title()) # 复制列表 list[:] my_food = ['pizza', 'falafel', 'cake'] friend_food = my_food[:] my_food.append('ice cream') friend_food.append('cannoli') print('my foods are:') for value in my_food: print(value) print('friend foods are:') for value in friend_food: print(value) # 元组:不可变的列表,用圆括号标识,不可修改元组的元素,但可以为元组直接赋值 dimensions = (200, 50) print(dimensions[0]) print(dimensions[1])