Python元组常用操作
Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号
aTuple = ('et',77,99.9)
Python访问元组
aTuple = ('et',77,99.9)
aTuple[0] #et
Python元组的index和count方法
a = ('a', 'b', 'c', 'a', 'b')
a.index('a', 1, 3) # 注意是左闭右开区间,即左边包含右边不包含
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: 'a' is not in list
a = ('a', 'b', 'c', 'a', 'b')
a.index('a', 1, 3)
print(a.count('b'))
# 2
print(a.count('d'))
# 0
Python元素不可修改,所以没有列表的增删改操作