Kamus Python get() Metode
Contoh
Dapatkan nilai item "model":
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("model")
print(x)
Definisi dan Penggunaan
Metode get()
mengembalikan nilai item dengan kunci yang ditentukan.
Sintaksis
dictionary.get(keyname, value)
Nilai Parameter
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 |
Lebih Banyak Contoh
Contoh
Coba kembalikan nilai item yang tidak ada:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("price", 15000)
print(x)