Python-元组

元组为不可改变的列表,元组只有两个方法,一个count(),一个index()

定义一个元组

>>> web = tuple() #定义一个空元组
>>> web
()
>>> web = () #定义一个空元组
>>> web
()
>>> web = ('html','php','javascript') #定义一个元组
>>> web
('html', 'php', 'javascript')

元组中值的查询

>>> web = ('html','php','javascript')
>>> web[2] #根据索引获取值
'javascript'
>>> web[0:2] #切片
('html', 'php')

count()获取元组中某个元素的个数

>>> web = ('html','php','javascript')
>>> web.count('php')
1

index()获取元组中某个元素的索引

>>> web = ('html','php','javascript')
>>> web.index('html')
0

元组里面的元素是不可以修改的,但是如果元素是可修改的数据类型就能修改,如列表

>>> web = ('html',['php','asp'],'javascript')
>>> web[1]
['php', 'asp']
>>> web[1][1] = 'aspx'
>>> web
('html', ['php', 'aspx'], 'javascript')
原文地址:https://www.cnblogs.com/sch01ar/p/8462526.html