Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีได้ Value จาก Key ใน Python Dictionary

ได้ value จาก key ใน Python dictionary ด้วย operator [] ส่ง key เป็น argument

ตัวอย่าง

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
print(my_dict['name'])
print(my_dict['time'])

ผลลัพธ์

TutorialsPoint
15 years

อีกวิธีใช้เมธอด get บน dictionary:

ตัวอย่าง

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
print(my_dict.get('name'))
print(my_dict.get('time'))

ผลลัพธ์

TutorialsPoint
15 years