Last active
May 20, 2020 21:43
-
-
Save adityadev11/2874426b68e99b057cd74cbd7d8fbfca to your computer and use it in GitHub Desktop.
Dictionaries_in_python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Dictionaries are created by {} or using dict() function. | |
Dict1={'Hello':"World",2:"two",3:4,"List":[1,2,5,6]} | |
Dict2=dict(Key='Value',Integer=10) | |
print(Dict1) | |
print(Dict2) | |
#Length of Dictionary is given by len() | |
print(len(Dict1)) | |
print(Dict1[2]) #printing value of keyword '2' (Note its not printing acc to index) | |
print(Dict1.get("Hello")) #get() method to get the value of a key | |
Dict1['Hello']="Everyone" #changing the value of key 'Hello' | |
Dict2['New element']="Added" #adding new key-value pair | |
print(Dict1) | |
print(Dict2) | |
print(Dict1.keys()) #keys() method returns a list of all keys. | |
print(Dict1.items()) #items() method returns all the key-value pairs in a dictionary | |
print(Dict2.pop("Key")) #pop() deletes and returns the value associated with the key entered | |
print(Dict1.clear()) #clear() removes all elements of the dictionary and hence returns empty dictionary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment