列表
创建和访问列表
bicycle = ['trek', 'cannondale', 'redline','specialized']
print bicycle
print bicycle[0]
print bicycle[-1]
print bicycle[:1]
print bicycle[1:3]
print bicycle[0].title()
message = 'My first bicycle was a '+bicycle[0].title()+'.'
print message
['trek', 'cannondale', 'redline', 'specialized']
trek
specialized
['trek']
['cannondale', 'redline']
Trek
My first bicycle was a Trek.
修改、添加和删除元素
bicycle = ['trek', 'cannondale', 'redline','specialized']
bicycle[1] = 'baoma'
bicycle[2:] = ['baoma','haima']
print bicycle
['trek', 'baoma', 'baoma', 'haima']
bicycle.append('xiaodao')
print bicycle
bicycle.insert(1,'bb')
print bicycle
['trek', 'baoma', 'baoma', 'haima', 'xiaodao']
['trek', 'bb', 'baoma', 'baoma', 'haima', 'xiaodao']
bicycle = ['trek0', 'trek1','trek2','trek3','trek4','trek5']
del bicycle[1]
print bicycle
bicycle_pop = bicycle.pop()
print bicycle_pop
print bicycle
bicycle_pop0 = bicycle.pop(0)
print bicycle_pop0
print bicycle
bicycle.remove('trek3')
print bicycle
['trek0', 'trek2', 'trek3', 'trek4', 'trek5']
trek5
['trek0', 'trek2', 'trek3', 'trek4']
trek0
['trek2', 'trek3', 'trek4']
['trek2', 'trek4']
列表操作
cars = ['bwm','audi','toyota','subaru']
cars.sort()
print cars
cars = ['bwm','audi','toyota','subaru']
cars.sort(reverse=True)
print cars
cars = ['bwm','audi','toyota','subaru']
print sorted(cars)
print cars
['audi', 'bwm', 'subaru', 'toyota']
['toyota', 'subaru', 'bwm', 'audi']
['audi', 'bwm', 'subaru', 'toyota']
['bwm', 'audi', 'toyota', 'subaru']
cars = ['bwm','audi','toyota','subaru']
print cars
cars.reverse()
print cars
print len(cars)
['bwm', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bwm']
4
number1 = list(range(1,6))
print number1
number2 = list(range(1,11,2))
print number2
[1, 2, 3, 4, 5]
[1, 3, 5, 7, 9]
squares1 = []
for value in range(1,11,2):
square = value**2
squares1.append(square)
print squares1
squares2 = [value**2 for value in range(1,11,2)]
print squares2
[1, 9, 25, 49, 81]
[1, 9, 25, 49, 81]
digits = [1,2,3,4,5]
print min(digits)
print max(digits)
print sum(digits)
1
5
15
players = ['aaa','bbb','ccc','ddd','eee','fff']
print players[0:3]
print players[:3]
print players[3:]
print players[:-3]
print players[-3:]
players = ['aaa','bbb','ccc','ddd','eee','fff']
for player in players[:3]:
print player.title()
print '--------------------------------------------'
players = ['aaa','bbb','ccc','ddd','eee','fff']
players_copy1 = players[:]
print players_copy1
players.append('ggg1')
players_copy1.append('ggg2')
print players
print players_copy1
print '------------------'
players = ['aaa','bbb','ccc','ddd','eee','fff']
players_copy2 = players
print players_copy2
players.append('ggg1')
players_copy2.append('ggg2')
print players
print players_copy2
['aaa', 'bbb', 'ccc']
['aaa', 'bbb', 'ccc']
['ddd', 'eee', 'fff']
['aaa', 'bbb', 'ccc']
['ddd', 'eee', 'fff']
Aaa
Bbb
Ccc
--------------------------------------------
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg1']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg2']
------------------
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg1', 'ggg2']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg1', 'ggg2']
元祖(不可变的列表)
dims = (20,50)
print dims[0],dims[1]
print type(dims)
dims[1] = 30
20 50
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
5
6 ##元祖不可改
----> 7 dims[1] = 30 # 此处会报错
TypeError: 'tuple' object does not support item assignment
dims = (10,20,30,40)
for dim in dims:
print dim
10
20
30
40
dims = (10,20,30)
print dims
dims = (40,50,60)
print dims
(10, 20, 30)
(40, 50, 60)