Python基础学习lesson6

Python第六课的基础知识02:52来自LearningYard学苑

01Python的序列结构

有序序列:序列中元素可进行索引查找

无序序列:序列中元素没有顺序,无法用索引查找

可变序列:对其中的对象可进行增\删\改\排序操作

不可变序列:无法对序列中元素进行任何操作,无法更新序列

A[列表]--X((有序序列));

B[元组]--X;

C[字符串]--X;

F[range]--X;

D[字典]--Y((无序序列));

E[集合]--Y;

A[列表]--M((可变序列));

D[字典]--M;

E[集合]--M;

B[元组]--N((不可变序列));

C[字符串]--N;

F[range]--N;

Orderedsequence:theelementsinthesequencecanbeindexedandsearched

Unorderedsequence:Theelementsinthesequencehavenoorderandcannotbesearchedbyindex

Variablesequence:add/delete/modify/sorttheobjectsinit

Immutablesequence:unabletoperformanyoperationsontheelementsinthesequence,unabletoupdatethesequence

02列表、元祖:数据收纳盒

用来收纳数据对象的数据类型

以一种规则的下标索引方式name[index]访问到每个数据

列表:方括号法[];指明类型法list()

元组:圆括号法();指明类型法tuple()

列表或元组中保存各个数据对象没有类型限制

列表增长:append();insert();extend()

列表缩减:pop();remove();clear()

重新组织:

reverse()/sort():将原列表按头围反转/将原列表按元素大小重新排序

reversed()/sorted():得到重新排列的列表,不影响原列表

Thedatatypeusedtostorethedataobject

Accesstoeachdatainaregularsubscriptindexmethodname[index]

List:squarebracketmethod[];specifytypemethodlist()

Tuple:parenthesismethod();specifiedtypemethodtuple()

Thereisnotypelimitforsavingeachdataobjectinalistortuple

Listgrowth:append();insert();extend()

Listreduction:pop();remove();clear()

Reorganize:

reverse()/sort():Reversetheoriginallistbyheadcircumference/resorttheoriginallistbyelementsize

reversed()/sorted():gettherearrangedlistwithoutaffectingtheoriginallist

例:

合并:

加法+:连接两个列表/元组

乘法*:复制n次,生成新列表/元组

长度len():列表/元组中元素的个数

索引:name[i]

切片:name[start:end:step]

查找:

in():判断某个元素是否存在于列表/元组中

index():指定的数据在列表/元组的索引

count():指定的数据在列表/元组中出现的次数

计算:

sum():列表中所有数据元素累加

min()/max():返回列表中最小/最大的数据元素

merge:

Addition+:concatenatetwolists/tuples

Multiplication*:Copyntimestogenerateanewlist/tuple

Lengthlen():thenumberofelementsinthelist/tuple

Index:name[i]

Slice:name[start:end:step]

Find:

in():Determinewhetheranelementexistsinthelist/tuple

index():Theindexofthespecifieddatainthelist/tuple

count():Thenumberoftimesthespecifieddataappearsinthelist/tuple

Calculation:

sum():accumulatealldataelementsinthelist

min()/max():returnsthesmallest/largestdataelementinthelist

03列表

1.列表list是包含若干元素的有序连续内存空间

2.当列表增加或删除元素时,列表对象自动进行内存扩展或收缩

3.在非尾部位置插入或删除元素时会改变该位置后面的元素的列表索引

4.尽量从列表尾部进行元素的追加或删除操作

5.列表所有元素放在[]中,相邻元素间用,隔开。例如[1,2,3]

6.列表中元素数据类型可以各不相同,[]表示空列表。例如[1,’a’,{‘a’:1},(m,n),[1,’e’]]

7.python采用基于值的内存管理模式,变量不直接存储值,列表中元素是值的引用

8.列表的创建与删除

使用=直接将列表赋值给变量即可创建列表对象。

1.Listlistisanorderedcontiguousmemoryspacecontainingseveralelements

2.Whenthelistaddsordeleteselements,thelistobjectautomaticallyexpandsorshrinksitsmemory

3.Wheninsertingordeletinganelementatanon-tailposition,thelistindexoftheelementbehindthepositionwillbechanged

4.Trytoappendordeleteelementsfromtheendofthelist

5.Allelementsofthelistareplacedin[],separatedbyadjacentelements.Forexample[1,2,3]

6.Thedatatypesoftheelementsinthelistcanbedifferent,[]meansanemptylist.Forexample[1,’a’,{‘a’:1},(m,n),[1,’e’]]

7.Pythonusesavalue-basedmemorymanagementmode,variablesdonotdirectlystorevalues,andtheelementsinthelistarereferencestovalues

8.Listcreationanddeletion

Use=todirectlyassignthelisttoavariabletocreatealistobject

例:

使用list()把元组、range对象、字符串、字典、集合或其他科迭代对象转换为列表

Uselist()toconverttuples,rangeobjects,strings,dictionaries,collections,orotherscientificiterationobjectsintolists

当一个列表不再使用时,可以使用del命令将其删除,这一点适用于所有类型的Python对象

对象删除后无法再访问,抛出异常

Whenalistisnolongerused,youcanusethedel


转载请注明:http://www.aierlanlan.com/rzfs/840.html

  • 上一篇文章:
  •   
  • 下一篇文章: 没有了