元组
元组(tuple):可存储任意类型数据,元组不可变(其内的列表中的元素可以变)。
一、创建元组
创建元组 | ||
项目 | 示例 | 输出 |
创建一个空元组 | tup1 =() tup2 = tuple() print(type(tup1)) print(type(tup2)) | <class 'tuple'> () <class 'tuple'> () |
创建存储一个数据的元组 | tup3 = (50,) tup4 = 50, print(type(tup3)) print(type(tup4)) | <class 'tuple'> (50,) <class 'tuple'> (50,) |
创建存储多个不同类型数据的元组 | tup5 = ('hello', 10, True, 3.14) tup6 = "a", "b", "c", "d" print(type(tup5)) print(type(tup6)) | <class 'tuple'> ('hello', 10, True, 3.14) <class 'tuple'> ('a', 'b', 'c', 'd') |
- 元组的基本操作
元组的基本操作 | ||
项目 | 示例 | 输出 |
访问元组:可以使用下标索引来访问元组中的值。 | tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5]) | tup1[0]: Google tup2[1:5]: (2, 3, 4, 5) |
对元组进行组合 | tup1 = 12,34,56 tup2 = 'abc', 'xyz' tup3 = tup1 + tup2 print (tup3) | (12, 34, 56, 'abc', 'xyz') |
删除整个元组 | tup = ('Google', 'Runoob', 1997, 2000) del(tup) | |
len(tuple):计算元组元素个数。 | tup1 = ('Google', 'Runoob', 1997, 2000) print(len(tup1)) | 4 4 |
对元组进行复合 | tup1=('Hi!',) print(('Hi!',) * 4) print(tup1* 4) | ('Hi!', 'Hi!', 'Hi!', 'Hi!') ('Hi!', 'Hi!', 'Hi!', 'Hi!') |
tuple.index(obj):从元组中找出某个值第一个匹配项的索引值 | tup1=(1,2,3,5,6,2) print (tup1.index(2)) #2出现了两次,只返回第一个匹配项的索引值 | 1 |
统计某个元素在元组中出现的次数:tuple.count(obj) | tup1=(1,2,3,5,6,2,5,8,3,2,7) print (tup1.count(2)) #统计2出现的次数 | 3 |
sum(tuple) :求元组中的元素之和 | tup1=(2,4,3,6,7) #元组中的元素必须全部为int或float类型 print(sum(tup1)) | 22 |
tuple(seq) :将列表转换为元组 | list1 = ['Google', 'Runoob', 1997, 2000] tup1=tuple(list1) print(list1,tup1,type(tup1)) | ['Google', 'Runoob', 1997, 2000] ('Google', 'Runoob', 1997, 2000) <class 'tuple'> |
min(tuple) :返回元组中元素最小值 | tup1=(5,4,6,1,9) print(min(tup1)) tup2=('9','3','2','0') print(min(tup2)) | 1 0 |
max(tuple) :返回元组中元素最大值 | tup2=(5,4,6,1,9) #元素必须为同一类型的数据类型 print(max(tup2)) tup3=('5','3','2','0') print(max(tup3)) | 9 5 |
使用in关键字检查某个元素是否包含在元组中。语法如下: value in tuple | tup1=(5,4,6,1,9) print(4 in (tup1)) print(4 in(5,4,6,1,9)) | True True |
使用for in遍历 | girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班") for everyOne in girl_tuple: print(everyOne) | 貂蝉 狐狸精 范金链 翠花 小班 |
使用内置函数enumerate遍历。 | girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班") for index, everyOne in enumerate(girl_tuple): print (str(index) + everyOne) | 0貂蝉 1狐狸精 2范金链 3翠花 4小班 |
使用range()内置函数遍历。 | girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班") for index in range(len(girl_tuple)): print (girl_tuple[index]) | 貂蝉 狐狸精 范金链 翠花 小班 |
使用iter()内置函数遍历。 | girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班") for everyOne in iter(girl_tuple): print(everyOne) | 貂蝉 狐狸精 范金链 翠花 小班 |
三、元组的使用实例
下面的代码是判断输入的日期是当年的第几天。将闰年和平年每月的天数分别存入元组tup1、tup2,然后判断查询的年份是平年还是闰年,分别计算天数即可。
year=int(input("请输入需要查询的年:"))
month=int(input("请输入需要查询的月:"))
day=int(input("请输入需要查询的天:"))
tup1=(31,29,31,30,31,30,31,31,30,31,30,31) #闰年
tup2=(31,28,31,30,31,30,31,31,30,31,30,31) #平年
if year%400==0 or (year%4==0 and year%100!=0): #闰年
days=sum(tup1[:month-1])
fin=days+day
print(month,"月",day,"日是",year,"年的第",fin,"天")
else:
days=sum(tup2[:month-1])
fin=days+day
print(month,"月",day,"日是",year,"年的第",fin,"天")
运行结果:
请输入需要查询的年:2019
请输入需要查询的月:12
请输入需要查询的天:31
12 月 31 日是 2019 年的第 365 天