dictionary.org
changeset 121 331c5fdc06d4
parent 119 7dc53e6c8065
equal deleted inserted replaced
120:50716c7c4c0c 121:331c5fdc06d4
    20     Lets start by opening IPython interpreter. 
    20     Lets start by opening IPython interpreter. 
    21     '{}' are used to create Python dictionaries. 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     Let's see what player contains by typing:
       
    26 
       
    27     print player
       
    28 
    25     Its a dictionary storing statistics of a cricket player.
    29     Its a dictionary storing statistics of a cricket player.
    26     Here 'Mat', 'Inn' etc are the keys. Now in order to get the 'average' of
    30     Here 'Mat', 'Inn' etc are the keys. Now in order to get the 'average' of
    27     this player we simply type
    31     this player we simply type
    28     print player['Avg']
    32     print player['Avg']
    29     52.53
    33     52.53
    30 
    34 
    31     To add a new key-value pair to this dictionary we type
    35     To add a new key-value pair to this dictionary we type
    32     player['Name'] = 'Rahul Dravid'
    36     player['Name'] = 'Rahul Dravid'
    33     print player
    37     print player
       
    38     As you can see the given key-value pair has been added.
    34     Please note that Python dictionaries don't maintain the order
    39     Please note that Python dictionaries don't maintain the order
    35     in which the key-value pairs are stored. The order might change
    40     in which the key-value pairs are stored. The order might change
    36     as we add new entries.
    41     as we add new entries.
    37 
    42 
    38     In dictionaries Duplicate keys are overwritten, that is when we do 
    43     In dictionaries Duplicate keys are overwritten, that is when we do