序列是具有先后关系的一组元素
s[0]:第0个位置
>>> s ='123456' >>> s[0:2] '12' >>> s[::2] '135' >>> s*3 '123456123456123456' >>> t='cv' >>> s+t '123456cv' >>> '1' in s True
>>> A =[1 ,2, 3] >>> A [1, 2, 3] >>> A=A[:2] >>> A [1, 2] >>> A[1] 2
列表
xs = [1,2,3] print(xs,xs[2]) print(xs[-1]) xs[2]='too' print(xs) xs.append('good') print(xs) x = xs.pop() print(x,xs)
[1, 2, 3] 3 3 [1, 2, 'too'] [1, 2, 'too', 'good'] good [1, 2, 'too']
>>> ls = ['ac','da','dvv'] >>> la.append('sf') Traceback (most recent call last): File "<pyshell#93>", line 1, in <module> la.append('sf') NameError: name 'la' is not defined >>> ls.append('acd') >>> ls ['ac', 'da', 'dvv', 'acd'] >>> ls.insert(0,'dvs') >>> ls ['dvs', 'ac', 'da', 'dvv', 'acd'] >>> a = ls.pop(1) >>> a 'ac' >>> ls ['dvs', 'da', 'dvv', 'acd'] >>> ls = ['a','a','c','d','c'] >>> ls.remove('a') >>> ls ['a', 'c', 'd', 'c'] >>> ls.remove('c') >>> ls ['a', 'd', 'c']
>>> ls.reverse()
>>> ls
['c', 'd', 'a']
del ls[i:j:k] [i:j] 左闭右开
>>> ls[0:3:2]=['96','36'] >>> ls ['96', 'waf', '36', 'vgf'] >>> del ls[0] >>> ls ['waf', '36', 'vgf'] >>> del ls[0:3:2] >>> ls ['36'] >>> ls+=['afv','d','vs'] SyntaxError: unexpected indent >>> ls+=['afv','d','vs'] >>> ls ['36', 'afv', 'd', 'vs']
>>> ls*=2
>>> ls
['36', 'afv', 'd', 'vs', '36', 'afv', 'd', 'vs']
Slicing:
>>> s = ['dcv','vfs','123'] >>> s[::-1] ['123', 'vfs', 'dcv'] >>> s='123466' >>> len(s) 6 >>> min(s) '1' >>> max(s) '6' >>> s.index('6') 4 >>> s.index('6',0,3) Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> s.index('6',0,3) ValueError: substring not found >>> s.count('6') 2
元组
>>> cre = 'a','b','c'
>>> cre
('a', 'b', 'c')
>>> cer = (cre,0x10,'red')
>>> cer
(('a', 'b', 'c'), 16, 'red')
>>> cer[0][1]
'b'
#获取多个输入的数字 def get(): num= [] isn = input('请输入数字: ') while isn !='': num.append(eval(isn)) isn = input('请输入数字: ') #isn = eval(input('请输入数字: '))是错的,不能eval(空) return num print(get())