目录
t=(1,1.3,'aa')
# 相当于t=tuple((1,1.3,'aa'))
print(t,type(t))
# (1, 1.3, 'aa') <class 'tuple'>
x=(10) # 单独一个括号代表包含的意思为整数数据类型!!
print(x,type(x))
# 10 <class 'int'>
t=(10,) # 如果元组中只有一个元素,必须加逗号
print(t,type(t))
# (10,) <class 'tuple'>
t=(1,1.3,'aa') # t=(0->值1的内存地址,1->值1.3的内存地址,2->值'aaa'的内存地址,)
t[0]=11111
# TypeError: 'tuple' object does not support item assignment
t=(1,[11,22]) # t=(0->值1的内存地址,1->值[1,2]的内存地址,)
print(id(t[0]),id(t[1]))
# 1358874288 2579862236488
t[0]=111111111 # 不能改第一层的内存地址
# TypeError: 'tuple' object does not support item assignment
t[1]=222222222 # 不能改第一层的内存地址
# TypeError: 'tuple' object does not support item assignment
t[1][0]=11111111111111111 # 修改第二层的内存地址
print(t)
# (1, [11111111111111111, 22])
print(id(t[0]),id(t[1]))
# 1358874288 2579862236488
# 修改第二层的内存地址后第一层内存地址不变
print(tuple('hello'))
# ('h', 'e', 'l', 'l', 'o')
print(tuple([1,2,3]))
# (1, 2, 3)
print(tuple({'a1':111,'a2':333}))
# ('a1', 'a2')
print(tuple(range(10)))
# (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
t=('aa','bbb','cc')
print(t[0])
# aa
print(t[-1])
# cc
t=('aa','bbb','cc','dd','eee')
print(t[0:3])
# ('aa', 'bbb', 'cc')
print(t[::-1])
# ('eee', 'dd', 'cc', 'bbb', 'aa')
t=('aa','bbb','cc','dd','eee')
print(len(t))
# 5
t=('aa','bbb','cc','dd','eee')
print('aa' in t)
# True
for x in t:
print(x)
t=(2,3,111,111,111,111)
print(t.index(111))
# 2
print(t.index(1111111111)) # index查找数据不存在时会报错
# ValueError: tuple.index(x): x not in tuple
print(t.count(111)) # 111出现的次数
# 4
原文:https://www.cnblogs.com/achai222/p/12465332.html