Python- The Dictionary

This is the implementation of the ‘key-value’ pair in Python. Each key and value is separated by a colon :

dict = {‘Color’: ‘Red’,’Value’:’255,0,0′}

The value of the dict can be updated as follows:

dict[‘Color’] = ‘Green’
dict[‘Value’] = ‘0,255,0’

You can delete individual pair using the del statement.

Len gives the length of the dictionary.

dict = {‘Color’: ‘Red’,’Value’:’255,0,0′}
print(dict)
print (“dict[‘Color’]”,dict[‘Color’])
print (“dict[‘Value’]”,dict[‘Value’])

dict[‘Color’] = ‘Green’
dict[‘Value’] = ‘0,255,0’

print (“dict[‘Color’]”,dict[‘Color’])
print (“dict[‘Value’]”,dict[‘Value’])

del dict[‘Color’]
print(dict)

del dict[‘Value’]
print(dict)
dict = {‘Color’: ‘Blue’,’Value’:’0,0,255′}

print(len(dict))

print(‘Str produces a printable string reprsentation of %s’ % str(dict))

The output of the above program:

Output :

{‘Color’: ‘Red’, ‘Value’: ‘255,0,0’}
dict[‘Color’] Red
dict[‘Value’] 255,0,0
dict[‘Color’] Green
dict[‘Value’] 0,255,0
{‘Value’: ‘0,255,0’}
{}
2
Str produces a printable string reprsentation of {‘Color’: ‘Blue’, ‘Value’: ‘0,0,255’}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s