Python3 教程 字典

2021-10-23 08:20:44 字數 1383 閱讀 8318

字典是另一種可變容器模型,且可儲存任意型別物件

字典的每個鍵值 key=>value 對用冒號 : 分割,每個鍵值對之間用逗號 , 分割,整個字典包括在花括號 {} 中 ,格式如下所示:

d =

dict

=print

("dict['name']: "

,dict

['name'])

print

("dict['age']: "

,dict

['age'

])

dict[

'name'

]: zara

dict[

'age'

]: 7

dict

=dict

['age']=

8# 更新

dict

['school']=

"runoob"

# 新增

print

("dict['age']: "

,dict

['age'])

print

("dict['school']: "

,dict

['school'

])

dict[

'age'

]: 8

dict[

'school'

]: runoob

dict

=del

dict

['name'

]# 刪除鍵是'name'的條目

dict

.clear(

)# 清空字典所有條目

deldict

# 刪除字典

print

("dict['age']: "

,dict

['age'])

print

("dict['school']: "

,dict

['school'

])

traceback (most recent call last):

file "e:/python/python/python06.py", line 20, in

print(

"dict['age']: ", dict[

'age'])

typeerror: 'type' object is not subscriptable

訪問乙個空的資料字典,提示不存在

Python3基礎教程字典的使用

字典的關鍵字 dict 格式 注意 鍵是具有唯一性的,不能更改,通常使用字串和數字,也可以是元組 值可以是任何資料 數字 字串 列表 元組等資料格式 字典的操作 a print len a 可以獲取字典的長度 通過鍵獲取資料 也可以使用get的方式去獲取 1 直接通過鍵獲取 如果沒有值列印 就會報錯...

手機python3教程 python3基礎教程

python 基礎教程 python 是一種解釋型 物件導向 動態資料型別的高階程式語言。python 由guido van rossum 於年底發明,第乙個公開發行版發行於年。像 perl 語言一樣 python 源 同樣遵循 gpl gnu general public license 協議。現...

python3字典遍歷 python3字典遍歷

python版本 python3.7 info infog.get name 得到字典info中name的值 info.keys 得到字典info中所有的鍵,結果是乙個物件 dict keys name age 需要注意在python2中該操作得到的是乙個列表 遍歷key for temp in i...