- io进行修改字符串,替换;
>>> import io
>>> s = "hello,sef"
>>> sio = io.stringio(s)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
sio = io.stringio(s)
AttributeError: module 'io' has no attribute 'stringio'
>>> sio = io.StringIO(s)
>>> sio
<_io.StringIO object at 0x10f0faee0>
>>> sio.getvalue()
'hello,sef'
>>> sio.seek(8)
8
>>> sio.write("g")
1
>>> sio.getvalue()
'hello,seg'
>>>
- a<<2 左移一位相当于乘以4
- 序列:列表list里面的元素可以不同类型,字符串和列表都是序列类型
- range()可以方便创建整数数列
>>> list(range(3,6,2)) [3, 5]
5.循环for创建多个元素
>>> a=[x*2 for x in range(100) if x%9==0]
>>> a
[0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198]
>>>
- pop,del列表元素删除填写的是位置,remove直接删除元素内容
- index()出现元素首次出现的位置,可以指定搜索的位置
- len()列表的长度
- 列表排序 a. sort(),默认升序,sort(reverse=true) 降序;随机排序,
>>> a=[10,20,30,40,50]
>>> import random
>>> random.shuffle(a)
>>> a
[40, 30, 20, 10, 50]
- 列表是可变序列,元组tuple()属于不可变序列
>>> b=tuple([1,2,3])
>>> b
(1, 2, 3)
- 元组的元素不能修改,可作为字典的键,列表不行,元组比序列快