笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=4
3.1 列表是什么
bicyles = ['trek', 'cannondale', 'redline', 'specialize'] print (bicyles)
['trek', 'cannondale', 'redline', 'specialize']
3.1 访问列表元素
bicyles = ['trek', 'cannondale', 'redline', 'specialize'] print (bicyles[0])
trek
bicyles = ['trek', 'cannondale', 'redline', 'specialize'] print (bicyles[0].title())
Trek
3.1.2 索引从0而不是1开始
bicyles = ['trek', 'cannondale', 'redline', 'specialize'] print (bicyles[1]) print (bicyles[3])
cannondale
specialize
bicyles = ['trek', 'cannondale', 'redline', 'specialize'] print (bicyles[-1])
specialize
3.1.1 使用列表中的各个值
bicyles = ['trek', 'cannondale', 'redline', 'specialize'] message = "My first bicyles was a " + bicyles[0].title() + "." print (message)
My first bicyles was a Trek.
3.2.1 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki'] print (motorcycles) motorcycles[0] = 'ducati' print (motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
3.2.2 在列表中添加元素
1.在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki'] print (motorcycles) motorcycles.append('ducati') print (motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles = [] motorcycles.append('honda') motorcycles.append('yamaha') motorcycles.append('suzuki') print (motorcycles)
['honda', 'yamaha', 'suzuki']
2.在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki'] motorcycles.insert(0,'ducati') print (motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']