《python基礎(chǔ)教程》讀書筆記第二章-列表和元組

31.python的序列感覺和其他語言中的數(shù)組類似。初看類似,實(shí)則強(qiáng)大太多!

2.序列和元組的區(qū)別在于序列可以修改,元組不能。

3.序列用[]表示,元組用()表示

4.序列操作有:

索引(indexing)

>>> greeting = 'hello'

>>> greeting[0]

'h'

>>> greeting[-1]

'o'

>>> fourth=input('year:')[3]

year:2017

>>> fourth

'7'

分片(sliceing)

url=input('please enter the URL:')

domain = url[11:-4]

print("domain name :"+domain)

運(yùn)行結(jié)果:

please enter theURL:http://www.python.org

domain name :python

>>> number = [1,2,3,4,5,6,7,8,9,10]

>>> number[0:10:1]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> number[0:10:2]

[1, 3, 5, 7, 9]

加(adding)

乘(multiplying)

成員資格檢查

eg.

users = ['hello','hi','hyj']

test = input('enter you user name:')

if test in users:print('true')

計(jì)算長度

找最大元素

找最小元素

eg.

>>> numbers = [100,50,89]

>>> len(numbers)

3

>>> max(numbers)

100

>>> min(9,8,6,3,1,-1,10)

-1

1.元素賦值

2.元素刪除

>>> test = [1,2,3,4,5,6,7,8]

>>> del test[4]

>>> test

[1, 2, 3, 4, 6, 7, 8]

>>> del test[1:2]

>>> test

[1, 3, 4, 6, 7, 8]

2.分片賦值

>>> name = list('perl')

>>> name[3:]=list('test')

>>> name

['p', 'e', 'r', 't', 'e', 's', 't']

>>> name[2:2]=[' ',' ' ,' ' ]

>>> name

['p', 'e', ' ', ' ', ' ', 'r', 't', 'e', 's', 't']

3.列表方法

append

>>> a=[1,2]

>>> a.append(6)

>>> a

[1, 2, 6]


count

extend

>>> a = [1,2,3]

>>> b=[4,5,6]

>>> a.extend(b)

>>> a

[1, 2, 3, 4, 5, 6]

index

>>> a=[1,2,3,4,5,6,7,8,8,8,9]

>>> a.index(8)

7


insert

>>> a = ['s',"f",'rr',"ttt",7]

>>> a.insert(0,0)

>>> a

[0, 's', 'f', 'rr', 'ttt', 7]

>>> a.insert(1,"aa")

>>> a

[0, 'aa', 's', 'f', 'rr', 'ttt', 7]

>>> a.insert(8,9)

>>> a

[0, 'aa', 's', 'f', 'rr', 'ttt', 7, 9]


pop

>>> a=[1,2,3,4,5,6,7,8,9,0]

>>> a.pop()

0

>>> a

[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> a.pop(2)

3

>>> a

[1, 2, 4, 5, 6, 7, 8, 9]


remove

reverse

sort

>>> a=[2, 4, 5, 6, 7, 8, 9, 1]

>>> a.remove(9)

>>> a

[2, 4, 5, 6, 7, 8, 1]

>>> a.reverse()

>>> a

[1, 8, 7, 6, 5, 4, 2]

>>> a.sort()

>>> a

[1, 2, 4, 5, 6, 7, 8]

1.

迭代(iteration)

元組:不可變序列,除了不可變以外其他都和列表差不多

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容