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}. - كل مفتاح فريد ويشير إلى قيمة واحدة.
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.
المفتاح كمسجل
- المسجل (record) يجمع حقائق ذات صلة حول شيء واحد.
- المفتاح طريقة أنيقة لحفظ المسجل.
- كل مفتاح هو حقل، مثل
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. · اضغط تشغيل لرؤية المخرجات هنا.