Python辞書get()メソッド
例
「モデル」アイテムの値を取得します。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("model")
print(x)
定義と使用法
このget()
メソッドは、指定されたキーを持つアイテムの値を返します。
構文
dictionary.get(keyname, value)
パラメータ値
Parameter | Description |
---|---|
keyname | Required. The keyname of the item you want to return the value from |
value | Optional. A value to return if the specified key does not exist. Default value None |
その他の例
例
存在しないアイテムの値を返すようにしてください。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("price", 15000)
print(x)