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.
אחסן נתונים לפי שם
- מילון מאחסן ערכים תחת שמות הנקראים מפתחות.
- כתוב זוגות בתוך סוגריים:
{"name": "Sam", "age": 15}. - כל מפתח הוא ייחודי ומצביע לערך אחד.
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.
קבל והגדר ערכים
- קרא ערך עם המפתח שלו:
person["name"]. - הוסף או שנה ערך באותו אופן:
person["age"] = 16. - רשימה משתמשת באינדקס מספרי; מילון משתמש במפתח.
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.
מילון כרשום
- רשום מקבץ עובדות הקשורות לדבר אחד.
- מילון הוא דרך מסודרת לאחסון רשום.
- כל מפתח הוא שדה, כמו
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:מבקר בכל מפתח בתור.person.get("x", 0)מחזיר ערך ברירת מחדל אם המפתח חסר.
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.
בפסאודוקוד קמברידג'
- מילון מדגם את רשום הבדיקה
TYPE; כל מפתח הוא שדה הניתן לגישה עם נקודה.
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.
טעויות נפוצות
- לקרוא ערך לפי המפתח שלו:
d["name"]; מפתח חסר גורם ל-KeyError. - המפתחות הם ייחודיים — הצבת אותו מפתח שוב מחליפה את הערך.
- השתמש ב-
d.get(key)כאשר ייתכן שהמפתח חסר.
Now you try
- Build or update a dictionary in each task.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- בנה עדכן מילון בכל משימה.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
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. · צור מילון book עם שני מפתחות: title מוגדר ל-"Python" ו-pages מוגדר ל-200.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
The dictionary student already has name and score. Change score to 85, and add a new key passed set to True. · המילון student כבר מכיל את הname ואת הscore. שנה את הscore ל85, והוסף מקדם חדש passed שמוגדר לTrue.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Loop over the dictionary prices and add up its values. Store the total in total (the answer is 10). · לולאת על המילון prices וחבר את ערכיו. אחסן את הסך הכל ב-total (התשובה היא 10).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
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}. · כתוב count_words(text) שתחזיר מילון: כל מילה ב-text (בפיצול לפי רווחים) מקודדת למספר הפעמים שהיא מופיעה. count_words("the cat and the hat") מספק {"the": 2, "cat": 1, "and": 1, "hat": 1}.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.