Python基礎(chǔ)入門教程9:程序編程基礎(chǔ)之組合數(shù)據(jù)類型
來源:
奇酷教育 發(fā)表于:
接著上一章內(nèi)容,繼基本數(shù)據(jù)類型之后,本章進(jìn)行組合數(shù)據(jù)類型的講解3 2 組合數(shù)據(jù)類型python中為了方便一部分相似數(shù)據(jù)的處理,提供了各種組
接著上一章內(nèi)容,繼基本數(shù)據(jù)類型之后,本章進(jìn)行組合數(shù)據(jù)類型的講解
3.2. 組合數(shù)據(jù)類型
python中為了方便一部分相似數(shù)據(jù)的處理,提供了各種組合類型,常見的如列表、元組
python中的列表是可變序列,元組是不可變序列
3.2.1. 列表
列表的語法結(jié)構(gòu):通過一堆方括號包含起來的數(shù)據(jù)序列,可以存放重復(fù)數(shù)據(jù)

因為列表本身也是對象,所以可以進(jìn)行嵌套處理

列表數(shù)據(jù)的查看,可以通過索引/下標(biāo)進(jìn)行查看

列表中追加數(shù)據(jù):append()

列表中指定位置追加數(shù)據(jù):insert()

刪除列表末尾的元素:pop()

刪除列表中指定位置的元素:pop(index)

將列表中指定位置的數(shù)據(jù)進(jìn)行替換,直接給對應(yīng)的索引賦值

至此,常見的列表操作就基本介紹結(jié)束了,后續(xù)更加細(xì)致的操作手段和方式,在項目中需要進(jìn)行深入的時候和熟練了。
3.2.2. 元組
元組在python中,其實是列表的一種特殊的形式,一旦聲明創(chuàng)建就不允許改變。
語法結(jié)構(gòu):使用一對圓括號包含起來的一組數(shù)據(jù),可以存放重復(fù)數(shù)據(jù)

定義并給元組進(jìn)行賦值之后,就可以像列表一樣進(jìn)行數(shù)據(jù)的查看了

由于元組的數(shù)據(jù)一旦定義就不可更改,在一定程度上提高了代碼的安全性,所以在程序設(shè)計開發(fā)的過程中,對于一部分不需要更改的數(shù)據(jù)如果可以定義成元組的,就不要定義成列表了。

3.2.3. 集合
python中,還提供了一種特殊的數(shù)據(jù)類型,集合Set,和列表類似,但是不能存儲相同的數(shù)據(jù);注意:集合set中存放數(shù)據(jù)是不能重復(fù)的,并且是沒有存放順序的,也就是沒有下標(biāo)的。
語法:使用一對大括號/花括號包含起來的一組數(shù)據(jù)

集合中增加數(shù)據(jù):add()

集合中刪除數(shù)據(jù):remove()

3.2.4. 字典
為了方便數(shù)據(jù)在組合數(shù)據(jù)類型中的管理,python 還提供了一種特殊存儲數(shù)據(jù)的組合數(shù)據(jù)類型:字典。通過key-value鍵值對的形式來存儲數(shù)據(jù),可以很方便的通過key來對value進(jìn)行增刪改查的操作
語法結(jié)構(gòu):
查詢字典中的數(shù)據(jù),使用方括號或者get()函數(shù)

修改字典中指定的數(shù)據(jù),通過給指定的key直接賦值就可以修改

刪除字典中的數(shù)據(jù),通過刪除指定的key對應(yīng)的數(shù)據(jù)即可刪除

4種組合數(shù)據(jù)類型部分的基本使用講解完成,下一章單獨(dú)講解變量和運(yùn)算符的操作
>>>更多VR/AR入門教程:VR入門