a = ()
b = (1) # 不是元组类型,是int型
c = (1,) # 只有一个元素的时候,要加逗号才能表示是元组
d = (1, 2, 3, 4, 5, 6, 1)
print(type(a), type(b), type(c)) # <class 'tuple'> <class 'int'> <class 'tuple'>
print(d.index(1)) # return first index of value 返回值的第一个索引
print(d.count(1)) # return number of occurrences of value 返回值的出现的次数
print("===以上为tuple的内置方法===")
# 除了以上方法外,常用的方法有:
print(d[2]) # 访问索引为2对应的值, 返回3
# d[0]=888 #TypeError: 'tuple' object does not support item assignment 类型错误:tuple 对象不支持项分配。
# tuple 类型一旦初始化就不能修改
print(d + c)
# del d[4] #TypeError:“元组”对象不支持项目删除
print(d)
del c # 删除整个元组
print(len(d)) #计算元组的长度为7
print(max(d)) # 最大值是6
print(min(d)) #最小值的1
print(d[2:]) #返回索引为2到最后一个元素的值
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/tech/python/275711.html