列表使用[]来标识

列表和PHP中的数组类似 包括使用和访问方式都是类似

可以用下标索引的方式直接访问

来几个例子,这样看起来才舒服

names = ['baker','pitty','david','philp']
print(names[0])
print(names[1].title())
print(names[-1]) message = "hello,nice to meet you "+names[0].title()+"."
print(message) ways = ['bike','moto','car','walk']
info = "I would like to go to company by "+ways[0].title()+"."
print(info)

插入和删除列表

ways.append('mobike')
print(ways) ways.insert(0,'hello')
print(ways) del ways[0]
print(ways) last=ways.pop()
print(last)
print(ways) ways.remove('car')
print(ways)

使用sort()对列表进行永久性的排序

cars = ['byd','shangqi','mazida','jili']
cars.sort()
print(cars) cars.sort(reverse=True)
print(cars) cars = ['byd','shangqi','mazida','jili']
print(sorted(cars)) cars.reverse()
print(cars) print(len(cars)) print(cars[-1])
05-11 11:06