首页 > 代码库 > Python:元组

Python:元组

元组:只读,不能修改,使用小括号

创建元组:

tup1 = (physics, chemistry, 1997, 2000)tup2 = (1, 2, 3, 4, 5 )tup3 = "a", "b", "c", "d"
tup4 = (50,)

元组访问:

>>> L = (Google, Taobao, Runoob)>>> L[2]Runoob>>> L[-2]Taobao>>> L[1:](Taobao, Runoob)

元组拼接:

tup1 = (12, 34.56)tup2 = (abc, xyz)tup3 = tup1 + tup2print (tup3)
输出结果为:(
12, 34.56, abc, xyz)

元组删除:

tup = (physics, chemistry, 1997, 2000, hema)del tup  #删除元组,元组中的元素是不能单独删除的!

元组内置函数:

len(tuple)   #计算元组元素个数>>> tuple1 = (Google, Runoob, Taobao)>>> len(tuple1)3max(tuple)   #返回元组中元素最大值min(tuple)   #返回元组中元素最小值tuple(seq)   #将列表转换为元组>>> list1= [Google, Taobao, Runoob, Baidu]>>> tuple1=tuple(list1)>>> tuple1(Google, Taobao, Runoob, Baidu)

 

Python:元组