Python——元组

1. 元组(tuple):

元组本身是不可变数据类型,没有增删改查
元组内可以存储任意数据类型

  t=(1,2.3,True,'star')
  print(t)
  print(type(t))

元组里面包含可变数据类型,可以间接修改元组内容

t1 = ([1,2,3],4)
t1[0].append(4)  #t1[0]:元组中的第一个元素,即[1,2,3]
print(t1)

元组里如果只有一个元素的时候,后面要加逗号,否则数据类型不确定

 t2 = ('hello',)
 print(type(t2)

如果不加逗号:

 t2 = ('hello')
 print(type(t2))

2.元组的特性:
1)索引

 1. allowusers = ('root','westos','redhat')
 2. allowpasswd = ('123','456','789')

 
 1. print(allowusers[0])
 2. print(allowusers[-1])

2)切片

 print(allowusers[1:])
 print(allowusers[:-1])
 print(allowusers[::-1])

在这里插入图片描述
3)重复

 print(allowusers * 3)

在这里插入图片描述
4)成员操作符

 print('westos' in allowusers)
 print('westos' not in allowusers)

在这里插入图片描述
5)迭代(for循环)

for user in allowusers:
    print(user)

在这里插入图片描述
6)枚举

for index,user in enumerate(allowusers):
    print('第%d个白名单用户: %s' %(index+1,user))

for user,passwd in zip(allowusers,allowpasswd):
print(user,’:’,passwd)

在这里插入图片描述
在这里插入图片描述

7)连接

 print(allowusers + ('linux','python'))

在这里插入图片描述
元组的常用方法:

     t = (1,2.3,True,'linux')

1)统计出现次数

     print(t.count('linux'))

2)查看索引值

 print(t.index(1))

元组的应用场景:
元组的赋值,有多少个元素,就用多少个变量接收

 t = ('westos',11,100)
 name,age,score = t
 print(name,age,score)

排序

1>.sort() 方法排序

scores = (100,89,45,78,65)
scoresLi = list(scores)     ###转换为列表###
scoresLi.sort()             ####排序####
print(scoresLi)

.

 minscore,*middlescore,maxscore = scores  #*表示可以接收多个值
    print(minscore)
    print(middlescore)
    print(maxscore)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值