序列
序列
1、字符串(str)
type('1 2') # <class 'str'>
2、元组
type((1, 2)) # <class 'tuple'>
3、列表
type([1, 2]) # <class 'list'>
数学运算
1、加法
相同类型的序列相加,会得到一个合并序列元素的新序列
'1' + '2' # '12'
(1,) + (2,) # (1, 2)
[1] + [2] # [1, 2]
2、乘法
一个序列乘以n,会得到一个n个此重复序列合并的新序列
'1' * 3 # '111'
(1,) * 3 # (1, 1, 1)
[1] * 3 # [1, 1, 1]
截取子序列
1、通过[index]
获取某一个子元素
- index 为正数,从
0
正数
'hello'[0] # 'h'
- index 为负数,从末尾开始数,
-1
开始
'hello'[-1] # 'o'
2、通过 [prefix:suffix]
获取子序列
- prefix 和 suffix 均存在:
[prefix, suffix)
的子序列
'hello'[0:2] # 'he'
'hello'[0:-1] # 'hell'
- suffix 不存在:获取
[prefix, 结尾]
的子序列
'hello'[0:] # 'hello'
'hello'[-2:] # 'lo'
- prefix 不存在:获取
[开头, suffix)
的子序列
'hello'[:2] # 'he'
'hello'[:-2] # 'hel'
查询是否含有子项
通过 in
关键字查询是否含有
'1' in '123' # True
'1' in ['1', '2'] # True
'1' in ('1', '2') # True
通过 not in
查询是否不含有
'1' not in '123' # False
'1' not in ['1', '2'] # False
'1' not in ('1', '2') # False
查询序列长度
通过 len()
方法插叙一个序列的长度
len('123') # 3
len(['1', '2', '3']) # 3
len(('1', '2', '3')) # 3
查询序列中的最大值/最小值
通过 max()
查询序列中的最大值
通过 min()
查询序列中的最小值