Adding items to Dictionaries in Python

Add an item to a dictionary in Python. Using keys we can assign values to a dictionary.

Source code :

# Creating a dictionary.
d = {}

# Add a new item using a key
d['name'] = 'Emberl'

d['marks'] = (10,20,30,40)

d['rank'] = 5


# Print the contents
print("Your details are " + str(d) )

Output :

Your details are {'name': 'Emberl', 'marks': (10, 20, 30, 40), 'rank': 5}

Output :


                

Comments :