元组-tuple功能介绍
#元组 不可变类型 相当于只读的列表,不可被修改,不可被修改哦
##创建元组最后加,最后加, 形成良好的习惯
""" tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. """
#1.统计子序列出现的次数
# tup = ('alex','eric','seven','qiqi',) # v = tup.count('alex') # print(v)
def count(self, value): # real signature unknown; restored from __doc__ """ T.count(value) -> integer -- return number of occurrences of value """ return 0
#2.根据子序列获取第一个索引的位置
# tup = ('alex','eric','seven','qiqi','qiqi',) # v = tup.index('qiqi') # print(v)
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0
#3. for循环
# tup = ('alex','eric','seven','qiqi','qiqi',) # for i in tup: # print(i)
#4.根据索引取值
# tup = ('alex','eric','seven','qiqi','qiqi',) # v = tup[0] # print(v)
#5.切片
# tup = ('alex', 'eric', 'seven', 'qiqi', 'qiqi',) # v = tup[0:4] # print(v)
#6.列表和元组互相嵌套
####列表里嵌套元组,可以修改吗 # name = ['alex','eric',('aa','bb')] # name[2]='seven' #可修改 # #name[2][0]='ss' #不可修改 # print(name) #解释: #这里整个元组作为列表里的一个元素,对于元组整体而言,是列表的一个元素,列表是可变类型,所以name[2]='seven' #修改的是元组的整体——列表里的一个元素,当然可修改 #name[2][0]='ss'整个定位到了元组里,修改的是元组里的元素,所以不可修改 ###元组里嵌套列表,可以修改吗,依据上边的推理,可推理出来 # name = ('alex','eric',['aa','bb']) # name[2][0] = 'cccccccc' # print(name)