File size: 11,076 Bytes
77eaf9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
- Okay, so we've taken a look at loops
    and now we're going to just take a little
    bit of a look at some of the operations
    that you can do with loops.
    - Um, Python has this,
    as we'll soon learn,
    object orient approach to its operators.
    And, the plus can add strings
    and it can add numbers,
    it can add floating-point numbers,
    integer numbers, strings, etc.
    And, so the plus similarly
    works this way with lists.
    The plus looks to its left
    and looks to its right
    and says, what am I adding?
    And in the case that I'm adding the list
    one, two, three and the
    list four, five, six.
    It concatenates them together
    and in this way it sort of
    functions like a string.
    And so, we get one, two,
    three, four, five, six.
    It just concatenates lists,
    this list to another list.
    And, it doesn't change a or b,
    just like in any kind
    of assignment statement
    Its calculations on the right side
    don't change the variables and
    then produce a new variable
    and then assign that into c.
    You can also use list slicing.
    It's easy to remember,
    if you remember how strings work,
    lists work exactly the same way.
    So, of course, it's a little tricky,
    the first number's the starting position.
    They start at zero.
    So, one is right there,
    so it's the zero position, one position,
    start at one, right, but go
    up to but not including three.
    There's one, two, three.
    So, this goes up to but
    not including three.
    And, that's why we get
    forty-one, twelve out of that.
    So, up to but not including,
    I'll just say that over
    and over and over again.
    If we do, you can leave
    the first part out,
    you can leave the first part out here
    and you can say up to
    but not including four.
    So, that starts at the
    beginning goes up to
    but not including four,
    and so that's how we get
    that piece right there.
    We can say, um,
    start at the position three,
    zero, one, two, three.
    Start at position three and go to the end.
    Now, the fact the number three is in here,
    is sort of irrelevant.
    Um, three to the end
    is those three numbers.
    And, then you can do the whole
    list with slicing as well.
    Again, these are pretty
    much the exact same examples
    I used when I was doing strings,
    they're pretty much the same.
    There's a number of different methods
    and you can look up all the
    documentation in the list.
    I often just use the dir command
    to remind myself of them,
    append we'll look at,
    count looks for certain
    values in the list,
    extend adds things to the end of the list,
    index looks things up in the list,
    insert allows them,
    the list to sort of be
    expanded in the middle,
    pop pulls things off the top,
    remove removes an item in the middle,
    reverse flips the order of them,
    and sort puts them sort of
    into order based on the values.
    So, let's look at a couple of these.
    Um, so if we build a list from scratch,
    we have a way to ask for an empty list.
    There are a couple of different ways
    to ask for an empty list.
    We could use just two square
    brackets next to each other.
    But, this is a form we
    call a constructor form
    where we say, hey Python, make a list.
    And, in this case the word list
    is like a reserved word to Python,
    it's really reserve class, but, um.
    Say list, list parenthesis
    says makes me an empty list
    and then, assign that list into stuff.
    So, stuff is now a list object,
    it's a type list but it has nothing in it.
    And, then we can call the append methods,
    stuff out of append and stick book in.
    And then, we say oh,
    and that knows how long,
    and the stuff knows how long it is,
    where the end is and how
    to add something to it,
    then add a ninety-nine to
    it and we print it out.
    And, we got book and ninety-nine,
    reminding ourselves that lists,
    while they're often the
    same types of variables,
    same types of values, in the
    various positions in the list,
    it doesn't always have to be that way.
    Then we say, oh, we'll
    stuff append cookie,
    you can keep on going
    and then we end up with
    three things and the cookie.
    We have an in operator,
    it works pretty much like
    the in operator in a string.
    Uh, is nine in my list?
    And, that's pretty simple and
    the answer of course is yes,
    nine is in my list.
    Is fifteen in my list?
    Looking through, no it's not,
    fifteen is not in my list.
    And then, there's the not in operator,
    you can think of that as
    kind of like one operator.
    Is twenty not in the list?
    And the answer, since
    it's not there, is true.
    And, so that's a way to just, you know,
    it's kind of like starts
    with or end for strings.
    Same kind of stuff.
    Lists are in order and they're sortable,
    and so this is something
    we take good advantage of.
    A lot of what computers
    want to do is sort stuff,
    you know look all these things up,
    append them and then get them sorted.
    And so, there is this
    method inside of list
    that's just the sort method.
    So, here we you know put three values
    in zero, one, two positions.
    Zero, one, and two,
    Joseph, Glenn, and Sally.
    And, then we tell the list to sort itself
    and then we print it out.
    Now, this is actually
    sorting the list in place
    which is different like
    than upper and lower,
    because if you remember
    strings are not mutable,
    but lists are mutable and so you say,
    hey, just sort yourself.
    Okay, and so just sort
    yourself and then it sorts it.
    And then, it's in alphabetical order,
    Glenn, Joseph, and Sally.
    I happen to be clever, I
    only put strings in there
    and I put my uppercase and lowercase
    in a very consistent pattern.
    But, the list has changed,
    and if I look at list sub one
    that is the second item, which is Joseph,
    that prints out right down there.
    There's a whole bunch
    of built in functions
    to help manipulate lists,
    the other things I was
    showing was method sort
    is a method that's part of lists
    but there are other functions
    that take lists as their arguments.
    Um, we already talked
    about the len function,
    it tells you how many items there are.
    There is, pretty obvious, max,
    it says go through and find the largest.
    Min, go through and find the smallest.
    Sum, goes through adds them all up.
    And, we can say, let's do average
    by taking the sum of all of them
    and dividing it by the length.
    And you might think to yourself, oh wow,
    I wish we'd had known
    this a few chapters back
    when we were having to
    write all those loops
    to do max, min, sum,
    largest, smallest, etc.
    You can kind of think in
    your mind that inside each
    one of these functions
    is a loop that does,
    pretty much, what you
    did in those chapters.
    Part of the reason we did that back then,
    was even though these things were here,
    was they're kind of easy
    loops to understand.
    And so, those are there and
    basically there allows two different ways
    of building loops to do
    the maximum and minimum.
    Now, it's not necessarily
    all that much easier to
    do something using these
    because you either can do them the old way
    or you can make a list
    and use these functions.
    So, let's take a look, and
    I'll just say that these
    two bits of code are doing
    the exact same thing,
    and what they are is
    implementing a program,
    that's going to repeatedly ask for numbers
    until we type the word done,
    and then it's going to compute the average
    and tell us what they are.
    And, so using sort of the
    stuff from the loop chapter,
    we start with a total
    variable and a count variable,
    set them to zero and
    then we read a number,
    check for done to break out,
    but then we convert it
    to a floating-point value
    and then we say total
    equals total plus value
    and count equals count plus one.
    So, this is gonna run over
    and over and over again,
    how ever many times
    we're going to do this.
    And then, it's going to
    pop out and when it's done,
    it's going to have this value of total,
    the running total will
    become the overall total
    divided by count and then it'll
    print the average out, okay.
    So, that, that's kind of
    how we would have done this
    before we knew how to do this with lists.
    Now, let's take a look at the other one.
    And, the other one, we say
    lets make an empty list,
    remember this is that constructive syntax
    that says to Python, make me an empty list
    and assign the empty list.
    It has nothing in it. Right.
    But it is a list.
    Has nothing in it, into
    the variable numlist.
    Now, we're going to write another loop,
    we're going to, this
    part here is the same.
    These three lines.
    Read the number, if it's done,
    quit and convert to value.
    But instead of doing the
    actual calculation right now,
    all we're going to do is
    just append it to the list.
    So, the list will start out empty,
    then the three will be in the list,
    then the nine will be in the list,
    then the five will be in the list.
    So, we're appending each
    time through the loop
    we're appending into the list.
    So, we're just growing the list
    every time I read it value.
    Instead of actually computing something
    with the value that we've got,
    so in either case we get value
    and in one case we append it to the list.
    And then finally, it
    finishes, the break happens
    and then we just say, oh hey Python,
    sum up everything in the list,
    add these three numbers together,
    and then take the, divide it by the length
    of all those things and
    you'll have the average.
    And, so these two things give
    us exactly the same output.
    Now, there is one difference,
    if there was like one million
    or one billion numbers,
    they actually all have to be stored
    in the memory simultaneously.
    Where as here, it's actually
    doing the calculation
    of the billion numbers and
    not using up so much memory.
    For most of the things
    you're going to be doing,
    the difference in memory, there
    is a difference in memory,
    this uses, this one here uses more memory,
    but I can't draw very well.
    More memory, um, it uses more memory,
    but it doesn't really matter
    by the time it's all said and done.
    And so, for you, the
    difference between these things
    is not all that significant,
    but it's important to
    understand that they're just two
    techniques to accomplish
    the same thing with lists.