Dictionary in Python
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
li=dict(name="Gunjan",age=22,cnt="India")
print(li)
print(thisdict["brand"])
print(thisdict['model'])
print(li.get("age"))
print(thisdict.keys())
print(thisdict.items())
print(thisdict.values())
thisdict['year']=2004
thisdict.update({"year":2005})
thisdict['gear']="Automatic"
# thisdict.pop("gear")
# thisdict.popitem() # popitem method removes the last element in the dictionary
# del thisdict # it throws an error instead of this we use clear method
# del thisdict['gear']
thisdict.clear() # deletes all the elements in the dictionary
li2=thisdict.copy() # copy is used to copy the entire dictionary
for x,y in thisdict.items():
print(f"{x} = {y}")
for x,y in li2.items():
print(f"{x} = {y}")
if "model" in thisdict:
print("yes")
if "Ford" in thisdict:
print("yeah")
Comments
Post a Comment