python中元組

1.元組:帶了緊箍咒的列表 不可變數據類型,沒有增刪改,能夠存儲任意數據類型。python

2.定義一個元組
t = (1,1.2,True,'redhat')
print(t,type(t))app

#若是元組裏麪包含可變數據類型,能夠間接的修改元組內容
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))ide

python中元組

#元組若是隻有一個元素,元素後面必定要加逗號,不然數據類型不肯定
t = (2,)
print(t,type(t))3d

3.元組的經常使用特性
count() # 統計指定的元素在改元組中出現的次數
index() # 表示指定元素的最小索引值
min() # 找出元組中的最小數
max() #找出元組中的最大數
sum() # 對元組中的元素求和blog

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中元組get

#切片
print(t[:-1]) #除過最後一個其餘的元素
print(t[::-1]) #元素反轉顯示
print(t[2:]) # 除過前兩個,剩餘的元素
python中元組input

#鏈接 能夠將兩個元組鏈接成一個元組:格式 tup1 + tup2it

tup1 = ('xian','xianyang','weinan') # 定義第一個元組
tup2 = ('ankang','hanzhong','tongchuan') # 定義第二個元組
tup = tup1 + tup2 # 將兩個元組鏈接成一個新的元組
('xian', 'xianyang', 'weinan', 'ankang', 'hanzhong', 'tongchuan')
python中元組io

#不一樣的數據類型之間不能鏈接
#print(t + [1,2,3])
#print(t + 'westos')

#重複
print(t * 3) 將元組中的元素重複三次
python中元組

#for 循環
tup = (1,1.2,'xian')
for i in tup :
print(i)
python中元組

#成員操做符
print(1 in t) #1是否在這個元組中 # <元素> in <元組> 屬於則爲True,不屬於爲False
print(1 not in t) #1是否不在這個元組值中 # <元素> not in <元組> 不屬於則爲True,屬於爲False
python中元組

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中元組

#元組的賦值:有多少個元素,就用多少個變量接收
t = ('westos',10,100)
name,age,score =t
print(name,age,score)

python中元組

1.系統裏面有多個用戶,用戶的信息目前保存在列表裏面
users = ['root','westos']
passwd = ['123','456']

  • 添加用戶:
    1). 判斷用戶是否存在?
    2). 若是存在, 報錯;
    3). 若是不存在,添加用戶名和密碼分別到列表中;
  • 刪除用戶
    1). 判斷用戶名是否存在
    2). 若是存在,刪除;
    3). 若是不存在, 報錯;
  • 用戶登錄
  • 用戶查看
  • 退出

user = ['root','westos','toto']passwd = ['123','456','789']import getpasswhile 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('用戶建立成功')breakelse:print('該用戶已經存在')elif action == 'D':#刪除用戶while 2 :username = input('請輸入用戶名稱 :')if username in user:num = user.index(username)user.pop(num)passwd.pop(num)print('用戶刪除成功')breakelse: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('輸入正確登錄成功')breakelse:print('密碼錯誤!!')breakelse:print('用戶不存在!!')#退出elif action == 'E':print('退出')breakelse:print('錯誤動做')

相關文章
相關標籤/搜索