Python Dictionary ได้รับ () Method
ตัวอย่าง
รับค่าของรายการ "รุ่น":
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)