25
loading...
This website collects cookies to deliver better user experience
AatmajProfileDictionary={"name":"Aatmaj","Hobby":"teaching","Commits":700}
a=[] #list
a=() #tuple
a={}#dictionary
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
>>> hardware={ "Brand": "Dell", "Model": 2430, "Year": "2020"}
>>> print(hardware) #prints the value of the dictionary
{'Brand': 'Dell', 'Model': 2430, 'Year': '2020'}
>>> print(hardware["Model"])
2430
>>> print(hardware.get("Model"))
2430
>>> hardware["Year"]=2021 #Changing the value of the dictionary
>>> print(hardware)
{'Brand': 'Dell', 'Model': 2430, 'Year': 2021}
>>> print(hardware.pop("Model"))
2430
>>> print(hardware)
{'Brand': 'Dell', 'Year': 2021}
>>> hardware["Model"]="Lenovo"
>>> hardware["Year"]=2019
>>> print(hardware.popitem()) #popitem returns the last value entered
('Model', 'Lenovo')
>>> print(hardware)
{'Brand': 'Dell', 'Year': 2019}
>>> for y in hardware:
... print(y)#Corresponds to each key
...
Brand
Year
>>> for x in hardware:
... print(hardware[x])#refers to the value
...
Dell
2019
>>> for z in hardware.values():
... print(z)
...
Dell
2019
>>> hardware.clear() #Cleares the dictionary (not delete)
>>> print(hardware)
{}
>>> print(hardware["Price"])#trying to remove element which is not present
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Price'
hardware={
"LAPTOP":{"Brand": "Dell","Model": 2430,"Year": "2020"},
"DESKTOP":{"Brand":"Lenovo","Model":8877,"Warranty": 2},
"TABLET":{"Brand":"Apple", "price":"3000$"}
}
print(hardware)
print(hardware["TABLET"])
print(hardware["LAPTOP"]["Model"]) #Note the syntax
{'TABLET': {'price': '3000$', 'Brand': 'Apple'}, 'LAPTOP': {'Model': 2430, 'Brand': 'Dell', 'Year': '2020'}, 'DESKTOP': {'Model': 8877, 'Brand': 'Lenovo', 'Warranty': 2}}
{'price': '3000$', 'Brand': 'Apple'}
2430
Please enter student name peter
Please enter marks 13
Please enter student name john
Please enter marks 32
Please enter student name pappu
Please enter marks 5
Please enter student name bob
Please enter marks 7
Please enter student name mina
Please enter marks 32
{'peter': 13, 'john': 32, 'pappu': 5, 'mina': 32, 'bob': 7}