31.python的序列感觉和其他语言中的数组类似。初看类似,实则强大太多!
2.序列和元组的区别在于序列可以修改,元组不能。
3.序列用[]表示,元组用()表示
4.序列操作有:
索引(indexing)
>>> greeting = 'hello'
>>> greeting[0]
'h'
>>> greeting[-1]
'o'
>>> fourth=input('year:')[3]
year:2017
>>> fourth
'7'
分片(sliceing)
url=input('please enter the URL:')
domain = url[11:-4]
print("domain name :"+domain)
运行结果:
please enter theURL:http://www.python.org
domain name :python
>>> number = [1,2,3,4,5,6,7,8,9,10]
>>> number[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> number[0:10:2]
[1, 3, 5, 7, 9]
加(adding)
乘(multiplying)
成员资格检查
eg.
users = ['hello','hi','hyj']
test = input('enter you user name:')
if test in users:print('true')
计算长度
找最大元素
找最小元素
eg.
>>> numbers = [100,50,89]
>>> len(numbers)
3
>>> max(numbers)
100
>>> min(9,8,6,3,1,-1,10)
-1
1.元素赋值
2.元素删除
>>> test = [1,2,3,4,5,6,7,8]
>>> del test[4]
>>> test
[1, 2, 3, 4, 6, 7, 8]
>>> del test[1:2]
>>> test
[1, 3, 4, 6, 7, 8]
2.分片赋值
>>> name = list('perl')
>>> name[3:]=list('test')
>>> name
['p', 'e', 'r', 't', 'e', 's', 't']
>>> name[2:2]=[' ',' ' ,' ' ]
>>> name
['p', 'e', ' ', ' ', ' ', 'r', 't', 'e', 's', 't']
3.列表方法
append
>>> a=[1,2]
>>> a.append(6)
>>> a
[1, 2, 6]
count
extend
>>> a = [1,2,3]
>>> b=[4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
index
>>> a=[1,2,3,4,5,6,7,8,8,8,9]
>>> a.index(8)
7
insert
>>> a = ['s',"f",'rr',"ttt",7]
>>> a.insert(0,0)
>>> a
[0, 's', 'f', 'rr', 'ttt', 7]
>>> a.insert(1,"aa")
>>> a
[0, 'aa', 's', 'f', 'rr', 'ttt', 7]
>>> a.insert(8,9)
>>> a
[0, 'aa', 's', 'f', 'rr', 'ttt', 7, 9]
pop
>>> a=[1,2,3,4,5,6,7,8,9,0]
>>> a.pop()
0
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a.pop(2)
3
>>> a
[1, 2, 4, 5, 6, 7, 8, 9]
remove
reverse
sort
>>> a=[2, 4, 5, 6, 7, 8, 9, 1]
>>> a.remove(9)
>>> a
[2, 4, 5, 6, 7, 8, 1]
>>> a.reverse()
>>> a
[1, 8, 7, 6, 5, 4, 2]
>>> a.sort()
>>> a
[1, 2, 4, 5, 6, 7, 8]
1.
迭代(iteration)
元组:不可变序列,除了不可变以外其他都和列表差不多