xxxxxxxxxx
# A dict (dictionary) is a data type that store keys/values
myDict = {"name" : "bob", "language" : "python"}
print(myDict["name"])
# Dictionaries can also be multi-line
otherDict {
"name" : "bob",
"phone" : "999-999-999-9999"
}
xxxxxxxxxx
student_data = {
"name":"inderpaal",
"age":21,
"course":['Bsc', 'Computer Science']
}
#the keys are the left hand side and the values are the right hand side
#to print data you do print(name_of_dictionary['key_name'])
print(student_data['name']) # will print 'inderpaal'
print(student_data['age']) # will print 21
print(student_data['course'])[0]
#this will print 'Bsc' since that field is an array and array[0] is 'Bsc'
xxxxxxxxxx
x = dict(name = "vvy", age = 17, country = "India")
print(x)
# {'name': 'vvy', 'age': 17, 'country': 'India'}
xxxxxxxxxx
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
print(x)
---------------------------------------------------------------------------
Mustang
xxxxxxxxxx
# dictionary refresh
new_dict = {
"first":"1,2,3",
"second":"321",
"third":"000",
}
# adding to dictionary
new_dict.update({"fourth":"D"})
print(new_dict)
#removing from dictionary
new_dict.pop("first")
print(new_dict)
new = {"five":"888"}
#updating a dictionary
new_dict.update(new)
print(new_dict)
xxxxxxxxxx
dictionary = {
"name": "Elie",
"family name": "Carcassonne",
"date of born": "01/01/2001",
"list": ["hey", "hey"]
}
xxxxxxxxxx
mydictionary = {'name':'python', 'category':'programming', 'topic':'examples'}
for x in mydictionary:
print(x, ':', mydictionary[x])
xxxxxxxxxx
#Python dictionaries consists of key value pairs tha
#The following is an example of dictionary
state_capitals = {
'Arkansas': 'Little Rock',
'Colorado': 'Denver',
'California': 'Sacramento',
'Georgia': 'Atlanta'
}
#Adding items to dictionary
#Modification of the dictionary can be done in similar maner
state_capitals['Kampala'] = 'Uganda' #Kampala is the key and Uganda is the value
#Interating over a python dictionary
for k in state_capitals.keys():
print('{} is the capital of {}'.format(state_capitals[k], k))