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(type(user)) #you can check type

Example: 2

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

 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(type(user)) #you can check type
second method ———–>
user1 = dict(name = ‘pramod’ , age = 22) #you are converting into tuple

Recommended Post:

Get Salesforce Answers

Hi, I’m Pramod Kumar Yadav & a computer science Engineer with 3years of experience in Digital marketing. I’m self-motivated and love to do SEO.

Best PYthon Course

Get More trending Courses

Leave a Reply

%d bloggers like this: