- Tuple
- Tuple (tuple) is similar to list, the difference is that the elements of tuple cannot be modified
- The elements in Tuple (tuple) are enclosed in parentheses and separated by commas
- The element types in Tuple (tuple) can also be different
- Also supports slicing operations
- The index value starts from 0 and ends with -1
- If there is only one Tuple (element), write it as (1,)
Specific code running process:
# 元组
t=('a','b','c')
t[1]=1 # 元组中的元素不能被修改
TypeError Traceback (most recent call last)
<ipython-input-42-5f612c647d12> in <module>
1 # 元组
2 t=('a','b','c')
----> 3 t[1]=1
TypeError: 'tuple' object does not support item assignment
t=(1)
type(t)
int
t=(t,)
type(t)
tuple
# 经典的面试题
l=['A','B']
t=('a','b',l)
t[2][0]='C' 元组不能改变是指元素指向不变,如果一个元素指向一个列表,指向不变,但所指向列表中的元素可改变
('a', 'b', ['C', 'B'])
l1=['A','B']
t[2]=l1 # 元组员素本身不能被改变,如下错误提示
TypeError Traceback (most recent call last)
<ipython-input-54-da6e52aa1129> in <module>
----> 1 t[2]=l1
TypeError: 'tuple' object does not support item assignment