Explain Dictionary in Python

Explain Dictionary in Python
Python dictionary is an unordered collection of items which is used to store data values

Dictionaries are optimized to retrieve values when the key is known.

In Python dictionaries are written with curly brackets, and they have keys and values.

An empty dictionary without any items is written with just two curly braces, like this: {}.

How to create a dictionary?

Creating a dictionary is as simple as placing items inside curly braces {} separated by comma.

# Creating an empty Dictionary 
Dict = {} 
print("Empty Dictionary: ") 
print(Dict)

# Creating a Dictionary with Integer Keys 
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'} 
print("\nDictionary with the use of Integer Keys: ") 
print(Dict)

# Creating a Dictionary with Mixed keys 
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]} 
print("\nDictionary with the use of Mixed Keys: ") 
print(Dict)

PROGRAM OF DICTIONARY:

my_dict = {'name':'Jack', 'age': 26} 
print(my_dict['name']) 
print(my_dict.get('age'))

Output:
Jack
26

To Learn more about Python, Click Here

Click here To Follow on pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *