Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

מילונים


מילון הוא סוג נתונים דומה למערכים, אך עובד עם מפתחות וערכים במקום אינדקסים. כל ערך המאוחסן במילון ניתן לגישה באמצעות מפתח, שהוא כל סוג של אובייקט (מחרוזת, מספר, רשימה וכו'), במקום להשתמש באינדקס שלו כדי לפנות אליו.

לדוגמה, ניתן לשמור מסד נתונים של מספרי טלפון באמצעות מילון כך:

לחלופין, ניתן לאתחל מילון עם אותם ערכים ברשומה הבאה:

איטרציה על מילונים

ניתן לבצע איטרציה על מילונים, בדיוק כמו רשימה. עם זאת, מילון, בניגוד לרשימה, אינו שומר על סדר הערכים המאוחסנים בו. כדי לבצע איטרציה על זוגות מפתח-ערך, השתמש בתחביר הבא:

הסרת ערך

כדי להסיר אינדקס ספציפי, השתמש באחת מהרשומות הבאות:

או:

תרגיל

Add "Jake" to the phonebook with the phone number 938273443, and remove Jill from the phonebook.

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy