转载请标明出处:
http://www.cnblogs.com/why168888/p/6407682.html本文出自:【Edwin博客园】
Python之List和Tuple类型
1. Python创建list
L = ['Adam', 95.5, 'Lisa', 85, 'Bart', 59]
print L
2. Python按照索引访问list
L = [95.5,85,59]
print L[0]
print L[1]
print L[2]
print L[3]
3. Python之倒序访问list
L = [95.5, 85, 59]
print L[-1]
print L[-2]
print L[-3]
print L[-4]
4. Python之添加新元素
L = ['Adam', 'Lisa', 'Bart']
L.insert(2, 'Paul')
print L
5. Python从list删除元素
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(3)
L.pop(2)
print L
6. Python中替换元素
L = ['Adam', 'Lisa', 'Bart']
L[0] = 'Bart'
L[-1] = 'Adam'
print L
7. Python之创建tuple
t = (0,1, 2, 3, 4, 5, 6, 7, 8, 9)
print t
8. Python之创建单元素tuple
t = ('Adam',)
print t
9. Python之“可变”的tuple
t = ('a', 'b', ('A', 'B'))
print t