1.创建元组

在括号中添加元素,并使用逗号隔开即可
tup1 = ("IE","google")

创建空元组
tup2 = ()

创建只包含一个元素时,元素后面要添加逗号
tup2=(1,)

2.访问元组:

tup1 = ('google','IE','firefox','edge')
tup2= (1,2,3)
print(tup1[1]) #IE
print(tup1[:len(tup1)-1]) # ('google', 'IE', 'firefox')
for i in tup1:print(i)

3.元组运算符:

len(tup1) #计算元素个数
tup1+tup2 # 连接
(tup1)*2 # 复制
'google' in tup1 # 元素是否存在
for x in tup1:print(x) # 迭代



4.函数

len(tuple) # 计算元组元素个数
max(tuple) # 返回元组中元素最大值
min(tuple) # 返回元组中元素最小值
tuple(seq) # 将序列转换为元组

eg:
tup1 = (1,5,2,6,1)
li = ['google','Ie','firefox']
str = "hello"
print(len(tup1))
print(max(tup1))
print(min(tup1))
tup2 = tuple(li)
print(tup2)
tup3 = tuple(str)
print(tup3)

posted on 2017-03-19 11:30  lovuever  阅读(121)  评论(0编辑  收藏  举报