版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
Python語言程序設(shè)計【列表基礎(chǔ)】PythonLanguageProgramming[ListBasics]知識點【訪問列表中的值】1234aList=['Deris','Weng',1,2]
bList=[1,2,3,4,5,6,7]
print("aList[0]:",aList[0])
print("bList[1:5]:",bList[1:5])【例】使用下標(biāo)索引來訪問列表aList[0]:DerisbList[1:5]:[2,3,4,5]輸出結(jié)果:KnowledgePoints[Accessingvaluesinthelist]1234aList=['Deris','Weng',1,2]
bList=[1,2,3,4,5,6,7]
print("aList[0]:",aList[0])
print("bList[1:5]:",bList[1:5])[Example]UsingasubscriptindextoaccessalistaList[0]:DerisbList[1:5]:[2,3,4,5]Outputresult:知識點【列表的更新】1234aList=['Deris','Weng',1,2]
print("第三個元素為:",aList[2])
aList[2]=3
print("更新后的第三個元素為:",aList[2])【例】列表的更新第三個元素為:1更新后的第三個元素為:3輸出結(jié)果:[Updatestothelist]1234aList=['Deris','Weng',1,2]
Print("Thethirdelementis:",aList[2])
aList[2]=3
Print("Thethirdupdatedelementis:",aList[2])[Example]UpdatestothelistThethirdelementis:1Theupdatedthirdelementis:3KnowledgePointsOutputresult:知識點【向列表指定位置插入元素】123aList=['Deris','Weng',1,2]
aList.insert(2,'Happy')#使用insert向索引2的位置插入元素‘黃金分割’
print("aList結(jié)果為:",aList)#列表中有5個元素【例】向列表插入元素aList結(jié)果為:['Deris','Weng','Happy',1,2]輸出結(jié)果:[Insertanelementintothelistatthespecifiedposition]123aList=['Deris','Weng',1,2]
AList.insert(2,'Happy')#Useinserttoinserttheelement'GoldenSection'intothepositionofindex2
Print("aListresultsare:",aList)#Thereare5elementsinthelist[Example]InsertingelementsintoalistTheaListresultis:['Deris','Weng','Happy',1,2]KnowledgePointsOutputresult:知識點【刪除或清空列表中的記錄】1234aList=['Deris','Weng',1,2]
print("刪除前的列表:",aList)
delaList[2]
print("刪除第三個元素后的列表:",aList)【例】利用del語句刪除列表中的元素刪除前的列表:['Deris','Weng',1,2]刪除第三個元素后的列表:['Deris','Weng',2]輸出結(jié)果:[Deleteoremptyrecordsinthelist]1234aList=['Deris','Weng',1,2]
Print("Listbeforedeletion:",aList)
delaList[2]
Print("Listafterdeletingthethirdelement:",aList)[Example]DeleteelementsinthelistwithdelstatementListbeforedeletion:['Deris','Weng',1,2]Listafterdeletingthethirdelement:['Deris','Weng',2]KnowledgePointsOutputresult:知識點【刪除或清空列表中的記錄】123456aList=['Deris','Weng',1,2]
#列表本身不包含數(shù)據(jù),而是包含變量:aList[0]到aList[3]
first=aList[0]#拷貝列表,創(chuàng)建新的變量引用,而不是數(shù)據(jù)對象的復(fù)制。
delaList[0]#del刪除的是變量,而不是數(shù)據(jù)。
print(aList)
print(first)【例】del語句作用在變量上,而不是數(shù)據(jù)對象上['Weng',1,2]Deris輸出結(jié)果:pop方法刪除列表中的元素123456aList=['Deris','Weng',1,2]
#Thelistitselfdoesnotcontaindata,butcontainsvariables:aList[0]toaList[3]
first=aList[0]#Copythelistandcreateanewvariablereferenceinsteadofcopyingdataobjects.
delaList[0]#deldeletesvariables,notdata.
print(aList)
print(first)[Example]delstatementsactonvariables,notdataobjects['Weng',1,2]DerisThepopmethoddeletestheelementsinthelist[Deleteoremptyrecordsinthelist]KnowledgePointsOutputresult:知識點【遍歷列表、二級索引】123aList=['Deris','Weng',1,2]
foriinaList:
print(i)【例】列表的遍歷DerisWeng12輸出結(jié)果:[Traversinglists,secondaryindexes]123aList=['Deris','Weng',1,2]
foriinaList:
print(i)[Example]IterationofalistDerisWeng12KnowledgePointsOutputresult:知識點【遍歷列表、二級索引】12aList=['Deris','Weng',[1,2,3]]
print(aList[2][0])【例】列表的二級索引1輸出結(jié)果:12aList=['Deris','Weng',[1,2,3]]
print(aList[2][0])[Example]Secondaryindexofalist1[Traversinglists,secondaryindexes]KnowledgePointsOutputresult:Python語言程序設(shè)計【索引的使用+元素數(shù)量+列表運算符】PythonLanguageProgramming[Useofindexes+numberofelements+listoperators]索引俗稱下標(biāo)。索引值從0開始,直到長度減1位置(例如對10個元素的列表,最大的索引值為[9])。
知識點【索引的使用】12aList=[1,2,3,4,5,6,7]
print("aList[3]:",aList[3])【例】使用下標(biāo)索引來訪問列表aList[3]:4輸出結(jié)果:IndexCommonlyknownassubscripts.Theindexvaluestartsfrom0andgoestothelengthminus1position(e.g.foralistof10elements,themaximumindexvalueis[9]).Forexample,foralistof10elements,themaximumindexvalueis[9])KnowledgePoints[UseofIndexes]12aList=[1,2,3,4,5,6,7]
print("aList[3]:",aList[3])[Example]UsingasubscriptindextoaccessalistaList[3]:4Outputresult:知識點【索引的使用】123x=[1,2,3,4,5,6]
print(x[6])#最大的索引值
print(x[-7])#最小的索引值【例】索引越界Traceback(mostrecentcalllast):File"indexError.py",line3,in<module>print(x[6])IndexError:listindexoutofrange輸出結(jié)果:123x=[1,2,3,4,5,6]
print(x[6])#Maximumindexvalue
print(x[-7])#Minimumindexvalue[Example]IndexoutofboundsTraceback(mostrecentcalllast):File"indexError.py",line3,in<module>print(x[6])IndexError:listindexoutofrangeKnowledgePoints[UseofIndexes]Outputresult:函數(shù)len()求集合中的元素數(shù)量知識點【求元素數(shù)量】1234x=[1,2,3,4,5,6]
y=[]
print(len(x))
print(len(y))【例】函數(shù)len()60輸出結(jié)果:Functionlen()tofindthenumberofelementsintheset[Numberofelementstobefound]1234x=[1,2,3,4,5,6]
y=[]
print(len(x))
print(len(y))[Example]Functionlen()60KnowledgePointsOutputresult:
+
和
*
的操作符+號用于組合列表,*號用于重復(fù)列表知識點【列表運算符】12345print([1,2,3,4]+[5,6])#列表的拼接
print(5in[1,2,3,4,5,6])#元素是否存在于列表中
print(['Weng']*3)#列表的重復(fù)倍增
forxin[1,2,3]:#列表的迭代
print(x,end="")【例】列表運算符[1,2,3,4,5,6]True['Weng','Weng','Weng']123輸出結(jié)果:
+and*operatorsThe+signisusedforcombininglists,the*signisusedforrepeatinglists[Listoperators]12345print([1,2,3,4]+[5,6])#Splicingoflists
print(5in[1,2,3,4,5,6])#Whethertheelementexistsinthelist
print(['Weng']*3)#Duplicatemultiplicationofthelist
forxin[1,2,3]:#Iterationoflist
print(x,end="")[Example]Thelistoperator[1,2,3,4,5,6]True['Weng','Weng','Weng']123Outputresult:KnowledgePointsPython語言程序設(shè)計【列表函數(shù)&方法】PythonLanguageProgrammingListFunctions&Methods知識點【列表函數(shù)】1.列表函數(shù)函數(shù)描述len(list)計算列表元素個數(shù)max(list)返回列表元素最大值min(list)返回列表元素最小值list(seq)將元組轉(zhuǎn)換為列表12345a=['Hello','Deris','Weng']
n=[1,2,3]
print(len(a))
print(max(a))
print(min(n))【例】列表函數(shù)的使用3Weng1輸出結(jié)果:KnowledgePoints[ListFunctions]1.thelistfunctionfunctionDescriptionlen(list)countsthenumberofelementsinthelistmax(list)Returnsthemaximumvalueofthelistelementmin(list)Returnstheminimumvalueofthelistelementlist(seq)Convertingtuplestolists12345a=['Hello','Deris','Weng']
n=[1,2,3]
print(len(a))
print(max(a))
print(min(n))[Example]Theuseofthelistfunction3Weng1Outputresult:知識點【列表函數(shù)】1234a=['Hello','Deris','Weng']
n=[1,2,3]
x=[a,n]
print(min(x))【例】列表函數(shù)的錯誤使用Traceback(mostrecentcalllast):File"ListError.py",line4,in<module>print(min(x))TypeError:'<'notsupportedbetweeninstancesof'int'and'str'輸出結(jié)果:1234a=['Hello','Deris','Weng']
n=[1,2,3]
x=[a,n]
print(min(x))[Example]MisuseofthelistfunctionTraceback(mostrecentcalllast):File"ListError.py",line4,in<module>print(min(x))TypeError:'<'notsupportedbetweeninstancesof'int'and'str'KnowledgePoints[ListFunctions]Outputresult:知識點【列表方法】2.列表方法方法描述list.append(obj)在列表末尾添加新的對象list.count(obj)統(tǒng)計某個元素在列表中出現(xiàn)的次數(shù)list.extend(seq)在列表末尾一次性追加另一個序列中的多個值(用新列表擴(kuò)展原來的列表)list.index(obj)從列表中找出某個值第一個匹配項的索引位置list.insert(index,obj)將對象插入列表list.pop(obj=list[-1])移除列表中的一個元素(默認(rèn)最后一個元素),并且返回該元素的值list.remove(obj)移除列表中某個值的第一個匹配項list.reverse()反向列表中元素[Listmethod]2.ThetabularapproachMethodsDescriptionlist.append(obj)Addsanewobjecttotheendofthelistlist.count(obj)countsthenumberoftimesanelementappearsinthelistlist.extend(seq)Appendmultiplevaluesfromanothersequenceatoncetotheendofthelist(extendstheoriginallistwithanewlist)list.index(obj)Findstheindexpositionofthefirstmatchofavaluefromthelistlist.insert(index,obj)Inserttheobjectintothelistlist.pop(obj=list[-1])Removesanelementfromthelist(defaultstothelastelement)andreturnsthevalueofthatelementlist.remove(obj)removesthefirstmatchofavalueinthelistlist.reverse()elementsinthereverselistKnowledgePoints知識點【列表方法】方法描述list.sort([func])對原列表進(jìn)行排序list.clear()清空列表list.copy()復(fù)制列表list.sort([func])對原列表進(jìn)行排序12345a=['Hello','Deris','Weng']
a.append('Happy')
print(a)
a.reverse()
print(a)【例】列表方法的使用['Hello','Deris','Weng','Happy']['Happy','Weng','Deris','Hello']輸出結(jié)果:MethodsDescriptionlist.sort([func])Sortingtheoriginallistlist.clear()Emptythelistlist.copy()Copythelistlist.sort([func])Sortingtheoriginallist12345a=['Hello','Deris','Weng']
a.append('Happy')
print(a)
a.reverse()
print(a)[Example]Theuseofthelistmethod['Hello','Deris','Weng','Happy']['Happy','Weng','Deris','Hello'][Listmethod]KnowledgePointsOutputresult:Python語言程序設(shè)計【元組基礎(chǔ)】PythonLanguageProgramming[TupleBasics]知識點【聲明元組】1234tup1=(50)
print(type(tup1))#不加逗號,類型為整型
tup1=(50,)
print(type(tup1))#加上逗號,類型為元組【例】只包含一個元素的元組<class'int'><class'tuple'>輸出結(jié)果:創(chuàng)建空元組tup1=()KnowledgePoints[declarationtuple]1234tup1=(50)
print(type(tup1))#Withoutcomma,thetypeisinteger
tup1=(50,)
print(type(tup1))#Addacomma,andthetypeistuple[Example]Atuplecontainingonlyoneelement<class'int'><class'tuple'>Creatinganemptytupletup1=()Outputresult:知識點【訪問元組】1234tup1=('Deris','Weng',1,2)
tup2=(1,2,3,4,5,6,7)
print("tup1[0]:",tup1[0])
print("tup2[1:5]:",tup2[1:5])【例】訪問元組tup1[0]:Deristup2[1:5]:(2,3,4,5)輸出結(jié)果:[Accesstuple]1234tup1=('Deris','Weng',1,2)
tup2=(1,2,3,4,5,6,7)
print("tup1[0]:",tup1[0])
print("tup2[1:5]:",tup2[1:5])[Example]Accessingthetupletup1[0]:Deristup2[1:5]:(2,3,4,5)KnowledgePointsOutputresult:知識點【修改元組】1234tup1=(12,34.56);
tup2=('abc','xyz')
tup3=tup1+tup2;
print(tup3)【例】修改元組(12,34.56,'abc','xyz')輸出結(jié)果:123tup1=(12,34.56)
#以下修改元組元素操作是非法的。
tup1[0]=100【例】非法訪問元組Traceback(mostrecentcalllast):File"tupError.py",line3,in<module>tup1[0]=100TypeError:'tuple'objectdoesnotsupportitemassignment輸出結(jié)果:元組自身不能修改[Modifytuple]1234tup1=(12,34.56);
tup2=('abc','xyz')
tup3=tup1+tup2;
print(tup3)[Example]Modifythetuple(12,34.56,'abc','xyz')123tup1=(12,34.56)
#Thefollowingtupleelementmodificationoperationsareillegal.
tup1[0]=100[Example]IllegalaccesstotupleTraceback(mostrecentcalllast):File"tupError.py",line3,in<module>tup1[0]=100TypeError:'tuple'objectdoesnotsupportitemassignmentThetupleitselfcannotbemodifiedKnowledgePointsOutputresult:Outputresult:知識點【刪除元組】12345tup=('Deris','Weng',1,2)
print(tup)
deltup;
print("刪除后的元組tup:")
print(tup)【例】元組中的元素不允許刪除,只可刪除整個元組Traceback(mostrecentcalllast):('Deris','Weng',1,2)File"C:/delTup.py",line5,in<module>print(tup)NameError:name'tup'isnotdefined刪除后的元組tup:輸出結(jié)果:[Deletetuple]12345tup=('Deris','Weng',1,2)
print(tup)
deltup;
print("Deletedtupletup:")
print(tup)[Example]Theelementsinthetuplearenotallowedtobedeleted,onlythewholetuplecanbedeletedTraceback(mostrecentcalllast):('Deris','Weng',1,2)File"C:/delTup.py",line5,in<module>print(tup)NameError:name'tup'isnotdefinedDeletedtupletup:KnowledgePointsOutputresult:元組的特點正好可以“彌補(bǔ)”Python沒有常量的“遺憾”,程序中不需要修改的數(shù)據(jù)都可以聲明在元組中。問題:為什么要設(shè)計元組?提問【元組基礎(chǔ)】Thecharacteristicsoftuplescanjust"makeup"the"regret"thatPythondoesnothaveconstants.Datathatdoesnotneedtobemodifiedintheprogramcanbedeclaredintuples.Question:Whydesigntuples?Askaquestion[TupleBasics]Python語言程序設(shè)計【元組運算符+索引與截取+內(nèi)置函數(shù)與方法】PythonLanguageProgramming[TupleOperators+IndexingandIntercepting+Built-inFunctionsandMethods]
+
和
*
的操作符+號用于組合,*號用于重復(fù)知識點【元組運算符】12345print(len((1,2,3)))#計算元組元素個數(shù)
print((1,2,3,4)+(5,6))#元組的拼接
print(5in(1,2,3,4,5,6))#元素是否存在于元組中
forxin(1,2,3):#元組的迭代
print(x,end="")【例】元組運算符3(1,2,3,4,5,6)True123輸出結(jié)果:
+and*operatorsThe+signisforcombinations,the*signisforrepetitionsKnowledgePoints[Tupleoperator]12345print(len((1,2,3)))#Calculatethenumberoftupleelements
Print((1,2,3,4)+(5,6))#Splicingoftuples
print(5in(1,2,3,4,5,6))#Whethertheelementexistsinthetuple
forxin(1,2,3):#Iterationoftuples
print(x,end="")[Example]Thetupleoperator3(1,2,3,4,5,6)True123Outputresult:知識點【元組運算符】12print(('Happy!',)*3)#有逗號(,)的情況下實現(xiàn)元組的重復(fù)倍增
print(('Happy!')*3)#沒有逗號(,),不認(rèn)為是元組?!纠吭M的使用('Happy!','Happy!','Happy!')Happy!Happy!Happy!輸出結(jié)果:12print(('Happy!',)*3)#Repeatmultiplicationoftupleswithcommas(,)
print(('Happy!')*3)#Thereisnocomma(,)anditisnotconsideredasatuple.[Example]Theuseoftuples('Happy!','Happy!','Happy!')Happy!Happy!Happy!KnowledgePoints[Tupleoperator]Outputresult:知識點【元組索引與截取】1234Tup=('Deris','Happy','Weng')
print(Tup[2])#讀取第三個元素
print(Tup[-2])#反向讀取;讀取倒數(shù)第二個元素
print(Tup[1:])#截取元素,從第二個開始后的所有元素【例】索引與截取WengHappy('Happy','Weng')輸出結(jié)果:KnowledgePoints[TupleIndexingandInterception]1234Tup=('Deris','Happy','Weng')
print(Tup[2])#Readthethirdelement
print(Tup[-2])#Reversereading;Readthepenultimateelement
print(Tup[1:])#Interceptelements,allelementsafterthesecondone[Example]IndexingandInterceptionWengHappy('Happy','Weng')Outputresult:課后練習(xí)【元組截取與拼接】問題:
tup1=(1,2,3,4,5,6,7)print("tup1[0]:",tup1[0])print("tup1[1:5]:",tup1[1:5])結(jié)果分別是什么?After-schoolexercises[Tupleinterceptionandsplicing]Question:tup1=(1,2,3,4,5,6,7)print("tup1[0]:",tup1[0])print("tup1[1:5]:",tup1[1:5])Whatweretheresultsrespectively?課后練習(xí)【元組截取與拼接】Python表達(dá)式結(jié)果描述len((1,2,3))長度(1,2,3)+(4,5,6)組合('Hi!',)*4重復(fù)3in(1,2,3)元素是否存在于列表中3(1,2,3,4,5,6)('Hi!','Hi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度水陸聯(lián)運貨物保險及運輸合同
- 二零二五年度新能源儲能技術(shù)聘用合同8篇
- 二零二四年度信息化設(shè)備融資租賃管理合同3篇
- 課件:正確認(rèn)識高職院校內(nèi)部質(zhì)量保證體系診斷與改進(jìn)
- 二零二五年度牧草生物質(zhì)能項目合作協(xié)議4篇
- 2025版農(nóng)家樂民宿租賃管理服務(wù)合同2篇
- 二零二五版年薪制勞動合同:房地產(chǎn)企業(yè)銷售精英激勵方案4篇
- 第三單元 資產(chǎn)階級民主革命與中華民國的建立(解析版)- 2023-2024學(xué)年八年級歷史上學(xué)期期中考點大串講(部編版)
- 2025年度個人家政服務(wù)分期支付合同范本2篇
- 二零二五年度地鐵車站安全門系統(tǒng)采購合同
- 2024年蘇州工業(yè)園區(qū)服務(wù)外包職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性測試歷年參考題庫含答案解析
- 人教版初中語文2022-2024年三年中考真題匯編-學(xué)生版-專題08 古詩詞名篇名句默寫
- 2024-2025學(xué)年人教版(2024)七年級(上)數(shù)學(xué)寒假作業(yè)(十二)
- 山西粵電能源有限公司招聘筆試沖刺題2025
- ESG表現(xiàn)對企業(yè)財務(wù)績效的影響研究
- 醫(yī)療行業(yè)軟件系統(tǒng)應(yīng)急預(yù)案
- 使用錯誤評估報告(可用性工程)模版
- 《精密板料矯平機(jī) 第2部分:技術(shù)規(guī)范》
- 黑枸杞生物原液應(yīng)用及產(chǎn)業(yè)化項目可行性研究報告
- 2024年黑龍江省政工師理論知識考試參考題庫(含答案)
- 四年級上冊脫式計算300題及答案
評論
0/150
提交評論