Watch Me Code 1: Dictionary Basics


In [2]:
student = {} # empty dictionary

In [3]:
# set some values
student['Name'] = 'Michael'
student['GPA'] = 3.4
student['Ischool'] = True

print(student)


{'GPA': 3.4, 'Ischool': True, 'Name': 'Michael'}

In [4]:
# mutable
student['GPA'] = 4.0
print(student['Name'], student['GPA'])


Michael 4.0

In [1]:
a = {"a": "b", "x": "y", "2": 2}

print(a[2])


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-192210c11917> in <module>()
      3 a = {"a": "b", "x": "y", "2": 2}
      4 
----> 5 print(a[2])
      6 
      7 

KeyError: 2

In [ ]:


In [ ]: