Previous post: Next Post:
Python #9-Set Operation in Python Python #11- Functions in Python
What are Dictionaries in Python?
Dictionaries is an unordered collection of key value pairs.
How the dictionaries will look?
A dictionaries hold the key and values inside the curly braces where as key and values are separated by colon : operator..
Syntax:
VariableName={' key1 ' : ' value1 ', ' key2 ' : ' value2 ', .....' keyN ' : ' valueN ' }
Note Worthy: In Python dictionaries will be created by means of the Curly braces { } consists of key and values inside in it
eg: {'key' : 'values'}
Example:
Note: Use the 🖉 to see code editor. Use the ▶ view the output .
Explanation:
#line 1 : phones={'a':'apple','b':'blackberry', 'c':'celkon', 'n':'nokia', 's':'samsung'}
This is way by which dictionaries in python are created.
#line 3 : print phones
This line will print the elements(keys : values) present in the dictionaries.
#line 6 : print phones['a']
This line is used to print the elements in the dictionaries by means of their index values.
#line 9 : print phones.values()
This line is used to print the values present in the dictionaries by means of using the .value() function.
#line 12: print phones.keys()
This line is used to print the keys present in the dictionaries by means of using the .keys() function.
Have any doubts don't forget to comment..!
Thanks and Regards,
Tech Bird
Comments