Python Dictionary Introduction | Why to use dictionary with example

What is a Dictionary in Python? Explain

Python Dictionary is an unordered assortment of data values, utilized to store data values like a map, which dissimilar to other Data Types that hold just a solitary value as a component, Dictionary holds key:value pair. Key-value is given in the dictionary to make it more advanced. Each key-value pair in a Dictionary is isolated by a colon: whereas each key is isolated by a ‘comma’.

A Dictionary in Python works like the Dictionary in reality. The keys of a Dictionary should be special and of immutable data types like Strings, Integers, and tuples, however, the key-values can be rehashed and be of any type.

Note – Keys in a dictionary don’t permit Polymorphism.

How to Create Dictionary in Python?

In Python, a Dictionary can be created by placing a sequence of elements within curly {}braces, separated by ‘comma’. Dictionary holds a pair of values, one being the Key and the other corresponding pair element being its Key:value. Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable.
Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces{}.

Example: 1

user = {‘name’ : ‘pramod’,‘age’: 22}
print(user)
print(type(user)) #you can check type


Example: 2

user1 = dict(name = ‘pramod’ , age = 22) #you are converting into tuple
print(user1

 Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated distinctly.

Questions and Answers:

Why use a dictionary?

Because of the limitations of the list, lists are not enough to represent real data. Example: user=[‘pramod’,22,[‘Ravi’,21],[‘Vijavy’,20]]
This list contains the user name, age, favorite movies, etc you can do this but this is not a good way to do it in this

How to create dictionary?

First method to create ——>
user = {‘name’ : ‘pramod’,‘age’: 22}
print(user)
print(type(user)) #you can check type
second method ———–>
user1 = dict(name = ‘pramod’ , age = 22) #you are converting into tuple
print(user1)

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment