Python学习笔记(六)—元组的操作

时间:2023-01-31 14:43:09

元祖也是一个列表,它和list的区别是元祖里面的元素无法修改;

如果元祖里面只有一个元素的话,那么你必须在这个元素后边加上逗号,这样才是元祖的类型;否则类型会显示其他类型

元组的定义:

Python学习笔记(六)—元组的操作

元祖中的方法:

1、index方法:找到元素的下标

Python学习笔记(六)—元组的操作

2、count方法:找到元素在元组中的个数

Python学习笔记(六)—元组的操作

元祖内置函数:

序号

方法及描述

实例

1

len(tuple)
计算元组元素个数。

>>> tuple1 = ('Google', 'Runoob', 'Taobao')

>>> len(tuple1)

3

>>>

2

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

>>> tuple2 = ('5', '4', '8')

>>> max(tuple2)

'8'

>>>

3

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

>>> tuple2 = ('5', '4', '8')

>>> min(tuple2)

'4'

>>>

4

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

>>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu']

>>> tuple1=tuple(list1)

>>> tuple1

('Google', 'Taobao', 'Runoob', 'Baidu')