Files
2025PY/day16/03-元祖的方法和函数.py
2025-05-22 16:50:44 +08:00

23 lines
719 B
Python

# 元祖的常用方法
# 1、index() 查找某个元素 如果数据存在返回对应的下标 否则报错
t1=('banana','apple','mango','pear','orange')
print(t1.index('orange')) # 4 输出对应元素的下标
# print(t1.index('hehe')) # 报错
# 2、count() 统计某个数据在当前元祖中出现的次数
t1=('apple','apple','apple','orange','pear')
print(t1.count('apple')) #3
print(t1.count('hehe')) #0
# 元祖的常用函数
# 1、len() 统计元祖中元素的个数 / 统计元祖的长度
t1=('apple','apple','apple','orange','pear','orange')
print(len(t1))
# 2、sorted() 排序 排序元祖返回的是列表
t2=(12,4,67,8,10,1)
print(sorted(t2)) #升序
print(sorted(t2,reverse=True)) #降序