其他分享
首页 > 其他分享> > 元组类型内置方法

元组类型内置方法

作者:互联网

元组类型的内置方法

​ 可以存多个数据

​ ()内可以有多个任意类型的值,用逗号隔开,元组是不可变的列表

name_tuple=('jack','alex','peiqi')
name_tuple[0]='nick'    #元素不能修改,会报错

​ 1.按索引取值

name_tuple = ('nick', 'jason', 'tank', 'sean')
# name_tuple[0] = 'nick handsom'  # 报错

print(f"name_tuple[0]: {name_tuple[0]}")
#name_tuple[0]: nick

​ 2.切片(顾头不顾尾,步长)

name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple[1:3:2]: {name_tuple[1:3:2]}")
#name_tuple[1:3:2]: ('jason',)

​ 3.长度len

name_tuple = ('nick', 'jason', 'tank', 'sean')
print(len(name_tuple))      #4

​ 4.成员运算 in 和not in

name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"'nick' in name_tuple: {'nick' in name_tuple}")
#True

​ 5.循环

name_tuple = ('nick', 'jason', 'tank', 'sean')

for name in name_tuple:
    print(name)
    
nick
jason
tank
sean

​ 6.count

name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple.count('nick'): {name_tuple.count('nick')}")
#1

​ 7.index

name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple.index('nick'): {name_tuple.index('nick')}")
#0

​ 多个值

​ 不可变数据类型

标签:内置,jason,name,tuple,元组,nick,sean,类型,tank
来源: https://www.cnblogs.com/zhaogang0104/p/10922875.html