元组的内置函数

!!!注:元组所指向的内存中的内容是不可变的

元组指向内存

>>> tup = ('P','y','t','h','o','n')
>>> tup[0] = 'p'
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    tup[0] = 'p'
TypeError: 'tuple' object does not support item assignment
>>> id(tup)
2003349130600
>>> tup = (1,2,3)
>>> id(tup)
2003349651600
>>> 

一、len(tuple) 计算元组元素个数

>>> tup = (1,2,3)
>>> len(tup)
3
>>> 

二、max(tuple) 返回元组中元素最大值

>>> tup = (1,2,3)
>>> max(tup)
3
>>> 

三、min(tuple) 返回元组中元素最小值

>>> tup = (1,2,3)
>>> min(tup)
1
>>> 

四、tuple(seq) 将列表转换为元组

>>> list = ['messi','xavi','Iniesta']
>>> list
['messi', 'xavi', 'Iniesta']
>>> tup = tuple(list)
>>> tup
('messi', 'xavi', 'Iniesta')
>>> 

猜你喜欢

转载自blog.csdn.net/weixin_42676530/article/details/105726862