**1.定义:**序列是具有先后关系的一组元素,它分为字符串类型,元组类型,列表 类型
2.列表类型:
是一种序列类型,创建后可以随意被更改,常采用方括号[]或者list()创建,元素简用逗号隔开,创建长度没有限制
2.1
ls=["123","py",".o"]
>>> print(ls)
['123', 'py', '.o']
>>> print(ls[::-1]) 翻转输出
['.o', 'py', '123']
///////////
len(ls)
3
/////////////////
max(ls)
'py'
ls.index('123') 返回它所在的位置
0
ls.index(x,i,j) 返回它在第i开始到j位置第一次出现的位置
ls.count('123')
1 返回出现的次数
2.2列表的功能操作
1.创建一个空列表lt
lt=[]
2.向lt新增5个元素
lt+=[1,2,3,4,5]
>>> lt
[1, 2, 3, 4, 5]
3.修改lt中的第二个元素
lt[2]=9
>>> lt
[1, 2, 9, 4, 5]
4.从lt的第一个位置删除一个元素
del lt[1]
>>> lt
[1, 9, 4, 5]
5.删除lt中1-3位置中的元素
del lt[1:3]
>>> lt
[1, 5]
6. 判断lt中是否包含0
0 in lt
False
7.往lt中添加0
lt.append(0)
>>> lt
[1, 5, 0]
8.返回0所在的位置
lt.index(0)
2
3.元组类型:
是序列类型的一种扩展,一旦被创建就不不能被修改,使用()或者tuple创建,元素间用逗号隔开。 也可以使用或者不使用小括号
创建:
home=("father","mom","son","daughter")
>>> print(home)
('father', 'mom', 'son', 'daughter')
color="red","blue","yellow"
>>> color
('red', 'blue', 'yellow')
bighome=("grangfather","grandmother",home)
>>> bighome
('grangfather', 'grandmother', ('father', 'mom', 'son', 'daughter'))