python列表 元組 字串

2021-09-06 15:49:18 字數 1549 閱讀 3566

python中列表、元組、字串均為可迭代物件:

lst =[1

,2,3

,4]for i in lst:

print

(i,end=

' ')

print()

tu =(1

,2,3

,4)for i in tu:

print

(i,end=

' ')

print()

str =

'1234'

for i in str:

print

(i,end=

' ')

print

()

1 2 3 4 

1 2 3 4

1 2 3 4

python中的列表為可修改物件,對列表的大多數操作可以在原列表上直接進行。列表底層為包含物件引用陣列長度陣列首位址的結構體,因而改變列表的長度並不銷毀列表物件。

lst =

print

(f'id of lst is : ;lst : '

)lst.

('a'

)print

(f'id of lst is : ;lst : '

)

id of lst is : 1787675887304 ;lst : 

id of lst is : 1787675887304 ;lst : ['a']

python中的tuple、string均為不可修改物件。所有修改string值的操作均會返回新的物件,原物件不變;無法通過下標修改tuple和string

str =

'abcde'

print

(f'id of str : ;str : '

)str1=str.

capitalize()

print

(f'id of str1: ;str1: '

)print

(f'id of str : ;str : '

)

id of str : 2461927781688 ;str : abcde

id of str1: 2461930351840 ;str1: abcde

id of str : 2461927781688 ;str : abcde

tu =(1

,2,3

)tu[1]

=5

traceback (most recent call last):

file "c:/pycharmprojects/practice.py", line 253, in tu[1]=5

typeerror: 'tuple' object does not support item assignment

python 列表 元組 字串

列表新增 list.extend list.insert 列表刪除 list.remove 刪除某乙個元素 list.pop 刪除某乙個返回刪除的哪乙個,預設刪除最後乙個 del list 這是個語句也可以刪除整個元素,加括號刪除某乙個元素 列表分片 list 1 3 從第二個到第四個切割,並顯示出...

字串,元組,列表

共異點 字串列表 元組拼接 a 1 b 2 a b a 1 b 2 a b a 1 b 2 a b 重複a 1 a 3 a 1 3 元組不可以重複 索引sr 123 sr 1 li 1 2 li 0 tp 1 2 tp 0 切片sr 123 sr 0 2 li 1 2 li 0 1 tp 1 2 t...

列表 元組 字串

容器資料型別 列表元組 字串1 列表 列表是有序集合,沒有固定大小,能夠儲存任意數量任意型別的 python 物件,語法為 元素1,元素2,元素n 關鍵點是 中括號 和 逗號 中括號 把所有元素綁在一起 逗號 將每個元素一一分開 2 元組 元組 定義語法為 元素1,元素2,元素n 小括號把所有元素綁...