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

ไวยากรณ์พื้นฐานในการเข้าถึงสมาชิก Python Dictionary

คุณสามารถเข้าถึงค่าใน dictionary เพื่อกำหนดให้ตัวแปรใน Python โดยใช้ออเปอเรเตอร์เข้าถึง []

ตัวอย่าง

my_dict = {
    'foo': 42,
    'bar': 12.5
}
new_var = my_dict['foo']
print(new_var)

ผลลัพธ์

42

คุณยังสามารถเข้าถึงค่าได้โดยใช้เมธอด get บน dictionary

ตัวอย่าง

my_dict = {
    'foo': 42,
    'bar': 12.5
}
new_var = my_dict.get('foo')
print(new_var)

ผลลัพธ์

42