元组类型的内置方法
- 1.用途
可以存多个数据
- 2.定义
()内可以有多个任意类型的值,用逗号隔开,元组是不可变的列表
name_tuple=('jack','alex','peiqi')
name_tuple[0]='nick' #元素不能修改,会报错
- 3.常用操作很内置方法
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
- 4.存一个值或多个值
多个值
-
5.有序或无序
有序
6.可变or不可变
不可变数据类型