1、索引
序列中所有元素都是从0编号开始,使用负数索引时会从右往左计数,最后一个元素是-1,例子:>>> greeting = 'hello'
>>> greeting[0]
'h'
>>> greeting[-1]
'o'
2、分片
分片可以访问一定范围内的元素>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:6]
[4, 5, 6]
分片操作需要提供两个索引作为边界,第1个索引的元素是包含在分片内的,而第2个则不包含在分片内
分片所得部分包括序列结尾的元素,那么只需置空最后一个索引>>> numbers[3:]
[4, 5, 6, 7, 8, 9, 10]
步长,当使用一个负数作为步长时,必须让开始点大于结束点
3、相加
两种相同类型的序列才能进行连接操作>>> [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
>>> [1,2,3] + 'lihuipeng'
Traceback (most recent call last):
File "", line 1, in
[1,2,3] + 'lihuipeng'
TypeError: can only concatenate list (not "str") to list
4、乘法
None是一个python内建值,意思是空>>> 'python' * 5
'pythonpythonpythonpythonpython'
>>> [None] * 10
[None, None, None, None, None, None, None, None, None, None]
5、成员资格
in 操作符检查一个值是否在序列中>>> permissions = 'rw'
>>> 'w' in permissions
True
>>> users = ['mlh', 'foo', 'bar']
>>> raw_input('Enter your user name: ') in users
Enter your user name: mhl
False
>>> raw_input('Enter your user name: ') in users
Enter your user name: mlh
True
6、长度、最大值、最小值
len() :返回序列中元素的数量
max() :返回序列中最大的元素
min() : 返回序列中最小的元素>>> numbers = [100, 34, 678]
>>> len(numbers)
3
>>> max(numbers)
678
>>> min(numbers)
34