DICTIONARY: It is Unordered data type, is in curly bracket, works on key and value, special use for database and mutable.
d = {"Name": "Python",
"Fees": "8000",
"Duration": "2 months"
}
print(d["Fees"]) # to get the value of the dictionary we have to use key
print(" ")
f = d["Name"] # in this way also we can store the value in another var using a key
print(f)
print(" ")
for n in d: # it returns key , keys present in d will be stored in n one by in every loop
print(n) # in n we have the keys
print(d[n]) # we can get value by passing key in d as we can see here
Dictionary Functions: get():,keys(),value(),items()
get(): it works on key we have to pass key
d = {"Name": "Python",
"Fees": "8000",
"Duration": "2 months"
}
n=d.get("Name") # or: print(d["Name"]) both will give the same output
print(n)
print(" ")
keys(): used in for loop
for a in d.keys(): # it will pass keys in a
print(a)
print(" ")
values():
for a in d.values(): # it will pass values in var a
print(a)
print(" ")
items(): get both keys and values
for a in d.items(): # it will pass both key and values in a
print(a)
More Dictionary Function: Delete: del, pop both uses key
del d["Name"] # it will delete this key and value
print(d)
print(" ")
a = d.pop("Fees") # it returns what it has deleted
print(a,d)
dict():
d=dict(name="Python", fees=800) # it creates dictionary but we have to pass it key and value
print(d)
update():
d.update({"Name": "java"}) # it adds the new key and value as well as it update or replace the value by keys
print(d)
d.clear(): it clears all the value
Add, Create or Insert a new key and value inside the dictionary