Menu
Coddy logo textTech

בדיקה אם מפתח קיים

שיעור 8 מתוך 14 בקורס מילונים ב-Python של Coddy.

כדי לקבוע אם מפתח מסוים נמצא במילון, השתמשו במילת המפתח in,

my_dict = {"name": "Alice", "age": 30, "city": "New York"}
print("name" in my_dict)  # בודק אם מפתח קיים, מחזיר True
challenge icon

אתגר

קל

ניתן לך מילון המייצג אוסף של מילים והמשמעויות שלהן. המשימה שלך היא לכתוב פונקציה בשם find_meaning שבודקת אם מילה נתונה קיימת במילון.

נסו בעצמכם

# המילון של המילים והמשמעויות שלהן
word_meanings = {
    "apple": "a fruit",
    "book": "a written or printed work",
    "car": "a motor vehicle with four wheels",
    "dog": "a domesticated mammal"
}

# הקוד שלך כאן

# אין לשנות את מה שמתחת לשורה זו
print("Does 'book' exist in the dictionary?", find_meaning("book"))
print("Does 'cat' exist in the dictionary?", find_meaning("cat"))
print("Does 'dog' exist in the dictionary?", find_meaning("dog"))

כל השיעורים ביחידה מילונים ב-Python

תרגלו בעצמכם: קומפיילר Python אונליין