dictionary.org
changeset 119 7dc53e6c8065
parent 117 d3005da44457
child 121 331c5fdc06d4
equal deleted inserted replaced
118:2f247bcfae8b 119:7dc53e6c8065
     1 * Lists
     1 * Dictionaries
     2 *** Outline
     2 *** Outline
     3 ***** Lists
     3 ***** Dictionaries
     4 ***** Tuples
     4 ***** Sets
     5 ***** Arsenal Required
     5 ***** Arsenal Required
     6 *** Script
     6 *** Script
     7     Welcome friends. 
     7     Welcome friends. 
     8     
     8     
     9     In previous tutorial we covered Lists and Tuples and related 
     9     In previous tutorial we covered Lists, Tuples and related 
    10     functions. In this session we will continue with Python supported
    10     functions. In this session we shall continue with Python
    11     data structures and cover Dictionaries and sets. We have already 
    11     data structures and cover Dictionaries and sets. We have already 
    12     covered some basics of Dictionaries in session on Statistics. Here
    12     covered some basics of Dictionaries in session on Statistics. Here
    13     we will revisit those concepts and some new ones. 
    13     we shall revisit those concepts and some new ones. 
    14     
    14     
    15     We give it a name and it should return a corresponding number. 
    15     We give it a name and it returns a corresponding number. 
    16     Dictionaries are just key-value pair. For each 'key' there is
    16     Dictionaries are just key-value pair. For each 'key' there is
    17     corresponding 'value'. In lists we have indexes to access elements,
    17     corresponding 'value' associated with it. In lists we use indexes 
    18     here we have 'key'. 
    18     to access elements, here we use the 'key'. 
    19     
    19     
    20     '{}' are used to create Python dictionaries. Lets start by opening 
    20     Lets start by opening IPython interpreter. 
    21     IPython interpreter. Lets create a dictionary say
    21     '{}' are used to create Python dictionaries. Lets create a dictionary say
    22 
    22 
    23     player = {'Mat': 134,'Inn': 233,
    23     player = {'Mat': 134,'Inn': 233,
    24     'Runs': 10823, 'Avg': 52.53}
    24     'Runs': 10823, 'Avg': 52.53}
    25     Its dictionary storing statistics of a cricket player.
    25     Its a dictionary storing statistics of a cricket player.
    26     Now to get the 'average' of this player we have to simply write
    26     Here 'Mat', 'Inn' etc are the keys. Now in order to get the 'average' of
       
    27     this player we simply type
    27     print player['Avg']
    28     print player['Avg']
    28     52.53
    29     52.53
    29 
    30 
    30     To add a new key-value to this dictionary we have to something like
    31     To add a new key-value pair to this dictionary we type
    31     player['Name'] = 'Rahul Dravid'
    32     player['Name'] = 'Rahul Dravid'
    32     print player    
    33     print player
    33     Please remember that Python dictionaries dont maintain the order
    34     Please note that Python dictionaries don't maintain the order
    34     in which the key-value pair are stored it might change as we add new
    35     in which the key-value pairs are stored. The order might change
    35     entries.
    36     as we add new entries.
    36 
    37 
    37     In dictionaries Duplicate keys are overwritten, that is when we do 
    38     In dictionaries Duplicate keys are overwritten, that is when we do 
    38     player['Mat'] = 139
    39     player['Mat'] = 139
    39     It wont create a new entry, rather it will simply overwrite previous
    40     It wont create a new entry, rather it will simply overwrite previous
    40     value with new one. So
    41     value with the new one. So
    41     print player
    42     print player
    42     will have updated value
    43     will have updated value
    43 
    44 
    44     As we covered in one of previous sessions to iterate through lists 
    45     As we covered in one of previous sessions 'for' can be used to iterate
    45     we use 'for'. In case of dictionaries we can iterate over them using 
    46     through lists. The same is possible in case of dictionaries too. We can
    46     'keys' for example
    47     iterate over them using the 'keys', for example:
    47     for key in player:
    48     for key in player:
    48         print key, player[key]
    49         print key, player[key]
    49     We saw how containership works in lists, there we can check if a 
    50     This prints the keys in the dictionary along with their corresponding 
    50     value is present in list or not, in case of Dictionaries it works
    51     values. Notice that the order is not the same as we entered it.
    51     only for keys. so
    52     
       
    53     We saw how containership works in lists. There we can check if a 
       
    54     value is present in a list or not but in case of Dictionaries we
       
    55     can only check for the containership of the keys. so
    52     'Inn' in player
    56     'Inn' in player
    53     returns True
    57     returns True
    54     'Econ' in Player
    58     'Econ' in Player
    55     returns False as there is no such 'key'
    59     returns False as there is no such 'key'
    56     If you try to look or search 'value' it wont work.
    60     If you try to look or search for a 'value' it will not work.
    57     Dictionaries supports some functions to retrieve keys and values 
    61     Dictionaries support functions to retrieve keys and values 
    58     like
    62     such as
    59     player.keys()
    63     player.keys()
    60     returns the list of all 'keys'
    64     returns the list of all 'keys'
    61     player.values()
    65     player.values()
    62     return list of all 'values'    
    66     return list of all 'values'    
    63 
    67 
    64     Next we shall look at 'sets'. Sets in Python are unordered 
    68     Now we shall move on to 'sets'. Sets in Python are an unordered 
    65     collection of unique elements. This data structure comes handy in
    69     collection of unique elements. This data structure comes in handy in
    66     situations while removing duplicates from a sequence, and computing 
    70     situations while removing duplicates from a sequence, and computing 
    67     standard math operations on sets such as intersection, union, 
    71     standard math operations on sets such as intersection, union, 
    68     difference, and symmetric difference. 
    72     difference, and symmetric difference. 
    69     
    73     
    70     Lets start by creating a set
    74     Lets start by creating a set
    71     f10 = set([1,2,3,5,8])
    75     f10 = set([1,2,3,5,8])
    72     f10 is set of Fibonacci numbers less then 10
    76     And thats how a set is created.
    73     lets print value of f10
    77     f10 is the set of Fibonacci numbers less than 10
       
    78     lets print the value of f10
    74     print f10
    79     print f10
    75 
    80 
    76     As we mentioned earlier, these are unordered structure so order of
    81     As we mentioned earlier, these are unordered structure so order of
    77     elements are not maintained, and output order is different than 
    82     elements is not maintained, and output order is different than 
    78     input order. Lets create one more set of all prime numbers less than
    83     input order, just as in dictionaries. Lets create one more set, a set of
    79     10
    84     all prime numbers less than 10
    80     p10 = set([2,3,5,7])
    85     p10 = set([2,3,5,7])
    81     print p10.
    86     print p10.
    82     
    87     
    83     To get union of these two sets we use '|' operator
    88     To get union of these two sets we use the or '|' operator
    84     f10 | p10
    89     f10 | p10
    85     
    90     
    86     For intersection & operator is used:
    91     For intersection we use the and '&' operator:
    87     f10 & p10
    92     f10 & p10
    88     
    93     
    89     f10 - p10 gives difference between f10 and p10, which is, elements
    94     f10 - p10 gives difference between f10 and p10, that is, the set of all elements
    90     present in f10 but not present in p10.
    95     present in f10 but not in p10.
    91     ^ operator gives us symmetric difference that is p10 union f10 minus
    96     The carat '^' operator gives us the symmetric difference of 2 sets. That is
    92     f10 intersection p10
    97     f10 union p10 minus f10 intersection p10
    93     f10 ^ p10
    98     f10 ^ p10
    94 
    99 
    95     To check if a set is super set or subset greater than and lesser than
   100     To check if a set is the super set or a subset of another set, the greater than 
    96     operators can be used
   101     and the lesser than operators are used
    97     set([2,3]) < p10
   102     set([2,3]) < p10
    98     returns True as p10 is superset of given set
   103     returns True as p10 is superset of given set
    99     
   104     
   100     Similar to lists and dictionaries sets also supports containership so
   105     Similar to lists and dictionaries, sets also supports containership so
   101     2 in p10
   106     2 in p10
   102     returns True as 2 is part of set p10 and 
   107     returns True as 2 is part of set p10 and 
   103     4 in p10
   108     4 in p10
   104     returns False.
   109     returns False.
   105     
   110     
   106     len function works with sets also:
   111     The 'len' function works with sets also:
   107     len(f10) returns the length, that is 5
   112     len(f10) returns the length, which is 5 in this case.
   108     We can also use 'for' loops to iterate through a set.
   113     We can also use 'for' loops to iterate through a set just as with dictionaries and lists.
   109     
   114     
   110     With this we come to the end of this tutorial on Dictionaries and 
   115     With this we come to the end of this tutorial on Dictionaries and 
   111     sets. We have covered some of properties of both data types and 
   116     sets. We have seen how to initialize dictionaries, how to index them using keys
   112     functions supported by them. Thank you.
   117     and a few functions supported by dictionaries. We then saw how to initialize
       
   118     sets, perform various set operations and a few functions supported
       
   119     by sets. Hope you have enjoyed it, Thank you.
   113 
   120 
   114 *** Notes
   121 *** Notes