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