@@ -39,18 +39,18 @@ which is where most people would expect things to start. This means that the
39
39
* first* element of a list is referred to with a ` 0 ` , the second element is
40
40
referred to with a ` 1 ` , and so on. Perhaps an example would be helpful:
41
41
42
- >>> places_to_visit[0]
42
+ >>> print( places_to_visit[0])
43
43
'Mexico'
44
- >>> places_to_visit[2]
44
+ >>> print( places_to_visit[2])
45
45
'Kenya'
46
46
47
47
Positive numbers aren't the only thing you can use for indexes though. Negative
48
48
numbers invert the index, so you can get the * last* element of a list by using
49
49
` -1 ` , or the third-to-last by using ` -3 ` :
50
50
51
- >>> places_to_visit[-1]
51
+ >>> print( places_to_visit[-1])
52
52
'New Zealand'
53
- >>> places_to_visit[-3]
53
+ >>> print( places_to_visit[-3])
54
54
'Kenya'
55
55
56
56
Try it yourself now: Try to get ` Nepal ` out of ` places_to_visit ` using both a
@@ -71,23 +71,23 @@ There's a lot that you can do with lists, so much that we simply can't cover it
71
71
all here, so instead here are some examples, and a [ link to the documentation] ( https://docs.python.org/3.5/tutorial/datastructures.html#more-on-lists " Methods of lists ")
72
72
for a more complete reference:
73
73
74
- >>> places_to_visit
74
+ >>> print( places_to_visit)
75
75
['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
76
76
77
77
>>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
78
78
>>> places_to_visit[1] = "Peru"
79
- >>> places_to_visit
79
+ >>> print( places_to_visit)
80
80
['Mexico', 'Peru', 'Kenya', 'Nepal', 'New Zealand']
81
81
82
82
>>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
83
- >>> places_to_visit.pop()
83
+ >>> print( places_to_visit.pop() )
84
84
'New Zealand'
85
- >>> places_to_visit
85
+ >>> print( places_to_visit)
86
86
['Mexico', 'Portugal', 'Kenya', 'Nepal']
87
87
88
88
>>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
89
89
>>> places_to_visit.append("Colombia")
90
- >>> places_to_visit
90
+ >>> print( places_to_visit)
91
91
['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand', 'Colombia']
92
92
93
93
Try some of these out yourself:
@@ -103,22 +103,24 @@ But we're not finished! You can do even more interesting things with `[` and
103
103
to get the first four elements of a list * as another list* . That's easy with
104
104
the subset syntax:
105
105
106
- >>> places_to_visit[0:4]
106
+ >>> print( places_to_visit[0:4])
107
107
["Mexico", "Portugal", "Kenya", "Nepal"]
108
- >>> places_to_visit[:4]
108
+
109
+ >>> print(places_to_visit[:4])
109
110
["Mexico", "Portugal", "Kenya", "Nepal"]
110
111
111
112
You can do some interesting things with negative numbers too:
112
113
113
- >>> places_to_visit[-3:4]
114
+ >>> print( places_to_visit[-3:4])
114
115
['Kenya', 'Nepal']
115
- >>> places_to_visit[-3:]
116
+
117
+ >>> print(places_to_visit[-3:])
116
118
['Kenya', 'Nepal', 'New Zealand']
117
119
118
120
...and as getting a subset returns a list itself, you can get a subset of a
119
121
subset:
120
122
121
- >>> places_to_visit[0:4][-1]
123
+ >>> print( places_to_visit[0:4][-1])
122
124
'Nepal'
123
125
124
126
### Nesting
@@ -128,15 +130,15 @@ you from putting a list inside a list. In fact, you can put a list inside a
128
130
list, inside a list, inside a... you get the idea. You're only limited by how
129
131
many levels deep you can go before your code is too confusing:
130
132
131
- cake_flavours = ["chocolate", ["chocolate", "vanilla"], "red velvet"]
133
+ >>> cake_flavours = ["chocolate", ["chocolate", "vanilla"], "red velvet"]
132
134
133
- cake_flavours[0]
135
+ >>> print( cake_flavours[0])
134
136
'chocolate'
135
137
136
- cake_flavours[1]
138
+ >>> print( cake_flavours[1])
137
139
['chocolate', 'vanilla']
138
140
139
- cake_flavours[1][1]
141
+ >>> print( cake_flavours[1][1])
140
142
'vanilla'
141
143
142
144
There's a lot more things you can do with lists including concatenating,
@@ -152,31 +154,31 @@ will result in a `TypeError`.
152
154
153
155
Tuples are represented using ` ( ` and ` ) ` :
154
156
155
- places_to_visit = ("Mexico", "Portugal", "Kenya", "Nepal", "New Zealand")
157
+ >>> places_to_visit = ("Mexico", "Portugal", "Kenya", "Nepal", "New Zealand")
156
158
157
- places_to_visit[1]
159
+ >>> print( places_to_visit[1])
158
160
'Portugal'
159
161
160
- places_to_visit[0:3]
162
+ >>> print( places_to_visit[0:3])
161
163
('Mexico', 'Portugal', 'Kenya')
162
164
163
165
This however will fail with a ` TypeError ` :
164
166
165
- places_to_visit[1] = "Canada"
167
+ >>> places_to_visit[1] = "Canada"
166
168
167
169
Tuples are commonly used in cases where you're defining something that
168
170
shouldn't ever change, but should you ever need to modify something that's in a
169
171
tuple, you can always * cast* it as a list:
170
172
171
- my_tuple = (1, 2, 3)
173
+ >>> my_tuple = (1, 2, 3)
172
174
173
- my_list = list(my_tuple)
174
- my_list[2] = 99
175
+ >>> my_list = list(my_tuple)
176
+ >>> my_list[2] = 99
175
177
176
- my_list
178
+ >>> print( my_list)
177
179
[1, 2, 99]
178
180
179
- my_tuple
181
+ >>> print( my_tuple)
180
182
(1, 2, 3)
181
183
182
184
This will make a ** copy** of the tuple that's a list, so you can edit it, but
@@ -194,7 +196,7 @@ corresponding *values*. You'll often seen them as a simple way to have a
194
196
lookup table or crude database in an application. For this tutorial we'll use
195
197
a dictionary for a phone book:
196
198
197
- my_phone_book = {
199
+ >>> my_phone_book = {
198
200
"Arya": "+4407485376242",
199
201
"Brienne": "+3206785246863",
200
202
"Cersei": "+14357535455",
@@ -210,7 +212,7 @@ though. This will give you Cersei's phone number:
210
212
There's nothing restricting you to strings as values in your dictionary though.
211
213
You can have * anything* in there:
212
214
213
- my_crazy_dictionary = {
215
+ >>> my_crazy_dictionary = {
214
216
"a number": 7,
215
217
"a float": 1.23456789,
216
218
"a string": "hello, world!",
@@ -228,7 +230,7 @@ can be used for keys, though this can sometimes lead to reduced readability,
228
230
so use this with caution. For example, while this is valid Python, it's not
229
231
exactly easy to understand:
230
232
231
- my_unreadable_dictionary = {
233
+ >>> my_unreadable_dictionary = {
232
234
("this", "is", "a", "tuple!"): {"a string": "hello, world!"}
233
235
}
234
236
@@ -247,15 +249,15 @@ doing this should hopefully feel intuitive by now:
247
249
248
250
Change a value for a key:
249
251
250
- my_phone_book["Brienne"] = "+830685432195"
252
+ >>> my_phone_book["Brienne"] = "+830685432195"
251
253
252
254
Add a new key/value pair:
253
255
254
- my_phone_book["Ellaria"] = "+560538942621"
256
+ >>> my_phone_book["Ellaria"] = "+560538942621"
255
257
256
258
This one is new: delete a key/value pair:
257
259
258
- del(my_phone_book["Ellaria"])
260
+ >>> del(my_phone_book["Ellaria"])
259
261
260
262
Now that you've got the tools to change your dictionaries, give a shot
261
263
yourself. Try changing Cersei's phone number. In fact, change it to a list of
@@ -274,13 +276,13 @@ manageable parts. Thankfully, Python has you covered with `.keys()`,
274
276
"Cersei": "+14357535455",
275
277
"Davos": "+244562726258"
276
278
}
277
- >>> my_phone_book.keys()
279
+ >>> print( my_phone_book.keys() )
278
280
dict_keys(['Davos', 'Cersei', 'Brienne', 'Arya'])
279
281
280
- >>> my_phone_book.values()
282
+ >>> print( my_phone_book.values() )
281
283
dict_values(['+3206785246863', '+14357535455', '+244562726258', '+4407485376242'])
282
284
283
- >>> my_phone_book.items()
285
+ >>> print( my_phone_book.items() )
284
286
dict_items([('Brienne', '+3206785246863'), ('Cersei', '+14357535455'), ('Davos', '+244562726258'), ('Arya', '+4407485376242')])
285
287
286
288
As you can see, ` .keys() ` and ` .values() ` do what you'd expect: they return the
@@ -290,20 +292,20 @@ These are sort of like tuples: you can't edit them, and if you want to do
290
292
anything with them other than read them as a complete entity, you'll have to
291
293
cast them as a list:
292
294
293
- >>> list(my_phone_book.keys())
295
+ >>> print( list(my_phone_book.keys() ))
294
296
['+3206785246863', '+14357535455', '+244562726258', '+4407485376242']
295
297
296
- >>> list(my_phone_book.keys())[2]
298
+ >>> print( list(my_phone_book.keys())[2])
297
299
'+244562726258'
298
300
299
301
The last one there, ` .items() ` is interesting. It returns all of the data in
300
302
your dictionary, but dumps it out as ` dict_items ` which is a sort of * tuple of
301
303
tuples* . This allows you to reference your dictionary with list syntax:
302
304
303
- >>> tuple(my_phone_book.items())[0]
305
+ >>> print( tuple(my_phone_book.items())[0])
304
306
('Brienne', '+3206785246863')
305
307
306
- >>> tuple(my_phone_book.items())[0][1]
308
+ >>> print( tuple(my_phone_book.items())[0][1])
307
309
'+3206785246863'
308
310
309
311
Truth be told though, you probably won't be accessing these values directly
0 commit comments