TUPLE
Python 的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e" #这样也可以创建一个元组
创建空元组
tup4 = ()
元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用:
tup1 = (99) print(type(tup1)) tup2 = (101,) print(type(tup2)) #执行结果: <class 'int'> <class 'tuple'>
访问元组
元组可以使用下标索引来访问元组中的值,如下实例
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' print(tup1[0]) print(tup2[1],tup2[4]) print(tup3[0],tup3[2],tup3[5]) print(tup2[0:3]) #执行结果: baidu 2 5 a c f (1, 2, 3)
修改元组
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例:
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' tup4 = tup1 + tup2 print(tup4) #执行结果: ('baidu', 'google', 'apple', 'amazon', 'IBM', 1, 2, 3, 4, 5) #相当于列表的extend方法
删除元组
元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例:
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' del tup1 #删除整个元组,但是不是能删除元组的元素,不然就修改了元组,因为:元组是不能修心的!!!!
元组运算符
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。
1.0 “+” 元组可以相加,组成一个新的元组
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' tup4 = tup1 + tup2 + tup3 print(tup4) #执行结果: ('baidu', 'google', 'apple', 'amazon', 'IBM', 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e', 'f')
2.0 元组可以复制,用*
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' print((tup1)*2) #2个tup1 print((tup2)*3) #3个tup2 print((tup3)*4) #4个tup3 #执行结果: ('baidu', 'google', 'apple', 'amazon', 'IBM', 'baidu', 'google', 'apple', 'amazon', 'IBM') (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5) ('a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f')
3.0 查询元组的元素的个数:
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' print(len(tup1)) print(len(tup2)) print(len(tup3)) #执行结果: 5 5 6
4.0 IN 判断是否在元组里面:
"baidu" in tup1 #True
5.0 for 迭代
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' for i in tup1: print(i) for i in tup2: print(i) for i in tup3: print(i) #执行结果: baidu google apple amazon IBM 1 2 3 4 5 a b c d e f
6.0 tuple 的切片方法:
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' print(tup1[1]), print(tup2[-1]), print(tup3[4:1:-1]) #执行结果: google 5 ('e', 'd', 'c')
元组内置函数
Python元组包含了以下内置函数
1.0 len(tuple):计算元组元素个数。
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' print(len(tup1)) print(len(tup2)) print(len(tup3)) #执行结果: 5 5 6
2.0 max(tuple) :返回元组中元素最大值。
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' print(max(tup1)),#ASCII 最大值 print(max(tup2)), print(max(tup3)) #执行结果: google 5 f
3.0 min(tuple):返回元组中元素最小值。
tup1 = ("baidu","google","apple","amazon","IBM") tup2 = (1,2,3,4,5) tup3 = "a","b","c","d","e",'f' print(min(tup1)),#ASCII 最小值 print(min(tup2)), print(min(tup3)) #执行结果: IBM 1 a
4.0 tuple(seq):将列表转换为元组。
list1 = ["baidu","google","apple","amazon","IBM"] tup1 = tuple(list1) print(tup1) #执行结果: ('baidu', 'google', 'apple', 'amazon', 'IBM')
5.0 list(tuple):将元组转为列表。
tup3 = ("a","b","c","d","e",'f') list1 = list(tup3) print(list1) #执行结果: ['a', 'b', 'c', 'd', 'e', 'f']