18
loading...
This website collects cookies to deliver better user experience
This article defines what is dictionary data type and how to perform some operations with them.
We will also learn how to use various built-in methods in dictionaries.
python_dict={“name”: “sachin”, “headline”: “Software Engineer”, “github_username”: “Sachin-chaurasiya”}
python_dict=dict({“name”: “sachin”, “headline”: “Software Engineer”, “github_username”: “Sachin-chaurasiya”}).
sequence=[(“name”, “sachin”),(“headline” , “Software Engineer”), (“github_username”, “Sachin-chaurasiya”)]
python_dict=dict(sequence).
# it will return the value of the key “name” that is “sachin”.
name=python_dict[“name”]
# output sachin
name=python_dict.get(“name”)
# accessing with get() method
# output: None
print(python_dict.get(“experience”))
# accessig with []
# output: KeyError
print(python_dict[“experience”])
python_dict[“headline”]= “FrontEnd Developer”
# output : {“name”: “sachin”, “headline”: “FrontEnd Developer”, “github_username”: “Sachin-chaurasiya”}
print(python_dict)
# output : sachin
print(python_dict.pop(“name”))
# output : (“github_username”, “Sachin-chaurasiya”)
print(python_dict.popitem())
# output : {}
print(python_dict.clear())
# this will throw an error
print(python_dict)
# output : [‘name’, ‘headline’ ,’github_username’]
print(python_dict.keys())
# output : [‘sachin’ , ‘Software Engineer’, ‘Sachin-chaurasiya’]
print(python_dict.values())
# output : [(“name”, “sachin”),(“headline” , “Software Engineer”), (“github_username”, “Sachin-chaurasiya”)]
print(python_dict.items())