Each key is separated by a colon (:) from its value, items are separated by commas, and the whole thing is enclosed in a curved brace. An empty dictionary with no items is written with only two curly braces, such as: {}.
Keys are unique within a dictionary while values cannot be. The values of a dictionary can be of any type, but the key must be of an immutable data type such as strings, numbers, or tuples.
Access value in the dictionary to access dictionary elements, you can use square brackets familiar with the key to getting its value.
How to access data from the python dictionary
Note – There is no indexing because of unordered collections of data
print(user1['name'])
# you cannot do like this
which types of data python dictionaries can store?
user_info = { 'name' : 'pramod', 'age' : 22 , 'rollno' :'17691a05b9', 'class' : 'B-Tech'} type(usser_info) print(user_info['name']) print(user_info['age']) print(user_info['rollno']) print(user_info['class'])
How to add data to an empty dictionary
user_info2 = {} user_info2['name'] = 'prem kumar' user_info2['age'] = 22 print(user_info2)
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 numbers 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