Skip to content

Commit 15898bf

Browse files
committed
Lesson 3: (James' sections) Adding subtitles for dictionary topics
* Added examples for the `del` keyword. * Made a distinction between assignment for the first time, and modifying a key
1 parent 6f99809 commit 15898bf

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

learn/resources/lesson_3/index.md

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The `dict` type is also referred to as a "dictionary" or "hashtable". They are a
2626
comma separated list of keys and values in the form `key1:value1, key2:value2,
2727
key3:value3, ...` surrounded by a pair of curly braces `{ ... }`.
2828

29-
#### Basic operations
29+
#### Looking up values
3030

3131
Lets take a look at a larger example:
3232

@@ -56,6 +56,8 @@ you use `=` to assign a value to a key the does not exist in the dictionary?
5656
As you saw in the task, we get a `KeyError` when we try to access a key that
5757
doesn't exist.
5858

59+
#### Checking values exist
60+
5961
To get around this there is the `in` keyword. You can use `in` to tell you
6062
whether a given key has a value in the dictionary and you can use `not in` to
6163
do the opposite:
@@ -67,6 +69,8 @@ do the opposite:
6769
'lastName' in mydict # => False
6870
'lastName' not in mydict # => True
6971

72+
#### Adding values
73+
7074
As you saw in the task, it is however possible to assign a value to a key that
7175
was not previously in the dictionary using `=`, for example:
7276

@@ -97,30 +101,63 @@ duplicate keys (E.g. Each key maps only to one value at a time):
97101
print mydict
98102
# => {'a': 2}
99103

104+
#### Modifying values
105+
100106
As you can see, python decided to automatically choose the last value assigned
101-
to the `'a'` key, which was `2`.
107+
to the `'a'` key, which was `2`. This behaviour is what allows you to
108+
*overwrite* existing values in a dictionary, if you assign a value to a key that
109+
already exists in the dictionary:
110+
111+
mydict = {'name': 'Joe'}
112+
113+
print mydict
114+
# => {'name': 'Joe'}
115+
116+
mydict['name'] = 'James'
117+
118+
print mydict
119+
# => {'name': 'James'}
102120

103-
Additionally you may only use certain types as keys.
121+
#### Key value types
104122

105-
<div class="panel panel-primary"> <div class="panel-
106-
heading"><strong>Task</strong></div> <div class="panel-body"> Find one type
107-
other than string that can be used for keys, and one type that cannot. </div>
123+
You can use more than just strings as keys in dictionaries, experiment with
124+
other values as key types and see which ones work and which ones don't.
125+
126+
<div class="panel panel-primary">
127+
<div class="panel- heading"><strong>Task</strong></div>
128+
<div class="panel-body">
129+
<p>
130+
Find one type other than string that can be used for keys, and one type
131+
that cannot.
132+
</p>
133+
<p>
134+
<strong>Hint</strong> If you choose a key type that is not allowed you
135+
will get an error that begins <code>TypeError: unhashable type</code>
136+
and tells you the name of the type you tried to use.
137+
</div>
108138
</div>
109139

110-
Finally, we have value assignment and updating, but what about deletion? You can
111-
remove a key:value pair by saying `del mydict['name']` watch out though - you'll
112-
get a `KeyError` if the key isn't in the dictionary.
140+
#### Deleting values
113141

142+
You can remove an entry from the dictionary using the `del` keyword:
114143

115-
#### Uses of dictionaries
144+
mydict = {'name': 'James', 'level': 9001}
145+
146+
del mydict['name']
116147

117-
So why might we want to use a dictionary? In the game
118-
we'll be creating later, we're going to need some way of keeping track of
119-
information regarding the player. A dictionary is an ideal type for containing
120-
all information relevant to the player in one place. We could have several
121-
separate variables, but that rapidly gets tedious, especially when we throw
122-
functions into the mix.
148+
print mydict
149+
# => {'level': 9001}
150+
151+
Watch out though! You'll get a `KeyError` if the key wan't in the dictionary
152+
to begin with.
153+
154+
#### Uses of dictionaries
123155

156+
So why might we want to use a dictionary? In the game we'll be creating later,
157+
we're going to need some way of keeping track of information regarding the
158+
player. A dictionary is an ideal type for containing all information relevant to
159+
the player in one place. We could have several separate variables, but that
160+
rapidly gets tedious, especially when we throw functions into the mix.
124161

125162
### Functions
126163

0 commit comments

Comments
 (0)