python----元组

1.元组的创建

元组(tuple): 元组本身是不可变数据类型,没有增删改查
元组内可以存储任意数据类型
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.元组的特性
allowusers = ('root','westos','redhat')
allowpasswd = ('123','456','789')

#索引 切片
print(allowusers[0])
print(allowusers[-1])
print(allowusers[1:])
print(allowusers[:-1])
print(allowusers[::-1])

#重复
print(allowusers * 3)

#连接
print(allowusers + ('linux','python'))

#成员操作符
print('westos' in allowusers)
print('westos' not in allowusers)

#for循环
for user in allowusers:
    print(user)

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

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

在这里插入图片描述

3.元组的常用方法
t = (1,2.3,True,'linux')

print(t.count('linux'))
print(t.index(1))

在这里插入图片描述

4.元组的应用场景
#元组的赋值,有多少个元素,就用多少个变量接收
t = ('westos',11,100)
name,age,score = t
print(name,age,score)

scores = (100,89,45,78,65)

scoresLi = list(scores)
scoresLi.sort()
print(scoresLi)

scores = sorted(scores)
print(scores)

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、付费专栏及课程。

余额充值