3.7 # for python version < 3.7, it used to remove random item from the dctionary # To check if an item is present in the dictionary mydict = {"Name": "Vinit", "Age": 22, "City": "Dharwad"} # Method-1 if "Name" in mydict: print(mydict["Name"]) # Method-2 try: print(mydict["Name"]) except: print("Error") for key in mydict: print(key) "> 3.7 # for python version < 3.7, it used to remove random item from the dctionary # To check if an item is present in the dictionary mydict = {"Name": "Vinit", "Age": 22, "City": "Dharwad"} # Method-1 if "Name" in mydict: print(mydict["Name"]) # Method-2 try: print(mydict["Name"]) except: print("Error") for key in mydict: print(key) "> 3.7 # for python version < 3.7, it used to remove random item from the dctionary # To check if an item is present in the dictionary mydict = {"Name": "Vinit", "Age": 22, "City": "Dharwad"} # Method-1 if "Name" in mydict: print(mydict["Name"]) # Method-2 try: print(mydict["Name"]) except: print("Error") for key in mydict: print(key) ">
# Dictionary: Key-Value pairs, Unordered, Mutable
mydict = {"Name": "Vinit", "Age": 22, "City": "Dharwad"}
print(mydict)

'''mydict2 = dict(name="Mary", age=27, city="Boston")
print(mydict2)'''

# Accessing values
value = mydict["Name"]
print(value)
value1 = mydict["Age"]
print(value1)
value2 = mydict["City"]
print(value2)

# Adding new elements
mydict["email"] = "[email protected]"
print(mydict)

# Deleting elements: Method-1 (del)
del mydict["Name"]
print(mydict)

# Deleting elements: Method-2 (pop)
mydict.pop("Age")
print(mydict)

# Deleting elements: Method-3 (popitem)
mydict.popitem()
print(mydict)
# removes last item for python version > 3.7
# for python version < 3.7, it used to remove random item from the dctionary

# To check if an item is present in the dictionary
mydict = {"Name": "Vinit", "Age": 22, "City": "Dharwad"}
# Method-1
if "Name" in mydict:
    print(mydict["Name"])
# Method-2
try:
    print(mydict["Name"])
except:
    print("Error")

for key in mydict:
    print(key)

for key in mydict.keys():
    print(key)

for value in mydict.values():
    print(value)

for key, value in mydict.items():
    print(key, value)

# Merging two dictionaries
my_dict = {"name": "Max", "age": 28, "email": "[email protected]"}
my_dict2 = dict(name="Mary", age=27, city="Boston")
my_dict.update(my_dict2)
print(my_dict)

mydict = {3: 9, 6: 36, 9: 81}
print(mydict)
print(mydict[3])

mytuple = (7, 8)
mydict = {mytuple: 15}
print(mydict)