码迷,mamicode.com
首页 > 编程语言 > 详细

Python:元组

时间:2016-08-31 18:39:36      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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

创建元组:

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 + tup2
print (tup3)
输出结果为: (
12, 34.56, ‘abc‘, ‘xyz‘)

元组删除:

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

元组内置函数:

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

 

Python:元组

标签:

原文地址:http://www.cnblogs.com/ping-y/p/5826941.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!