Page 446 - ComputerScience_Class_11
P. 446

Dictionary is a data type that represents a collection of objects indexed by a "key" rather than by a numerical index
              (like 0, 1, 2).
              Syntax of dictionary is as follows:

              dictionary_name = { key1: value1, key2: value2, key3: value3}
                    Note: Instead of using numbers to find items (like in a list), you use a "Key" (a label) to find a "Value" (the
                    information).


              Program 13: To display the output of following code using mapping.
                   Program 13.py

                File  Edit  Format   Run   Options   Window    Help

                # Creating a dictionary of student details
                student = {
                    "name": "Rahul",
                    "age": 15,
                    "grade": "A"
                }
                # Printing the complete dictionary
                print(student)
                # Accessing value using key "name"
                print("Name of student:", student["name"])
                # Accessing value using key "age"
                print("Age of student:", student["age"])




                   Output

                {'name': 'Rahul', 'age': 15, 'grade': 'A'}
                Name of student:  Rahul
                Age of student:  15




              12.6.1 Characteristics of Dictionary data types in Python
              There are some characteristics of Dictionary data types in Python:
              •  Key–Value Pair Structure: A dictionary stores data in the form of key–value pairs. Each key is associated with a
                 specific value.
              •  Unordered Collection (Insertion Ordered in Modern Python): Dictionaries maintain insertion order (from Python
                 3.7 onward), but elements are accessed using keys, not index numbers.
              •  Mutable: Dictionaries are changeable. You can add, update or remove key–value pairs after creation.
              •  Unique Keys: Each key in a dictionary must be unique. Duplicate keys are not allowed.

              •  Values Can Be Duplicated: While keys must be unique, values can be repeated.
              •  Accessed by Keys: Elements are accessed using their keys instead of index positions.
              •  Can Store Different Data Types: Keys and values can be of different data types (e.g., string, integer, float, etc.).
              •  Dynamic Size: The size of a dictionary can grow or shrink as elements are added or removed.




                  444  Touchpad Computer Science (Ver. 3.0)-XI
   441   442   443   444   445   446   447   448   449   450   451