python tuples

1. tuple: inhibition with a list of immutable data types, no additions and deletions, may store any type of data.

2. Define a tuple
T = (1, 1.2, True, 'RedHat')
Print (T, type (T))

# If the tuple which contains the variable data types, can indirectly modify the contents of the tuple
T = (2,2 &, True, 'RedHat')
Print (T, type (T))
T1 = ([1, 2.4],. 4 )
T1 [0] .append (. 3)
Print (T1)
T2 = ()
Print (T2, type (T2))
T3 = tuple ([])
Print (T3, type (T3))
T4 = List (T3)
Print (T4, type (T4))
List = [. 1]
Print (List, type (List))

python tuples

# Tuple if only one element, be sure to add a comma after the element, otherwise the data type of uncertainty
t = (2,)
Print (t, of the type (t))

3. The common characteristic of tuples
count () # statistics specified number of elements appear in Gaiyuan group
index () # represents the lowest index value specified element
min () # find the minimum number of tuples in
max () # find the maximum number of tuple
sum () # of summing the elements of the tuple

t = (10,1.2,True,'westos','westos')
print(t.count('westos')) #统计元素 westos出现的次数
print(t.index(1.2)) #显示元素 1.2 的最小索引

4.元组的特性
t = (10,1.2,True,'westos','westos')
#索引
print(t[0]) #通过索引找到指定的元素
print(t[-1] #通过索引找到指定的元素,负数表示从右往左
python tuples

#切片
print(t[:-1]) #除过最后一个其他的元素
print(t[::-1]) #元素反转显示
print(t[2:]) # 除过前两个,剩余的元素
python tuples

#连接 可以将两个元组连接成一个元组:格式 tup1 + tup2

tup1 = ('xian','xianyang','weinan') # 定义第一个元组
tup2 = ('ankang','hanzhong','tongchuan') # 定义第二个元组
tup = tup1 + tup2 # 将两个元组连接成一个新的元组
('xian', 'xianyang', 'weinan', 'ankang', 'hanzhong', 'tongchuan')
python tuples

#不同的数据类型之间不能连接
#print(t + [1,2,3])
#print(t + 'westos')

#重复
print(t * 3) 将元组中的元素重复三次
python tuples

#for 循环
tup = (1,1.2,'xian')
for i in tup :
print(i)
python tuples

#成员操作符
print(1 in t) #1是否在这个元组中 # <元素> in <元组> 属于则为True,不属于为False
print(1 not in t) #1是否不在这个元组值中 # <元素> not in <元组> 不属于则为True,属于为False
python tuples

5.元组的应用场景
a = 1
b = 2
c=a,b
a,b=c
print(a)
print(b)

a = 1
b = 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)

python tuples

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

python tuples

1.系统里面有多个用户,用户的信息目前保存在列表里面
users = ['root','westos']
passwd = ['123','456']

  • 添加用户:
    1). 判断用户是否存在?
    2). 如果存在, 报错;
    3). 如果不存在,添加用户名和密码分别到列表中;
  • 删除用户
    1). 判断用户名是否存在
    2). 如果存在,删除;
    3). 如果不存在, 报错;
  • 用户登陆
  • 用户查看
  • 退出

user = ['root','westos','toto']
passwd = ['123','456','789']
import getpass
while True :
action = input('请输入要进行的操作:[L]ogin|[P]rint|[C]reate|[D]elete|[E]xit :')
#用户查看
if action == 'P':
max = len(user)
print('用户\t\t\t密码')
for i in range(max):
print('%s\t\t%s' % (user[i], passwd[i]))
#添加用户
elif action == 'C':
while 1 :
username = input('请输入用户名称 :')
if username not in user:
user.append(username)
password = getpass.getpass('请输入用户密码 :')
passwd.append(passwd)
print('用户创建成功')
break
else:
print('该用户已经存在')
elif action == 'D':
#删除用户
while 2 :
username = input('请输入用户名称 :')
if username in user:
num = user.index(username)
user.pop(num)
passwd.pop(num)
print('用户删除成功')
break
else:
print('该用户不存在存在')
#用户登录
elif action == 'L':
while 3 :
username = input('请输入用户名称 :')
if username in user:
while 4:
password = getpass.getpass('请输入用户密码 :')
num = user.index(username)
if password == passwd[num]:
print('输入正确登陆成功')
break
else:
print('密码错误!!')
break
else:
print('用户不存在!!')
#退出
elif action == 'E':
print('退出')
break
else:
print('错误动作')

Guess you like

Origin blog.51cto.com/12893781/2402531