Python Language / Dictionaries in Python

It is a collection which is unordered and unindexed items and written with curly brackets.

Sample Programs
It is a collection which is unordered, changeable and indexed and written with curly brackets, and they have keys and values.

Program Output
# Create and print a dictionary
Ourdict = { "Language": "Python", "version": "3.5", "type": "object orinted" }
print(Ourdict)
{'Language': 'Python', 'version': '3.5', 'type': 'object orinted'}
# Accessing Items by referring to its key name, inside square brackets.
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
lang = Ourdict["Language"] print(lang)
Python
# print value of a key "model" key
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
lang = Ourdict.get("version")
print(lang)
3.5
#Program to Change dictionary Values
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
Ourdict["version"] = "3.9"
print(Ourdict)
{'Language': 'Python', 'version': '3.9', 'type': 'object oriented'}
#Loop a Dictionary and print its keys
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
for x in Ourdict:
print(x)
Language version type
#Loop Dictionary and print its Values
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
for x in Ourdict:
print(Ourdict[x])
Python 3.5 object oriented
#Loop Dictionary and print its Values
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
for x in Ourdict.values():
print(x)
Python 3.5 object oriented
#Loop Dictionary and print its keys and Values
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
for x, y in Ourdict.items():
print(x, y)
Language Python version 3.5 type object oriented
#Check if Key Exists
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
if "version" in Ourdict:
print("Yes version is a keys in Ourdict dictionary")
Yes, 'model' is one of the keys in the thisdict dictionary
#Determine Dictionary Length
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
print(len(Ourdict))
3
#Adding Items to dictionary
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
Ourdict["Advanced"] = "yes"
print(Ourdict)
{'Language': 'Python', 'version': '3.5', 'type': 'object oriented', 'Advanced': 'yes'}
#Removing Items of a dictionary
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
Ourdict.pop("type")
print(Ourdict)
{'Language': 'Python', 'version': '3.5'}
#Removing Items from dictionary Note: pop() removes last item only.
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
Ourdict.popitem()
print(Ourdict)
{'Language': 'Python', 'version': '3.5'}
#Removing Items from a dictionary using del keyword
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
del Ourdict["version"]
print(Ourdict)
{'Language': 'Python', 'type': 'object oriented'}
#Removing dictionary using del keyword
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
del Ourdict
//print(thisdict) causes Error because " Ourdict " got deleted.
#Clear() to empties the dictionary:
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" } Ourdict.clear() print(Ourdict)
{}
#Copy a Dictionary using copy() method
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
cpydict = Ourdict.copy()
print(cpydict)
{'Language': 'Python', 'version': '3.5', 'type': 'object oriented'}
#other way to Copy a Dictionary
Ourdict = { "Language": "Python", "version": "3.5", "type": "object oriented" }
cpydict = dict(Ourdict)
print(cpydict)
{'Language': 'Python', 'version': '3.5', 'type': 'object oriented'}
#dict() Constructor
Ourdict = dict(Language="Python", version="305", type="object oriented")
print(Ourdict)
{'Language': 'Python', 'version': '3.5', 'type': 'object oriented'}


Home     Back