Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 140 - 2615_KEC
P. 140

    Output
                       {}
                       {}

                      ACCESSING ELEMENTS OF A DICTIONARY
                      To access the elements of a dictionary, you need the key defined in the key:value pairs. You can

                      also use the get( ) method for fetching the value of a particular key of a dictionary.
                      The Syntax to access a dictionary is as follows:

                      <dictionary-name>[<key>].

                      Program 11: A program to access elements of a dictionary.

                          Program11.py
                       File  Edit  Format    Run   Options   Window    Help

                       book = {
                           "title": "Python Basics",
                           "author": "Riya Sharma",
                           "price": 450,
                           "publisher": "TechWorld"
                       }

                       print("Book Details:", book)

                       print("Title of the book:", book["title"])
                       print("Author of the book:", book["author"])

                          Output

                       Book Details: {'title': 'Python Basics', 'author': 'Riya Sharma', 'price': 450,
                       'publisher': 'TechWorld'}
                       Title of the book: Python Basics
                       Author of the book: Riya Sharma

                      MODIFYING A DICTIONARY
                      Since dictionaries are mutable, you can change existing values or add new key–value pairs after

                      the dictionary is created.

                      138   Premium Edition-VIII

| 135 136 137 138 139 140 141 142 143 144 145 |