7. Dictionaries
To map a key to a value, and to go through the keys in the order they were inserted.
Explanation
Section titled “Explanation”- A dictionary maps each key to a value:
{"سارة": 30}. You reach the value through its key:اعمار["سارة"]. - Assigning a value to a new key adds it to the dictionary.
لكل اسم في اعمار:goes through the keys in insertion order.مفاتيح(ق)(keys) returns them as a list in the same order.
Example
Section titled “Example”اعمار = {"سارة": 30, "علي": 25}اعمار["منى"] = 28لكل اسم في اعمار: اطبع(اسم, ": ", اعمار[اسم])اطبع(مفاتيح(اعمار))Common mistakes
Section titled “Common mistakes”Reading a key that does not exist is a runtime error. Check it with يحوي (contains) first:
ع = {"سارة": 30}اطبع(ع["علي"])Exit code: 3
In the error catalog: E0337
ع = {"سارة": 30}اذا يحوي(ع, "علي"): اطبع(ع["علي"])غير ذلك: اطبع("لا عمر لعلي")Exercise
Section titled “Exercise”Count how often each letter appears in «مرحبا بالعالم», ignoring the space, and print the count for each letter in the order it first appears.
Show the solution
كلمة = "مرحبا بالعالم"عداد قاموس[رمز, عدد64] = {}لكل ح في كلمة: اذا ح == ' ': تجاوز اذا يوجد(عداد, ح): عداد[ح] = عداد[ح] + 1 غير ذلك: عداد[ح] = 1لكل ح في عداد: اطبع(ح, ": ", عداد[ح])حرر(عداد)See also
Section titled “See also”Types · Builtins: مفاتيح and يحوي