Wednesday, 16 June 2021

CLASS 11 CS CHAPTER 9

 

CLASS -11  COMPUTER  SCIENCE

                        CHAPTER-  9  DICTIONARIES  

🔔To download textbook pdf click on the ''DOWNLOAD'' button below -




NOTES FOR COMPLETE UNDERSTANDING -

  • Dictionary  too is a collection of data like a string, list and tuple.

  • It is a very versatile data type.

  • It has key : value pair in it.

  • Dictionaries are mutable data types and it is an unordered collection in the form of   key : value .

  • In Tuple and List, index of a value is used for indexing whereas in dictionary, key of a value is used.

  • To create any dictionary, it is required to collect pairs of key:value in  “{ }” . 

  •  Example:                                                                                                                          teachers={“Rashi”:”Mathematics”, “Arti”:”Physics” , ”Priyanka”:”Chemistry" , ”Sakshi”:”CS”}

  • Some examples of Dictionary are :
  • Dict1= { }            # this is an empty dictionary without any element.

  • NoOfDaysInMonth= { January”:31, ”February”:28, ”March”:31, ”April”:30, ”May”:31, ”June”:30, ”July”:31, ”August”:31, ”September”:30, ”October”:31, ”November”:30, ”December”:31}  
  •  The thing to note is that keys should always be of immutable type.

  • Note: Dictionary is also referred as associative array or mapping or hashes . Keys should always be of immutable type. If you try to make keys as mutable, python will show error in it. 

  • Accessing a Dictionary :
  • To access a value from dictionary, we use key of it's key:value pair same as we use an index to access a value from a list or tuple.
  • We get the key from the pair of Key: value.
  • If we input following statement from above example-  
  •  >>>print(teachers['Rashi'])                                                                                       We have selected key “Rashi” and on printing it, Mathmatics got printed.                      

  • Traversal of a Dictionary :
  • For traversing a Dictionary, we use for loop. 
  • Example :    >>>for k in d:                                                                                                                                    print(d[k]) 
  • To access key and value we need to use keys( ) and values( ) function :
  • d.keys( ) function will display only key.
  • d.values ( ) function will display values of the dictionary only .

  • Features of Dictionary : 

  • 1. Unordered set: Dictionary is collection of key:value pairs in unordered manner . 

  • 2. Not a sequence: Unlike list, string and tuple , it is a collection of unordered elements whereas a sequence is a collection of indexed numbers to keep them in order. 

  • 3. Key of  any key:value pair is used for  indexing because according to Python key can only be of immutable type. Strings & numbers are  immutable type of data and therefore can be used as a key. Key of a Dictionary should always be of immutable type like integer or string or tuple whereas value of a dictionary can be of both mutable and immutable type.

  • 4. Keys should be unique : Because keys are used to identify values just like indices are used , so they should be unique. 

  • 5. Values of two unique keys can be same but this is not allowed for values.

  •  6. Dictionary is mutable hence we can update value of any key. 

  • 7.  Internally it is stored as a mapping. Its key : value are related to each other by an internal function called hash function.  Such procedure of linking is knows as mapping.                                                                                                                     **Hash-function is a type of  internal algorithm to link a variable and its value.

  • Working with Dictionary:

  • Dictionary initialization- For initialisation of dictionary we keep collection of pairs of key : value separated by comma (,) and then place this collection inside “{ }”.

  • Adding  key:value  pair to an empty dictionary. There exists 2 ways to create an empty dictionary :
  • 1. Employee = { } 
  • 2. Employee = dict( ) 
  • 3. Creating a Dictionary with the pair of name and value:                                          dict( ) constructor of python is used to create dictionary with the pairs of key and value. There are various methods for this--                                                                                                                 I. By passing Key:value pair as an argument.                                                               II. By specifying Comma-separated key : value pair .                                                III. By specifying Keys and values separately: For this, we use zip() function in dict ( ) constructor .                                                                                                             IV. By giving Key : value pair in the form of separate sequence .

  • Updation in a Dictionary following syntax is used to update an element in >>>Dictionary[key]=new value

  • Deleting an element from a Dictionary follows two syntaxes  For deletion, key should be there otherwise python will give error.                                                                                1. del ]- it deletes the value and does not return deleted value.                                2. .pop() it returns the deleted value after deletion.

  • Detection of an element from a Dictionary :  
  • Membership operator is used to detect presence of an element in a Dictionary.      1. in :  It gives true on finding the key otherwise gives false.                                      2. not in  : It gives true on not finding the key otherwise gives false.                             *** in and not in does not process on values, they can only work with keys.

  • Pretty Printing of a Dictionary :
  • To print a Dictionary in a beautified manner, we can  import json module.             After this following syntax of dumps( ) function will be used.                                                                                                           json.dumps(< >,indent= ' < < >')

  • Dictionary Function and Method : 

  • 1. len( ) Method : it specifies the length of dictionary. 

  • 2. clear( ) Method : it deletes every key:value pairs of  the dictionary. 

  • 3. get( ) Method : it outputs value of the given key. 

  • 4. items( ) Method : it returns all items of a dictionary in the form of  tuple  of (key : value). 

  • 5. keys( ) Method : it outputs list of dictionary keys. 

  • 6. values( ) Method : it outputs list of dictionary values.

  • 7. Update ( ) Method: This function of python is used to  merge the pair of key : value of a dictionary into other dictionary. Change and addition in this is possible as per need.  
        🔔MORE MATERIALS TO BE UPLOADED STAY TUNED🔔

WHY Software Developer ?