定义
# Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。 aTuple = ('et',77,99.9) print(aTuple)
元组的操作
aTuple = ('et',77,99.9) # 访问元组 print(aTuple[0]) # et # 修改元组 aTuple[0] = 11 # 'tuple' object does not support item assignment a = ('a', 'b', 'c', 'a', 'b') # 左闭右开区间 (b,c),找不到会报错 a.index('a', 1, 3) print(a.index('a', 1, 4)) # 3 print(a.count('b')) # 2
运算符
+
# 字符串 print("hello " + "xxx") # hello xxx # 列表 print([1, 2] + [3, 4]) # [1, 2, 3, 4] # 元组 print(('a', 'b') + ('c', 'd')) # ('a', 'b', 'c', 'd')
*
# 字符串 print('ab' * 4) # 'ababab' # 列表 print([1, 2] * 4) # [1, 2, 1, 2, 1, 2, 1, 2] # 元组 print(('a', 'b') * 4) # ('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b')
in & not in
# 字符串 print('itc' in 'hello itcast') # True # 列表 print(3 in [1, 2]) # False # 元组 print(4 in (1, 2, 3, 4)) # True # 字典 print("name" in {"name": "Delron", "age": 24}) # True