Dictionary
- Dictionary is one of the most important and useful datatypes of python programming language
- Dictionary pair is a pair of keys and values associated with keys.
For Example:
dictionary={"name":"ajay","age":23}
This dictionary haves 2 key and 2 values
Creating a Dictionary
Creating dictionary in python is very simple. Just write variable name and assigning key and value within {} brackets.
- dictionary={"name":"ajay","age":23}
Dictionary is created successfully
Accessing Dictionary Value
In Python's dictionary key is reference for values, means for accessing value we used key as the reference.
- dictionary={"name":"ajay","age":23}
- print("name: ",dictionary["name"])
- print("age: ",dictionary["age"])
Output
Method for getting keys and values
In python programming language keys() and values() method is used to get keys and values.
- keys()- get all keys from dictionary
- values()-get all values from dictionary
- dictionary={"name":"ajay","age":23}
- print("keys: ",dictionary.keys())
- print("values: ",dictionary.values())
Output