Metoda get() słownika Pythona
Przykład
Uzyskaj wartość pozycji „model”:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("model")
print(x)
Definicja i użycie
Metoda get()
zwraca wartość elementu z określonym kluczem.
Składnia
dictionary.get(keyname, value)
Wartości parametrów
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 |
Więcej przykładów
Przykład
Spróbuj zwrócić wartość przedmiotu, który nie istnieje:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("price", 15000)
print(x)