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'
<view> = <dict>.keys() # Coll. of keys that reflects changes.
<view> = <dict>.values() # Coll. of values that reflects changes.
<view> = <dict>.items() # Coll. of key-value tuples that reflects chgs.
value = <dict>.get(key, default=None) # Returns default if key is missing.
value = <dict>.setdefault(key, default=None) # Returns and writes default if key is missing.
<dict> = collections.defaultdict(<type>) # Creates a dict with default value of type.
<dict> = collections.defaultdict(lambda: 1) # Creates a dict with default value 1.
<dict> = dict(<collection>) # Creates a dict from coll. of key-value pairs.
<dict> = dict(zip(keys, values)) # Creates a dict from two collections.
<dict> = dict.fromkeys(keys [, value]) # Creates a dict from collection of keys.
<dict>.update(<dict>) # Adds items. Replaces ones with matching keys.
value = <dict>.pop(key) # Removes item or raises KeyError.
{k for k, v in <dict>.items() if v == value} # Returns set of keys that point to the value.
{k: v for k, v in <dict>.items() if k in keys} # Returns a dictionary, filtered by keys.
# 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"
}
#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))