Page 93 - Modular v1.1 Pyhton
P. 93

09                              DICTIONARY IN



                                                                                                     PYTHON















                         Your Aim

                         to learn about:
                                  Creating a Dictionary                         Accessing a Dictionary
                                   Python Methods                                Python Functions
                                    Updating a Dictionary                        Removing or Deleting Elements of a Dictionary
                                     Some More Programs




                 A dictionary in Python is another data type which contains a collection of values in the form of key

                 value pairs. Instead of using indexing, it uses keys to access the elements. It is a heterogeneous
                 collection of data elements, where key must be unique and immutable. All the elements of a
                 dictionary are enclosed within a pair of curly brackets {}.
                 In the previous chapters, you have learned Python lists and tuples. In this chapter you will learn
                 about Python dictionary, how to create a dictionary and operations performed on it.

                     CREATING A DICTIONARY

                 To create a dictionary in Python use the following syntax:

                 d = {key1: value1, key2 : value2, ... }
                 Example:

                 d2 = {’width’: 8.5, ’height’: 11}

                 d3 = {1: ’RED’, 2: ’GREEN’, 3: ’BLUE’, }
                 Program 1: To create a dictionary.



















                                                                                           Dictionary in Python   91
   88   89   90   91   92   93   94   95   96   97   98