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?
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.Example: 1
print(user)
print(type(user)) #you can check type
Example: 2
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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers