In [1]:
myCat = {'size': 'fat', 'color':'gray', 'disposition':'loud'}
In [2]:
myCat['size']
Out[2]:
In [3]:
'My cat has ' + myCat['color'] + ' fur.'
Out[3]:
In [4]:
spam = {12345: 'Luggage Combination', 42: 'The Answer'}
In [5]:
spam = ['cats', 'dogs', 'moose']
In [6]:
bacon = ['dogs', 'moose', 'cats']
In [7]:
spam == bacon
Out[7]:
In [8]:
eggs = {'name': 'Zophies', 'species': 'cat', 'age': 8}
In [9]:
ham = {'species': 'cat', 'age': 8, 'name': 'Zophies'}
In [10]:
eggs == ham
Out[10]:
In [11]:
spam['color']
In [12]:
spam = {'color': 'red', 'age': 42}
In [13]:
for v in spam.values():
print(v)
In [14]:
for k in spam.keys():
print(k)
In [15]:
for i in spam.items():
print(i)
In [16]:
spam.keys()
Out[16]:
In [17]:
list(spam.keys())
Out[17]:
In [18]:
for k, v in spam.items():
print('Key: ' + k + ', Value: ' + str(v))
In [19]:
spam = {'name': 'Zophie', 'age': 7}
In [20]:
'name' in spam.keys()
Out[20]:
In [21]:
'Zophie' in spam.values()
Out[21]:
In [22]:
'color' in spam
Out[22]:
In [23]:
'color' not in spam.keys()
Out[23]:
In [25]:
'color' in spam.keys()
Out[25]:
In [26]:
picnicItems = {'apples': 5, 'cups': 2}
In [27]:
'I am bringing ' + str(picnicItems.get('cups', 0)) + ' cups.'
Out[27]:
In [28]:
'I am bringing ' + str(picnicItems.get('eggs', 0)) + ' eggs.'
Out[28]:
In [29]:
'I am bringing ' + str(picnicItems['eggs']) + ' eggs.'
In [30]:
spam = {'name': 'Pooka', 'age': 5}
In [31]:
if 'color' not in spam:
spam['color'] = 'black'
In [32]:
spam
Out[32]:
In [36]:
spam = {'name': 'Pooka', 'age': 5}
In [34]:
spam.setdefault('color', 'black')
Out[34]:
In [35]:
spam
Out[35]:
In [ ]: