Python元组详解
参考文档:https://www.runoob.com/python/python-tuples.html
1.元组的定义
1.1元组的定义:
元组与列表类似,均可以存储任意的数据类型,但是元组中的元素,不可以任意做更改,没有增删改
1.2特点:
Python的元组与列表相似,不同之处在于元组的元素不能修改
元组使用小括号(),列表使用方括号[]
元组创建很简单,只需在括号中添加元素,并使用逗号隔开即可
元组的索引从0开始
2.元组的创建
2.1变量名=()
t=('I','love','python')
print(t)
print(type(t))
结果:
('I','love','python')
<class 'tuple'>
2.2使用内置函数tuple()
t1=tuple('I','love','python',123,435)
print(t1)
print(type(t1)
结果:
('I','love','python',123,435)
<class 'tuple'>
2.3如果创建元组中只有一个元素,必须要有逗号
#规范写法
t3=(1,)
t4=('python',)
print(t3,t4)
#也可以这样子书写(不推荐)
t5=(50)
print(t5)
结果:
(1,)('python')
t5
2.4创建空元组
print(tuple()) #第一种
t6=tuple() #第二种
empty_tuple=() #第三种
print(t6,type(t6)
print(type(empty_tuple))
结果:
()
() <class 'tuple'>
()
3.访问元组中的值
和列表一样,我们可以使用索引(Index)访问元组中的某个元素(得到的是一个元素的值),也可以使用切片访问元组中的一组元素(得到的是一个新的子元组)
使用索引访问元组元素的格式为:
tuple_name[i]
#其中,tuple_name表示元组名字,i表示索引值,元组的索引可以是正数,也可以是负数
代码演示:
t=tuple('https://www.csdn.net')
#使用索引访问元组中的某个元素
print(t[10]) #使用正数索引
print(t[-10]) #使用负数索引
结果为:
w
.
使用切片访问元组元素的格式为:
Name_tuple[start : end : step]
#其中,start表示起始索引,end表示结束索引,step表示步长
代码演示:
t=tuple('https://www.csdn.net')
print(t[3:5:]) #使用正数切片
print(t[4:5:2]) #start:4 end:5 step:2 指定开始和结束和步长
print(t[-3:-1]) #使用负数切片
print(t[:2]) #切片操作
print(t[0:-1]) #掐头去尾
print(t[::2]) #反序
print(t[::2]) #隔一个取一个
print(t[::]) #全取
结果为:
('p', 's')
('s',)
('n', 'e')
('h', 't')
('h', 't', 't', 'p', 's', ':', '/', '/', 'w', 'w', 'w', '.', 'c', 's', 'd', 'n', '.', 'n', 'e')
('h', 't', 's', '/', 'w', 'w', 'c', 'd', '.', 'e')
('h', 't', 's', '/', 'w', 'w', 'c', 'd', '.', 'e')
('h', 't', 't', 'p', 's', ':', '/', '/', 'w', 'w', 'w', '.', 'c', 's', 'd', 'n', '.', 'n', 'e', 't')
4.更新/修改元组
元组是不可变的,这意味着我们无法更新或更改元组元素的值,但是可以使用现有元组的一部分来创建新的元组
代码演示:
tuple1=('abc','efg','asd')
tuple2=(1,2,3,4,5,6)
c=tuple1+tuple2
print(c)
print(type(c))
结果为:
('abc', 'efg', 'asd', 1, 2, 3, 4, 5, 6)
<class 'tuple'>
5.删除元组元素
元素中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组
tup=('I','love','Python',234,54,765)
print (tup)
del tup
print("After deleting tup :")
print (tup)
结果为:
('I', 'love', 'Python', 234, 54, 765)
After deleting tup :
Traceback (most recent call last):
File "G:/Python/learn02/demo.py", line 15, in <module>
print (tup)
NameError: name 'tup' is not defined
6.基本元组操作
6.1元组的重复
users=('root','redhat','inux')
#重复
print (users *3)
结果为:
('root', 'redhat', 'inux', 'root', 'redhat', 'inux', 'root', 'redhat', 'inux')
6.2元组的连接
tuple=(1,2,3,4,5)
print(tuple + ('123','123'))
结果为:
(1, 2, 3, 4, 5, '123', '123')
6.3成员操作符
#成员操作符
hub=('redhat','Centos','Ubtun','中标麒麟')
passwds=('123','456','789')
print('unix' in hub)
print('redhat' not in hub)
#迭代
for kebi in hub:
print(kebi)
for index,kebi in enumerate(hub):
print('第%d个用户: %s' % (index+1,kebi))
for kebi,passwd in zip(hub,passwds):
print(kebi,':',passwd)
结果为:
False
False
redhat
Centos
Ubtun
中标麒麟
第1个用户: redhat
第2个用户: Centos
第3个用户: Ubtun
第4个用户: 中标麒麟
redhat : 123
Centos : 456
Ubtun : 789
7.索引、切片和矩阵
由于元组是序列、索引和切片的工作方式与列表的工作方式相同,假设输入以下值:
Y=('c','c++','python','GO')
print(Y[2])
print(Y[-2])
print(Y[1:])
结果为:
python
python
('c++', 'python', 'GO')
8.元组循环遍历
tup = ('小米', '创维', '海信','康佳','长虹')
for tv in tup:
print(tv)
结果为:
小米
创维
海信
康佳
长虹
9.元组和格式化字符串
格式化后面的括号和内容,本质上就是元组;我们可以先定义一个元组,然后在格式化后面%元组,一样能输出,同时,我们甚至可以将print括号里面的函数拿出来,定义成一个变量,在Print输出
name='zhangsan'
age=12
heigh=181
print("%s 的年龄是 %d,身高是 %d" % (name,age,heigh))
userinfo = ('zhansan',20,182)
print("%s 的年龄是 %d,身高是 %d" %(userinfo[0],userinfo[1],userinfo[2]))
#还可以这样子书写
print("%s 的年龄是 %d,身高是 %d" % userinfo)
info ="%s 的年龄是 %d,身高是 %d" % userinfo
print(info)
结果为:
zhangsan 的年龄是 12,身高是 181
zhansan 的年龄是 20,身高是 182
zhansan 的年龄是 20,身高是 182
zhansan 的年龄是 20,身高是 182
10.元组和列表之间的转换
将其它数据类型转换为元组类型,比如:字符串,列表,元组等
#将字符串转换成元组
tup1 = tuple("hello",)
print(tup1)
#将列表转换成元组
list1 = ['Python', 'Java', 'C++', 'JavaScript']
tup2 = tuple(list1)
print(tup2)
#将字典转换成元组
dict1 = {'a':100, 'b':42, 'c':9}
tup3 = tuple(dict1)
print(tup3)
#将区间转换成元组
range1 = range(1, 6)
tup4 = tuple(range1)
print(tup4)
结果为:
('h', 'e', 'l', 'l', 'o')
('Python', 'Java', 'C++', 'JavaScript')
('a', 'b', 'c')
(1, 2, 3, 4, 5)
11.内置元组函数功能
元组函数: cmp(tuple1,tuple2) 比较两个元组的元素 len(tuple) 给出元组的总长度 max(tuple) 从元组返回最大值项 min(tuple) 从元组返回最大值项 tuple(seq) 将列表转换成元组
代码演示:
tup1 = (1, 2, 3, 4, 5)
tup3 = (1, 2, 3, 4)
tup2 = ('a', 'b', 'c', 'd', 'e')
print (cmp(tup1, tup2))
print (cmp(tup1, tup3))
print (len(tup1))
print (len(tup2))
print (max(tup1))
print (max(tup2))
print (min(tup1))
结果为:
5
5
5
e
1
你们的点赞,是我最大的动力