摘要:元組的創(chuàng)建元組是帶了約束的列表,仍可以存放任意數(shù)據(jù)類型默認(rèn)元組內(nèi)容是不可改變的,但當(dāng)元組內(nèi)包含可變數(shù)據(jù)類型時(shí),可以間接修改元組如果元組內(nèi)只有一個(gè)元素,其后要加逗號(hào),否則數(shù)據(jù)類型不確定元組的常用方法元組的特性索引切片重復(fù)連接只能增加元組成
元組(tuple)的創(chuàng)建
元組是帶了約束的列表,仍可以存放任意數(shù)據(jù)類型
>>> sheen =(1,"3",True,3.4,[1,4],(1,5)) >>> print(type(sheen))
默認(rèn)元組內(nèi)容是不可改變的,但當(dāng)元組內(nèi)包含可變數(shù)據(jù)類型時(shí),可以間接修改元組
>>> star =([1,4,65],"hello") >>> star[0].append(10001) >>> print(star) ([1, 4, 65, 10001], "hello")
如果元組內(nèi)只有一個(gè)元素,其后要加逗號(hào),否則數(shù)據(jù)類型不確定
>>> sheen =("star") >>> print(type(sheen))元組的常用方法>>> sheen1 =("star",) >>> print(type(sheen1))
count()
index()
>>> sheen =(1,"morning",[1,9]) >>> print(sheen.count(1)) 1 >>> print(sheen.index("morning")) 1元組的特性 索引
>>> clotho =(1,4,6,8,"sheen") >>> print(clotho[1]) 4 >>> print(clotho[-2]) 8切片
>>> print(clotho[::-1]) ("sheen", 8, 6, 4, 1) >>> print(clotho[:5]) (1, 4, 6, 8, "sheen")重復(fù)
>>> print(clotho*2) (1, 4, 6, 8, "sheen", 1, 4, 6, 8, "sheen")連接
>>> print(clotho+("star",)) #只能增加元組 (1, 4, 6, 8, "sheen", "star")成員操作符
>>> print("sheen" in clotho) True >>> print("sheen" not in clotho) Falsefor 遍歷
users=("root","student","sheen") for item in users: print(item, end=",") root,student,sheen, Process finished with exit code 0for 循環(huán)并且?guī)в兴饕杜e)
users=("root","student","sheen") print("白名單顯示".center(50,"*")) for index,user in enumerate(users): print("第%d位白名單用戶:%s" %(index+1,user)) **********************白名單顯示*********************** 第1位白名單用戶:root 第2位白名單用戶:student 第3位白名單用戶:sheen Process finished with exit code 0zip:一一對(duì)應(yīng)
users=("root","student","sheen") passwds=("redhat","student","huawei") print("白名單顯示".center(50,"*")) for user,passwd in zip(users,passwds): print("白名單用戶%s的密碼為%s" %(user,passwd)) **********************白名單顯示*********************** 白名單用戶root的密碼為redhat 白名單用戶student的密碼為student 白名單用戶sheen的密碼為huawei元組的應(yīng)用場(chǎng)景 交換變量值
a=11 b=22 b,a=a,b #先把(a,b)封裝成一個(gè)元組(11,22) # b,a = a,b -----> (b,a) =(11,22) # b=(11,22)[0],a=(11,22)[1] print(a,b)打印變量值
name="root" age = 18 m=(name,age) print("%s的年齡是%s" %m)元組的賦值
m=("root",90,100) name,chinese,math =m print(name,chinese,math) root 90 100 Process finished with exit code 0
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/42229.html
摘要:數(shù)據(jù)類型有數(shù)字,字符串,值,列表,元組,集合,字典可變與不可變可變與不可變的區(qū)別對(duì)這個(gè)數(shù)據(jù)類型進(jìn)行增刪改差時(shí),數(shù)據(jù)存儲(chǔ)地址不變,不會(huì)開辟新的空間可變不開辟新空間不可變會(huì)改變內(nèi)存地址不可變數(shù)據(jù)類型數(shù)字,字符串,值,元組可變數(shù)據(jù)類型列表,集合, 數(shù)據(jù)類型 有:數(shù)字,字符串,bool值,列表,元組,集合,字典 可變與不可變 可變與不可變的區(qū)別:對(duì)這個(gè)數(shù)據(jù)類型進(jìn)行增刪改差時(shí),數(shù)據(jù)存儲(chǔ)地址不變,...
摘要:函數(shù)的定義范例總結(jié)無參函數(shù)名快注釋函數(shù)體定義函數(shù),并不會(huì)執(zhí)行函數(shù)體里面的內(nèi)容調(diào)用函數(shù)的過程函數(shù)里面嵌套函數(shù)調(diào)用外層函數(shù)時(shí),內(nèi)層函數(shù)不會(huì)執(zhí)行變量參數(shù)定義函數(shù)時(shí)的變量,稱做形參,可以任意命名真實(shí)的數(shù)據(jù)信息,調(diào)用函數(shù)時(shí)傳遞的參數(shù),稱為實(shí)參是形參是 函數(shù)的定義 范例 def print(self, *args, sep= , end=n, file=None): 總結(jié) 無參 def 函數(shù)名...
迭代 可以通過 for 循環(huán)來遍歷 list 或 tuple,這種遍歷我們稱為迭代(Iteration)只要是可迭代對(duì)象,都可以迭代,比如字典默認(rèn)情況下,字典迭代的是key值如何讓判斷一個(gè)類型是否可迭代 from collections import Iterable #導(dǎo)入collections模塊的Iterable類型判斷方法 print(isinstance({abc:1},Itera...
摘要:什么是包為了組織好模塊,會(huì)將多個(gè)模塊分為包。處理包也是相當(dāng)方便的。簡(jiǎn)單來說,包就是文件夾,但該文件夾下必須存在文件。最簡(jiǎn)單的情況下,只需要一個(gè)空的文件即可。當(dāng)然它也可以執(zhí)行包的初始化代碼包底下也能包含包,這和文件夾一樣,還是比較好理解的。 什么是包? 為了組織好模塊,會(huì)將多個(gè)模塊分為包。Python 處理包也是相當(dāng)方便的。簡(jiǎn)單來說,包就是文件夾,但該文件夾下必須存在 __init__....
閱讀 1711·2021-10-13 09:39
閱讀 3181·2021-10-12 10:11
閱讀 573·2021-09-28 09:36
閱讀 2687·2019-08-30 15:55
閱讀 1409·2019-08-30 13:04
閱讀 656·2019-08-29 17:08
閱讀 1934·2019-08-29 14:14
閱讀 3430·2019-08-28 18:23