元组的应用

元组的应用

元组的应用

allowUsers = (‘root’,‘westos’,‘redhat’)
allowPasswd = (‘123’,‘456’,‘789’)

#索引 切片
print(allowUsers[0])
#最后一个
print(allowUsers[-1])
#第一个以及它后面的
print(allowUsers[1:])
print(allowUsers[2:])
#除了最后一个
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)

元组的应用
#zip:两个元组的元素之间一一对应
for user,passwd in zip(allowUsers,allowPasswd):
print(user,’:’,passwd)

t = (1,2.3,True,‘westos’,‘westos’)
print(t.count(‘westos’))
print(t.index(2.3))

元组的应用

#变量交换数值
a = 1
b = 2
b,a = a,b

#1.先把(a,b)封装成一个元组(1,2)

#2.b,a=a,b —> b,a=(1,2)

#b = (1,2)[0] a = (1,2)[1]

#print(a)
#print(b)

#打印变量的值
name = ‘westos’
age = 11
t = (name,age)
print(‘name:%s , age:%d’ %(name,age))
print(‘name:%s , age:%d’ %t)

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

score = (100,89,45,78,65)
scoreLi = list(score)
scoreLi.sort()
print(scoreLi)
scores = sorted(score)
print(scores)
minscore,*middlescore,maxscore = scores
print(minscore)
print(middlescore)
print(maxscore)
print(‘最终成绩为: %.2f’ %(sum(middlescore) / len(middlescore)))

元组的应用

元组的应用

上一篇:redis配置文件优化


下一篇:Linux系统中用户的管理