Dictionaries (records)
Store data by name
- A dictionary stores values under names called keys.
- Write pairs inside braces:
{"name": "Sam", "age": 15}. - Each key is unique and points to one value.
เก็บข้อมูลด้วยชื่อ
- Dictionary เก็บค่าภายใต้ชื่อที่เรียกว่า keys
- เขียนคู่ภายในวงเล็บปีกกา:
{"name": "Sam", "age": 15} - แต่ละ key มีเอกลักษณ์และชี้ไปยังค่าเดียว
Get and set values
- Read a value with its key:
person["name"]. - Add or change a value the same way:
person["age"] = 16. - A list uses a number index; a dictionary uses a key.
ดึงและตั้งค่า
- อ่านค่าด้วย key ของมัน:
person["name"] - เพิ่มหรือแก้ไขค่าแบบเดียวกัน:
person["age"] = 16 - List ใช้ตัวเลข index; Dictionary ใช้ key
ages = {"Sam": 12, "Mia": 15}
print(ages["Sam"])
ages["Leo"] = 9
print(ages)
A dictionary as a record
- A record groups related facts about one thing.
- A dictionary is a neat way to hold a record.
- Each key is a field, like
nameorgrade.
Dictionary เป็น record
- Record กลุ่มข้อมูลที่สัมพันธ์กันเกี่ยวกับสิ่งใดสิ่งหนึ่ง
- Dictionary เป็นวิธีที่ดีในการจัดเก็บ record
- แต่ละ key เป็น field, เช่น
nameหรือgrade
pupil = {"name": "Ada", "grade": "A"}
print(pupil["name"])
print(pupil["grade"])
Check and loop
"name" in personisTrueif that key exists.for key in person:visits each key in turn.person.get("x", 0)returns a default if the key is missing.
ตรวจสอบและวนลูป
"name" in personเป็นTrueหากรายการนั้นมีอยู่for key in person:เยือนแต่ละ key ตามลำดับperson.get("x", 0)ส่งค่า default หาก key หายไป
menu = {"tea": 2, "cake": 5}
for item in menu:
print(item, menu[item])
print("tea" in menu)
In Cambridge pseudocode
- A dictionary models the exam's
TYPErecord; each key is a field reached with a dot.
ใน伪代码 Cambridge
- Dictionaryจำลอง exam's
TYPErecord; Each key is a field reached with a dot
TYPE Student
DECLARE Name : STRING
DECLARE Age : INTEGER
ENDTYPE
DECLARE Pupil : Student
Pupil.Name ← "Sam"
Pupil.Age ← 15
Common mistakes
- Read a value by its key:
d["name"]; a missing key raises a KeyError. - Keys are unique — assigning the same key again overwrites the value.
- Use
d.get(key)when the key might be missing.
ข้อผิดพลาดที่พบบ่อย
- อ่านค่าด้วย key ของมัน:
d["name"]; A missing key raises a KeyError - Keys เป็นเอกลักษณ์ — การ assign同一个keyอีกครั้งจะoverwrite ค่า
- ใช้
d.get(key)เมื่อ key อาจหายไป
Now you try
- Build or update a dictionary in each task.
- Press Check answer to test your code.
ลองดูเลย
- สร้างหรืออัปเดต dictionary ในแต่ละงาน
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Look up by key
A dictionary jumps straight to the value for a key — no scanning.
Create a dictionary book with two keys: title set to "Python" and pages set to 200.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The dictionary student already has name and score. Change score to 85, and add a new key passed set to True.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Loop over the dictionary prices and add up its values. Store the total in total (the answer is 10).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write count_words(text) that returns a dictionary: each word of text (split on spaces) mapped to how many times it appears. count_words("the cat and the hat") gives {"the": 2, "cat": 1, "and": 1, "hat": 1}.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่