Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 142 - 2615_KEC
P. 142

    Method                                           Explanation
                       clear()            Removes all elements from the dictionary, leaving it empty.
                       del                Removes a specific key-value pair from the dictionary.

                      Program 13: A program to demonstrate dictionary methods.

                          Program13.py
                       File  Edit  Format    Run   Options   Window    Help

                       library = {
                           "Book1": "AI Fundamentals",
                           "Book2": "Robotics Made Easy",
                           "Book3": "Data Science Basics"
                       }

                       print("Library Dictionary:", library)

                       print("Keys:", library.keys())
                       print("Values:", library.values())
                       print("Items:", library.items())

                       print("Get Book2:", library.get("Book2"))

                       # Updating dictionary
                       library.update({"Book4": "Python Programming"})
                       print("After Update:", library)

                          Output
                       Library Dictionary: {'Book1': 'AI Fundamentals', 'Book2': 'Robotics Made Easy',
                       'Book3': 'Data Science Basics'}
                       Keys: dict_keys(['Book1', 'Book2', 'Book3'])
                       Values:  dict_values(['AI Fundamentals',  'Robotics  Made Easy', 'Data Science
                       Basics'])
                       Items:  dict_items([('Book1',  'AI  Fundamentals'),  ('Book2',  'Robotics  Made
                       Easy'), ('Book3', 'Data Science Basics')])
                       Get Book2: Robotics Made Easy
                       After  Update:  {'Book1':  'AI  Fundamentals', 'Book2':  'Robotics  Made  Easy',
                       'Book3': 'Data Science Basics', 'Book4': 'Python Programming'}

                      140   Premium Edition-VIII

| 137 138 139 140 141 142 143 144 145 146 147 |