@@ -39,18 +39,18 @@ which is where most people would expect things to start. This means that the
3939* first* element of a list is referred to with a ` 0 ` , the second element is
4040referred to with a ` 1 ` , and so on. Perhaps an example would be helpful:
4141
42- >>> places_to_visit[0]
42+ >>> print( places_to_visit[0])
4343 'Mexico'
44- >>> places_to_visit[2]
44+ >>> print( places_to_visit[2])
4545 'Kenya'
4646
4747Positive numbers aren't the only thing you can use for indexes though. Negative
4848numbers invert the index, so you can get the * last* element of a list by using
4949` -1 ` , or the third-to-last by using ` -3 ` :
5050
51- >>> places_to_visit[-1]
51+ >>> print( places_to_visit[-1])
5252 'New Zealand'
53- >>> places_to_visit[-3]
53+ >>> print( places_to_visit[-3])
5454 'Kenya'
5555
5656Try 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
7171all 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 ")
7272for a more complete reference:
7373
74- >>> places_to_visit
74+ >>> print( places_to_visit)
7575 ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
7676
7777 >>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
7878 >>> places_to_visit[1] = "Peru"
79- >>> places_to_visit
79+ >>> print( places_to_visit)
8080 ['Mexico', 'Peru', 'Kenya', 'Nepal', 'New Zealand']
8181
8282 >>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
83- >>> places_to_visit.pop()
83+ >>> print( places_to_visit.pop() )
8484 'New Zealand'
85- >>> places_to_visit
85+ >>> print( places_to_visit)
8686 ['Mexico', 'Portugal', 'Kenya', 'Nepal']
8787
8888 >>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
8989 >>> places_to_visit.append("Colombia")
90- >>> places_to_visit
90+ >>> print( places_to_visit)
9191 ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand', 'Colombia']
9292
9393Try some of these out yourself:
@@ -103,22 +103,24 @@ But we're not finished! You can do even more interesting things with `[` and
103103to get the first four elements of a list * as another list* . That's easy with
104104the subset syntax:
105105
106- >>> places_to_visit[0:4]
106+ >>> print( places_to_visit[0:4])
107107 ["Mexico", "Portugal", "Kenya", "Nepal"]
108- >>> places_to_visit[:4]
108+
109+ >>> print(places_to_visit[:4])
109110 ["Mexico", "Portugal", "Kenya", "Nepal"]
110111
111112You can do some interesting things with negative numbers too:
112113
113- >>> places_to_visit[-3:4]
114+ >>> print( places_to_visit[-3:4])
114115 ['Kenya', 'Nepal']
115- >>> places_to_visit[-3:]
116+
117+ >>> print(places_to_visit[-3:])
116118 ['Kenya', 'Nepal', 'New Zealand']
117119
118120...and as getting a subset returns a list itself, you can get a subset of a
119121subset:
120122
121- >>> places_to_visit[0:4][-1]
123+ >>> print( places_to_visit[0:4][-1])
122124 'Nepal'
123125
124126### Nesting
@@ -128,15 +130,15 @@ you from putting a list inside a list. In fact, you can put a list inside a
128130list, inside a list, inside a... you get the idea. You're only limited by how
129131many levels deep you can go before your code is too confusing:
130132
131- cake_flavours = ["chocolate", ["chocolate", "vanilla"], "red velvet"]
133+ >>> cake_flavours = ["chocolate", ["chocolate", "vanilla"], "red velvet"]
132134
133- cake_flavours[0]
135+ >>> print( cake_flavours[0])
134136 'chocolate'
135137
136- cake_flavours[1]
138+ >>> print( cake_flavours[1])
137139 ['chocolate', 'vanilla']
138140
139- cake_flavours[1][1]
141+ >>> print( cake_flavours[1][1])
140142 'vanilla'
141143
142144There's a lot more things you can do with lists including concatenating,
@@ -152,31 +154,31 @@ will result in a `TypeError`.
152154
153155Tuples are represented using ` ( ` and ` ) ` :
154156
155- places_to_visit = ("Mexico", "Portugal", "Kenya", "Nepal", "New Zealand")
157+ >>> places_to_visit = ("Mexico", "Portugal", "Kenya", "Nepal", "New Zealand")
156158
157- places_to_visit[1]
159+ >>> print( places_to_visit[1])
158160 'Portugal'
159161
160- places_to_visit[0:3]
162+ >>> print( places_to_visit[0:3])
161163 ('Mexico', 'Portugal', 'Kenya')
162164
163165This however will fail with a ` TypeError ` :
164166
165- places_to_visit[1] = "Canada"
167+ >>> places_to_visit[1] = "Canada"
166168
167169Tuples are commonly used in cases where you're defining something that
168170shouldn't ever change, but should you ever need to modify something that's in a
169171tuple, you can always * cast* it as a list:
170172
171- my_tuple = (1, 2, 3)
173+ >>> my_tuple = (1, 2, 3)
172174
173- my_list = list(my_tuple)
174- my_list[2] = 99
175+ >>> my_list = list(my_tuple)
176+ >>> my_list[2] = 99
175177
176- my_list
178+ >>> print( my_list)
177179 [1, 2, 99]
178180
179- my_tuple
181+ >>> print( my_tuple)
180182 (1, 2, 3)
181183
182184This 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
194196lookup table or crude database in an application. For this tutorial we'll use
195197a dictionary for a phone book:
196198
197- my_phone_book = {
199+ >>> my_phone_book = {
198200 "Arya": "+4407485376242",
199201 "Brienne": "+3206785246863",
200202 "Cersei": "+14357535455",
@@ -210,7 +212,7 @@ though. This will give you Cersei's phone number:
210212There's nothing restricting you to strings as values in your dictionary though.
211213You can have * anything* in there:
212214
213- my_crazy_dictionary = {
215+ >>> my_crazy_dictionary = {
214216 "a number": 7,
215217 "a float": 1.23456789,
216218 "a string": "hello, world!",
@@ -228,7 +230,7 @@ can be used for keys, though this can sometimes lead to reduced readability,
228230so use this with caution. For example, while this is valid Python, it's not
229231exactly easy to understand:
230232
231- my_unreadable_dictionary = {
233+ >>> my_unreadable_dictionary = {
232234 ("this", "is", "a", "tuple!"): {"a string": "hello, world!"}
233235 }
234236
@@ -247,15 +249,15 @@ doing this should hopefully feel intuitive by now:
247249
248250Change a value for a key:
249251
250- my_phone_book["Brienne"] = "+830685432195"
252+ >>> my_phone_book["Brienne"] = "+830685432195"
251253
252254Add a new key/value pair:
253255
254- my_phone_book["Ellaria"] = "+560538942621"
256+ >>> my_phone_book["Ellaria"] = "+560538942621"
255257
256258This one is new: delete a key/value pair:
257259
258- del(my_phone_book["Ellaria"])
260+ >>> del(my_phone_book["Ellaria"])
259261
260262Now that you've got the tools to change your dictionaries, give a shot
261263yourself. 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()`,
274276 "Cersei": "+14357535455",
275277 "Davos": "+244562726258"
276278 }
277- >>> my_phone_book.keys()
279+ >>> print( my_phone_book.keys() )
278280 dict_keys(['Davos', 'Cersei', 'Brienne', 'Arya'])
279281
280- >>> my_phone_book.values()
282+ >>> print( my_phone_book.values() )
281283 dict_values(['+3206785246863', '+14357535455', '+244562726258', '+4407485376242'])
282284
283- >>> my_phone_book.items()
285+ >>> print( my_phone_book.items() )
284286 dict_items([('Brienne', '+3206785246863'), ('Cersei', '+14357535455'), ('Davos', '+244562726258'), ('Arya', '+4407485376242')])
285287
286288As 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
290292anything with them other than read them as a complete entity, you'll have to
291293cast them as a list:
292294
293- >>> list(my_phone_book.keys())
295+ >>> print( list(my_phone_book.keys() ))
294296 ['+3206785246863', '+14357535455', '+244562726258', '+4407485376242']
295297
296- >>> list(my_phone_book.keys())[2]
298+ >>> print( list(my_phone_book.keys())[2])
297299 '+244562726258'
298300
299301The last one there, ` .items() ` is interesting. It returns all of the data in
300302your dictionary, but dumps it out as ` dict_items ` which is a sort of * tuple of
301303tuples* . This allows you to reference your dictionary with list syntax:
302304
303- >>> tuple(my_phone_book.items())[0]
305+ >>> print( tuple(my_phone_book.items())[0])
304306 ('Brienne', '+3206785246863')
305307
306- >>> tuple(my_phone_book.items())[0][1]
308+ >>> print( tuple(my_phone_book.items())[0][1])
307309 '+3206785246863'
308310
309311Truth be told though, you probably won't be accessing these values directly
0 commit comments