a = (1,2,3,4) PRint(a)
a1 = 1,2,3 print(a1)
a2 = 2, print(a2)
e = tuple([111,222,333]) print(e) say = tuple(“hello”) print(say)
索引(下標(biāo)): tu = tuple([‘hello’,’world’,’welldown’]) print(tu[2])
切片取值: print(tu[0:2])
元組是不可變的,類似字符串,不能在原處修改內(nèi)容,但我們可以把它的類型做下轉(zhuǎn)換,例如把一個(gè)元組轉(zhuǎn)換為一個(gè)列表進(jìn)行修改,之后再轉(zhuǎn)換成元組。 例如:元組a = (3,6,6) 要把它變成(6,6,6)要怎么操作呢? ch = (3,6,6) d1 = list(ch) d1[0] = 6 print(tuple(d1))
如果元組內(nèi)包含的元素中,有可變數(shù)據(jù)類型,修改起來就十分方便了 sd = ([‘hello’,False],123.222,True) sd[0][1] = ‘this world’ print(sd)
df = (123,456) ef = (7,8,9) cf = df + ef print(cf)
tf = (“say”,’hello’)*4 print(tf)
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注