python 列表 元組 字典 迴圈

2021-08-08 17:33:22 字數 711 閱讀 8762

print(fruit)

print(fruit)

#insert()在索引位置增加元素

fruit.insert(1,'pear')

print(fruit)

#pop()刪除索引位置元素

fruit.pop(1)

print(fruit)

執行結果

>>> tup1 = (50,)

>>> type

(tup1)

>>> tup2 = (50)

>>> type

(tup2)

= ['alice','bob','tracy']

for name in l:

print('hello,%s'

%name)

執行結果

hello,alice

hello,bob

hello,tracy

第二種迴圈是while迴圈,只要條件滿足,就不斷迴圈,條件不滿足時退出迴圈

break退出迴圈,continue退出當前迴圈

>>> d = 

>>> d['michael']

95

dict的key必須是不可變物件,如字串,元組等,列表就不行

python列表元組字典

1.列表的資料項不需要具有相同的型別 建立乙個列表,只要把逗號分隔的不同的資料項使用方括號括起來即可 list1 google runoob 1997 2000 print list 0 list 0 2.列表的增 刪 改 查 insert delete update query 增 list.in...

python 列表,元組,字典

python中,有3種內建的資料結構 列表 元組和字典。1.列表 list是處理一組有序專案的資料結構,即你可以在乙個列表中儲存乙個序列的專案。列表中的專案。列表中的專案應該包括在方括號中,這樣python就知道你是在指明乙個列表。一旦你建立了乙個列表,你就可以新增,刪除,或者是搜尋列表中的專案。由...

python 列表,元組,字典

list a a b b c c for x in list 輸出的是列表裡的每乙個元素 print x for x y in list 輸出的是每個元組中的每個元素 print x,y for x y in enumerate list 輸出的是每個索引和索引對應的元素 print x,y lis...