列表简介.py

bicycles = ['trek','cannondale','redline','specialized']        #访问列表元素
print(bicycles)

bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0])

bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0].title())
print(bicycles[0].upper())
print(bicycles[0].lower())



bicycles = ['trek','cannondale','redline','specialized']        #索引从0开始
print(bicycles[1])
print(bicycles[3])
print(bicycles[-1])



bicycles = ['trek','cannondale','redline','specialized']        #使用列表中的各个值
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)

names = ['ALex','eric','SB']
message = names[1]+"is a good people"
print(message)
message = names[2]+"is a good people"
print(message)
message = names[0]+"is a good people"
print(message)

bicycles = ['trek','cannondale','redline','specialized']        #列表中修改元素
print(bicycles)
bicycles[0] = 'honda'
print(bicycles)

bicycles = ['trek','cannondale','redline','specialized']        #列表末尾添加元素
bicycles.append('honda')
print(bicycles)

bicycles = []
bicycles.append('honda')
bicycles.append('yamaha')
bicycles.append('suzuki')
print(bicycles)

bicycles = ['trek','cannondale','redline','specialized']        #列表中指定位置添加元素
bicycles.insert(1,'yamaha')
print(bicycles)

bicycles = ['trek','cannondale','redline','specialized']        #del 删除列表元素
del bicycles[0]
print(bicycles)

bicycles = ['trek','cannondale','redline','specialized']        #pop 删除列表元素
last_bicycle = bicycles.pop()
print('The lase bicyle I owned was a ' + last_bicycle.title()+ '.')

bicycles = ['trek','cannondale','redline','specialized']        #根据值删除元素
too_expensive = 'trek'
bicycles.remove('trek')
print(bicycles)
print( "\nA" +too_expensive.title()+   'is too expensive for me')


cars = ['bmw','audi','toyota','subaru']                         #使用sort和reverse对列表进行永久性排序
cars.sort()
print(cars)
cars.reverse()
print(cars)


cars = ['bmw','audi','toyota','subaru']                         #使用函数sorted对列表进行临时排序

print('Here is the original list:')
print(cars)

print('Here is the sorted list:')
print(sorted(cars))

print('Here is the original list again')
print(cars)


cars = ['bmw','audi','toyota','subaru']                         #倒着打印列表
cars.reverse()
print(cars)
print(len(cars))                                                #列表长度

 

posted @ 2016-09-11 14:32  JessisLong  阅读(211)  评论(0编辑  收藏  举报