python 元组

元组 

   序列类型之一,不可更改,常作为参数

1.元组的创建

    使用( )创建,元素用逗号隔开

例如:

tup1 = ('physics', 'chemistry', 1997, 2000)

tup2 = (1, 2, 3, 4, 5 )

tup3 = "a", "b", "c", "d" #也可以省略小括号,但是建议不要省略

tup4=() # 建立空元组

tup5=(5,) # 含有一个元素的元组,逗号不能省略

2.元组的访问

    同列表

元组中的元素不可修改,但可以用 + (元组连接) * (元组复制)对元组进行组合

3.函数

     

函数

作用

例子

min()

返回元组中元素最小值。

tuple1 = ('2', '5', '1')

min(tuple1)

结果:1

max()

返回元组中元素最大值。

tuple1 = ('2', '5', '1')

max(tuple1)

结果:5

tuple()

将可迭代系列转换为元组。

list1 = ['hello', 'tom', 'jack']

tuple1=tuple(list1)

tuple1

结果:('hello', 'tom', 'jack')

len()

计算元组元素个数。

tuple1 = ('hello', 'tom', 'jack')

len(tuple1)

结果:3

del

删除元组

tuple1 = ('hello', 'tom', 'jack')

del tuple1

 

 

posted @ 2020-05-14 19:36  代码人生#  阅读(168)  评论(0编辑  收藏  举报