python自学第三天,列表】的更多相关文章

1.列表 names=[] #这就是一个空列表 names=[1,5,2,3,4,5]#列表是用的中括号,每个元素是用逗号分开的.列表里面的元素是可以重复的. names[-1]#表示的是取列表的最后一个元素 names[0]#表示的是取列表的第一个元素,取列表的元素是以下标以0开始 #切片 names[1:3]表示取得列表元素是从第二个元素到第三个元素 .所谓顾头不顾尾. names[1:-1]表示取得列表的从第二个元素到倒数第二个元素 names[-3:-1]表示的取的列表是从倒数第二个元素…
# -*- coding: GBK -*- cars = ["bmw", "audi", "toyota", "subaru"] print("这是以前的列表:") print(cars) print("\n这是排序后的列表:") print(sorted(cars)) print("\n再次核对是否改变以前的列表:") print(cars) 输出为: 这是以前的列…
>>> cars = ["bmw", "audi", "toyota", "subaru"] >>> len(cars) 4…
# -*- coding: GBK -*- #reverse: 相反的 cars = ["bmw", "audi", "toyota", "subaru"] print(cars) cars.reverse() print(cars) 输出为: ['bmw', 'audi', 'toyota', 'subaru'] ['subaru', 'toyota', 'audi', 'bmw']…
cars = ["bmw", "audi", "toyota", "subaru"] cars.sort() print(cars) 输出为: ['audi', 'bmw', 'subaru', 'toyota'] 反向排序: cars = ["bmw", "audi", "toyota", "subaru"] cars.sort(reverse…
motorcycles = ["honda", "yamaha", "suzuki"] first_owned = motorcycles.pop(0) print("The first motorcycle I owned was a " + first_owned.title() + ".") 输出为: The first motorcycle I owned was a Honda.…
motorcycles = ['honda', 'yamaha' ,'suzuki'] motorcycles.insert(0, "ducati") print(motorcycles) 输出为: ['ducati', 'honda', 'yamaha', 'suzuki'] 2: motorcycles = [] motorcycles.append("honda") motorcycles.append("yamaha") motorcyc…
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles[0] = 'ducati' print(motorcycles) 输出为: ['honda', 'yamaha', 'suzuki'] ['ducati', 'yamaha', 'suzuki']…
bicycles = ['trek','cannondale','redline','specialized'] message = "My first bicycle was a " + bicycles[0].title() + "." print(message) 输出为: My first bicycle was a Trek.…
#输出并首字母大写 bicycles = ['trek','cannondale','redline','specialized'] print(bicycles[0].title()) 输出为: Trek…