




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Python语言程序设计【列表基础】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])【例】使用下标索引来访问列表aList[0]:DerisbList[1:5]:[2,3,4,5]输出结果: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输出结果:[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结果为:",aList)#列表中有5个元素【例】向列表插入元素aList结果为:['Deris','Weng','Happy',1,2]输出结果:[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]输出结果:[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]
#列表本身不包含数据,而是包含变量:aList[0]到aList[3]
first=aList[0]#拷贝列表,创建新的变量引用,而不是数据对象的复制。
delaList[0]#del删除的是变量,而不是数据。
print(aList)
print(first)【例】del语句作用在变量上,而不是数据对象上['Weng',1,2]Deris输出结果: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输出结果:[Traversinglists,secondaryindexes]123aList=['Deris','Weng',1,2]
foriinaList:
print(i)[Example]IterationofalistDerisWeng12KnowledgePointsOutputresult:知识点【遍历列表、二级索引】12aList=['Deris','Weng',[1,2,3]]
print(aList[2][0])【例】列表的二级索引1输出结果:12aList=['Deris','Weng',[1,2,3]]
print(aList[2][0])[Example]Secondaryindexofalist1[Traversinglists,secondaryindexes]KnowledgePointsOutputresult:Python语言程序设计【索引的使用+元素数量+列表运算符】PythonLanguageProgramming[Useofindexes+numberofelements+listoperators]索引俗称下标。索引值从0开始,直到长度减1位置(例如对10个元素的列表,最大的索引值为[9])。
知识点【索引的使用】12aList=[1,2,3,4,5,6,7]
print("aList[3]:",aList[3])【例】使用下标索引来访问列表aList[3]:4输出结果: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输出结果: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:函数len()求集合中的元素数量知识点【求元素数量】1234x=[1,2,3,4,5,6]
y=[]
print(len(x))
print(len(y))【例】函数len()60输出结果:Functionlen()tofindthenumberofelementsintheset[Numberofelementstobefound]1234x=[1,2,3,4,5,6]
y=[]
print(len(x))
print(len(y))[Example]Functionlen()60KnowledgePointsOutputresult:
+
和
*
的操作符+号用于组合列表,*号用于重复列表知识点【列表运算符】12345print([1,2,3,4]+[5,6])#列表的拼接
print(5in[1,2,3,4,5,6])#元素是否存在于列表中
print(['Weng']*3)#列表的重复倍增
forxin[1,2,3]:#列表的迭代
print(x,end="")【例】列表运算符[1,2,3,4,5,6]True['Weng','Weng','Weng']123输出结果:
+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语言程序设计【列表函数&方法】PythonLanguageProgrammingListFunctions&Methods知识点【列表函数】1.列表函数函数描述len(list)计算列表元素个数max(list)返回列表元素最大值min(list)返回列表元素最小值list(seq)将元组转换为列表12345a=['Hello','Deris','Weng']
n=[1,2,3]
print(len(a))
print(max(a))
print(min(n))【例】列表函数的使用3Weng1输出结果: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:知识点【列表函数】1234a=['Hello','Deris','Weng']
n=[1,2,3]
x=[a,n]
print(min(x))【例】列表函数的错误使用Traceback(mostrecentcalllast):File"ListError.py",line4,in<module>print(min(x))TypeError:'<'notsupportedbetweeninstancesof'int'and'str'输出结果: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)统计某个元素在列表中出现的次数list.extend(seq)在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)list.index(obj)从列表中找出某个值第一个匹配项的索引位置list.insert(index,obj)将对象插入列表list.pop(obj=list[-1])移除列表中的一个元素(默认最后一个元素),并且返回该元素的值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])对原列表进行排序list.clear()清空列表list.copy()复制列表list.sort([func])对原列表进行排序12345a=['Hello','Deris','Weng']
a.append('Happy')
print(a)
a.reverse()
print(a)【例】列表方法的使用['Hello','Deris','Weng','Happy']['Happy','Weng','Deris','Hello']输出结果: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语言程序设计【元组基础】PythonLanguageProgramming[TupleBasics]知识点【声明元组】1234tup1=(50)
print(type(tup1))#不加逗号,类型为整型
tup1=(50,)
print(type(tup1))#加上逗号,类型为元组【例】只包含一个元素的元组<class'int'><class'tuple'>输出结果:创建空元组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)输出结果:[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')输出结果:123tup1=(12,34.56)
#以下修改元组元素操作是非法的。
tup1[0]=100【例】非法访问元组Traceback(mostrecentcalllast):File"tupError.py",line3,in<module>tup1[0]=100TypeError:'tuple'objectdoesnotsupportitemassignment输出结果:元组自身不能修改[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:输出结果:[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:元组的特点正好可以“弥补”Python没有常量的“遗憾”,程序中不需要修改的数据都可以声明在元组中。问题:为什么要设计元组?提问【元组基础】Thecharacteristicsoftuplescanjust"makeup"the"regret"thatPythondoesnothaveconstants.Datathatdoesnotneedtobemodifiedintheprogramcanbedeclaredintuples.Question:Whydesigntuples?Askaquestion[TupleBasics]Python语言程序设计【元组运算符+索引与截取+内置函数与方法】PythonLanguageProgramming[TupleOperators+IndexingandIntercepting+Built-inFunctionsandMethods]
+
和
*
的操作符+号用于组合,*号用于重复知识点【元组运算符】12345print(len((1,2,3)))#计算元组元素个数
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输出结果:
+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)#有逗号(,)的情况下实现元组的重复倍增
print(('Happy!')*3)#没有逗号(,),不认为是元组。【例】元组的使用('Happy!','Happy!','Happy!')Happy!Happy!Happy!输出结果: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])#反向读取;读取倒数第二个元素
print(Tup[1:])#截取元素,从第二个开始后的所有元素【例】索引与截取WengHappy('Happy','Weng')输出结果: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:课后练习【元组截取与拼接】问题:
tup1=(1,2,3,4,5,6,7)print("tup1[0]:",tup1[0])print("tup1[1:5]:",tup1[1:5])结果分别是什么?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?课后练习【元组截取与拼接】Python表达式结果描述len((1,2,3))长度(1,2,3)+(4,5,6)组合('Hi!',)*4重复3in(1,2,3)元素是否存在于列表中3(1,2,3,4,5,6)('Hi!','Hi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 职场应用写作与交流(二)(课件)-中职语文高二同步课件(高教版2023职业模块)
- 2025年教育改革:构建中学生心理健康教案新体系
- 踏春活动总结(8篇)
- 2025年心理辅导:作文看烧菜教你应对压力
- 2025年中考第一次模拟考试生物(青海西宁卷)
- 股份制企业合作发展策略建议书
- 零件数据采集与逆向工程 习题答案 任务一 回转体零件的逆向建模
- 2025年威海货运从业资格证考试模拟
- 2025年鞍山货运从业资格证模拟考试题
- 2025年大连货运上岗证模拟考试
- 考察领导谈话怎么评价领导【六篇】
- 无侧限抗压强度试验记录
- 钳形电流表使用PPT
- 建筑工程分部分项工程划分表(新版)
- 福建省危险化学品企业安全标准化(三级)考核评分标准指导意见(试行)
- 上海市长宁区2022年高考英语一模试卷(含答案)
- 城镇详细设计控制性详细规划
- 智能垃圾桶系统的设计论文
- 质量管理体系过程识别矩阵图及与条款对照表
- 北碚区幼儿园
- 2021年度锚索张拉机具及锚杆拉力计技术规格书
评论
0/150
提交评论