PK iA2^g3p p ! Python_Glossary/01-Welcome.en.srt1
00:00:00,000 --> 00:00:03,000
Hi. This is the Python glossary.
2
00:00:03,000 --> 00:00:07,000
There's going to be a lot of new and potentially confusing functions and techniques
3
00:00:07,000 --> 00:00:10,000
that we use in CS212.
4
00:00:10,000 --> 00:00:14,000
When that happens, I'm going to try to record something to help you all out.
5
00:00:14,000 --> 00:00:16,000
This is the place where I'm going to post that.
6
00:00:16,000 --> 00:00:19,000
I might not be able to get to every single function that's covered in the course,
7
00:00:19,000 --> 00:00:23,000
but if there are any that are particularly confusing to you, you should let me know in the forums.
8
00:00:23,000 --> 00:00:26,000
Remember, if there is anything that I don't cover,
9
00:00:26,000 --> 99:59:59,000
you should check out the Python or just do a search.
PK iAF $ Python_Glossary/02-Set_Theory.en.srt1
00:00:00,000 --> 00:00:04,000
The set is an important tool in Python, and today we're going to talk about it.
2
00:00:04,000 --> 00:00:09,000
A set in Python is simply an unordered collection of elements with no duplicates.
3
00:00:09,000 --> 00:00:13,000
This unordered and no duplication--these are important clauses.
4
00:00:13,000 --> 00:00:18,000
The sets used by Python are just a programmatic version of the sets used by mathematicians.
5
00:00:18,000 --> 00:00:20,000
Let's talk a bit about set theory.
6
00:00:20,000 --> 00:00:27,000
Let's say we have a basket of fruit, and let's say it contains 5 apples, 3 oranges, and 2 tomatoes.
7
00:00:27,000 --> 00:00:32,000
These I suppose would be the apples or maybe the tomatoes, but we don't have to worry about that.
8
00:00:32,000 --> 00:00:36,000
If we want to represent this as a set, we wouldn't care about this quantity.
9
00:00:36,000 --> 00:00:38,000
The fact that there is 5 apples doesn't matter.
10
00:00:38,000 --> 00:00:42,000
In fact, I could just write apple, orange, and tomato
11
00:00:42,000 --> 00:00:44,000
and label this set "fruit."
12
00:00:44,000 --> 00:00:50,000
The elements of this set "fruit" are simply the entries apple, orange, and tomato.
13
00:00:50,000 --> 00:00:53,000
Let's say we also have a set called "vegetable."
14
00:00:53,000 --> 00:00:59,000
Some of the elements in this set would be broccoli, carrot, and tomato.
15
00:00:59,000 --> 00:01:02,000
When I was coming up with this question there was some heated debate in the office
16
00:01:02,000 --> 00:01:07,000
about whether tomatoes are fruits or vegetables. Let's just include it in both.
17
00:01:07,000 --> 00:01:11,000
That means that when I draw the circle that encompasses this set,
18
00:01:11,000 --> 00:01:14,000
it actually has an overlap with the set fruit.
19
00:01:14,000 --> 00:01:17,000
In set theory, this overlap is called the intersection.
20
00:01:17,000 --> 00:01:21,000
I'm going to mention these set theory terms, but you don't have to worry about them too much.
21
00:01:21,000 --> 00:01:26,000
This diagram I've drawn here is known in set theory as a Venn diagram.
22
00:01:26,000 --> 00:01:29,000
They can be very useful for visualizing what's going on with sets.
23
00:01:29,000 --> 00:01:31,000
Here comes the interesting part.
24
00:01:31,000 --> 00:01:38,000
We can create new sets by taking subsections or performing operations on these sets.
25
00:01:38,000 --> 00:01:42,000
For example, if we want to know what elements are fruits and vegetables,
26
00:01:42,000 --> 00:01:45,000
which like I said before is called the intersection,
27
00:01:45,000 --> 00:01:51,000
we could write "Fruit & Vegetable," and we could see the only element would be tomato.
28
00:01:51,000 --> 00:01:55,000
I should point out that this symbol and the other symbols I will be using are not
29
00:01:55,000 --> 00:01:57,000
what we usually use in mathematical set theory,
30
00:01:57,000 --> 00:02:00,000
but since they're what we use in Python I'm going to be sticking with this convention.
31
00:02:00,000 --> 00:02:04,000
We could also ask what elements are fruits but not vegetables,
32
00:02:04,000 --> 00:02:06,000
which is known as the set difference.
33
00:02:06,000 --> 00:02:11,000
The only element that are fruits but not vegetables are apple and orange.
34
00:02:11,000 --> 00:02:14,000
We could ask which elements are fruits or vegetables.
35
00:02:14,000 --> 00:02:20,000
Here I've used the vertical line to indicate that. This would give us all of this entries.
36
00:02:20,000 --> 00:02:23,000
All of these entries are either a fruit or a vegetable or both.
37
00:02:23,000 --> 00:02:25,000
The last operation I'm going to talk about is asking
38
00:02:25,000 --> 00:02:30,000
which elements are fruits or vegetables but not both.
39
00:02:30,000 --> 00:02:32,000
This is known as the symmetric difference.
40
00:02:32,000 --> 00:02:37,000
Here we've used the caret to indicate that, which is not the same as this carrot.
41
00:02:37,000 --> 00:02:41,000
This is just all of the entries except for tomato.
PK iA(NM ) Python_Glossary/03-Set_Theory_Quiz.en.srt1
00:00:00,000 --> 00:00:05,000
Okay, so now that you've seen an example, let's do a quiz makes things a bit more general.
2
00:00:05,000 --> 00:00:08,000
For this quiz we have two sets--A and B.
3
00:00:08,000 --> 00:00:11,000
A is represented by the blue circle, B by the red.
4
00:00:11,000 --> 00:00:16,000
I want you to match the set operation, and these are the four set operations.
5
00:00:16,000 --> 00:00:18,000
to the appropriate Venn diagram.
6
00:00:18,000 --> 00:00:22,000
I've shaded in in each of these diagrams a section,
7
00:00:22,000 --> 00:00:25,000
and that section corresponds to one of these operations.
8
00:00:25,000 --> 00:00:29,000
I want you to enter the appropriate number into the appropriate box.
PK iAtK9 9 2 Python_Glossary/04-Set_Theory_Quiz_Solution.en.srt1
00:00:00,000 --> 00:00:02,000
This is the answer.
2
00:00:02,000 --> 00:00:07,000
A minus B corresponds to 3, because this is all the elements that are in A but not B.
3
00:00:07,000 --> 00:00:09,000
We haven't filled in this area.
4
00:00:09,000 --> 00:00:12,000
A or B is number 2.
5
00:00:12,000 --> 00:00:15,000
All of the elements are either in A or B.
6
00:00:15,000 --> 00:00:20,000
A & B is number 1. I filled in this middle area corresponding to both sets.
7
00:00:20,000 --> 00:00:24,000
The symmetric difference of A and B is 4.
8
00:00:24,000 --> 00:00:28,000
All the elements that are in A or B but not in both, and I've left out this region.
9
00:00:28,000 --> 00:00:33,000
Okay, so now that you understand the basics of set theory, we can implement this in Python.
PK iAa % Python_Glossary/05-Python_Sets.en.srt1
00:00:00,000 --> 00:00:03,000
If we want to define the set Fruit like we did before,
2
00:00:03,000 --> 00:00:06,000
we can write the following, and when I print Fruit I get the following.
3
00:00:06,000 --> 00:00:09,000
Notice that I could have included apple twice,
4
00:00:09,000 --> 00:00:14,000
and when I print I get the exact same thing because sets don't take duplicates into account.
5
00:00:14,000 --> 00:00:19,000
I can also print the set Vegetable and notice that the order that I defined this set in
6
00:00:19,000 --> 00:00:23,000
is not necessarily the order that it gets printed in. Sets don't care about order.
7
00:00:23,000 --> 00:00:27,000
Once we've defined these sets, we can check for membership.
8
00:00:27,000 --> 00:00:30,000
For example, maybe I want to know if apple is in the set Fruit,
9
00:00:30,000 --> 00:00:33,000
and when I run this statement I get a true.
10
00:00:33,000 --> 00:00:35,000
Apple is in Fruit.
11
00:00:35,000 --> 00:00:39,000
Sense apple is not in Vegetable, this statement gives me False.
12
00:00:39,000 --> 00:00:44,000
For a quiz, what's going to be the output when I print this statement:
13
00:00:44,000 --> 00:00:49,000
fruit--with a vertical bar--vegetable? Select the best option.
PK iA1
_ _ . Python_Glossary/06-Python_Sets_Solution.en.srt1
00:00:00,000 --> 00:00:04,000
When I run this, we remember that this is the or operator,
2
00:00:04,000 --> 00:00:07,000
so we get the set that is all of the elements--
3
00:00:07,000 --> 00:00:11,000
tomato, carrot, apple, orange, and broccoli. Great.
4
00:00:11,000 --> 00:00:14,000
Now I think you have a good understanding of what sets are.
5
00:00:14,000 --> 00:00:18,000
This is a powerful programming tool, and we're going to use it throughout CS212.
6
00:00:18,000 --> 99:59:59,000
There are other set operations, which you can read about in the link below this video.
PK iAC^ 9 Python_Glossary/07-List_Methods_Count_Sort_Reverse.en.srt1
00:00:00,000 --> 00:00:04,000
Unit 1 introduces a lot of new list methods that you may never have seen.
2
00:00:04,000 --> 00:00:08,000
Let's talk about count, sort, and reverse.
3
00:00:08,000 --> 00:00:13,000
List.count(x) returns the number of occurrences of x in the list.
4
00:00:13,000 --> 00:00:16,000
Here we've just called it list.
5
00:00:16,000 --> 00:00:18,000
The way you would use this is you would write the name of your list here
6
00:00:18,000 --> 00:00:21,000
and whatever element you want to count.
7
00:00:21,000 --> 00:00:26,000
I should mention that I've basically taken this description straight from the Python documentation.
8
00:00:26,000 --> 00:00:33,000
List.sort sorts the list in place, and I'll talk a little bit more about what "in place" means later.
9
00:00:33,000 --> 00:00:35,000
List.reverse does what you would think.
10
00:00:35,000 --> 00:00:39,000
It just reverses the elements of the list, and again it does so in place.
11
00:00:39,000 --> 00:00:41,000
Now, let's try using these methods.
12
00:00:41,000 --> 00:00:44,000
Let's define a list of the Udacity professors.
13
00:00:44,000 --> 00:00:47,000
In this list I've included all of the professors of the six classes,
14
00:00:47,000 --> 00:00:50,000
and you'll see that Dave's name comes up twice.
15
00:00:50,000 --> 00:00:53,000
This is because Dave teaches two of the classes.
16
00:00:53,000 --> 00:00:55,000
Also, it makes this problem a bit more interesting.
17
00:00:55,000 --> 00:00:59,000
Let's say we want to know how many times Steve's name shows up on this list.
18
00:00:59,000 --> 00:01:04,000
For this example, it's pretty easy to count ourselves, but let's have Python do it anyway.
19
00:01:04,000 --> 00:01:08,000
We'll define a variable called "stevecount" and set it equal to this.
20
00:01:08,000 --> 00:01:13,000
Here the name of our list.count, and I'm counting the occurrences of Steve.
21
00:01:13,000 --> 00:01:16,000
If I print stevecount, I get 1--just as I'd expect.
22
00:01:16,000 --> 00:01:19,000
We could also count the number of occurrences of Dave.
23
00:01:19,000 --> 00:01:23,000
When I run this I get 2, as expected--Dave and Dave.
24
00:01:23,000 --> 00:01:26,000
Okay, now let's see what happens when we sort this list.
25
00:01:26,000 --> 00:01:33,000
We'll define a new variable called "sortedprofs" and set it equal to udacityprofs.sort.
26
00:01:33,000 --> 00:01:39,000
When I print sorted profs, I get None. Wait. What happened here?
27
00:01:39,000 --> 00:01:42,000
I would have expected a sorted version of this list,
28
00:01:42,000 --> 00:01:46,000
and this is where that "in place" comes from that I was talking about before.
29
00:01:46,000 --> 00:01:50,000
Methods that work in place only modify the existing list.
30
00:01:50,000 --> 00:01:54,000
They modify udacityprofs. They do not create a new list.
31
00:01:54,000 --> 00:02:03,000
In fact, since we've just sorted udacityprofs let's see what happens when we print udacityprofs.
32
00:02:03,000 --> 00:02:06,000
Now you can see that they've been sorted in alphabetical order.
33
00:02:06,000 --> 00:02:08,000
Isn't mutability just wonderful?
34
00:02:08,000 --> 00:02:11,000
Now, poor Wes is alway at the end of these sorted lists.
35
00:02:11,000 --> 00:02:14,000
Let's give Wes some love by reversing this list.
36
00:02:14,000 --> 00:02:19,000
Just as we'd expect, this is the same list but sorted in reverse alphabetical order.
37
00:02:19,000 --> 00:02:22,000
There is plenty other Python list methods.
38
00:02:22,000 --> 99:59:59,000
Feel free to check out the link below this video to see them.
PK iA+i
$ Python_Glossary/08-Generators.en.srt1
00:00:00,000 --> 00:00:04,000
We saw generator expressions and functions in Unit 2,
2
00:00:04,000 --> 00:00:07,000
but some people in the forums still seemed a bit confused.
3
00:00:07,000 --> 00:00:09,000
Don't worry. This is understandable.
4
00:00:09,000 --> 00:00:12,000
The generator is an entirely new object, and it can be confusing.
5
00:00:12,000 --> 00:00:14,000
In the next few videos, we're going to answer three questions.
6
00:00:14,000 --> 00:00:20,000
How do we use generators? I think this is actually an easier question to answer than the second.
7
00:00:20,000 --> 00:00:25,000
What is a generator expression/function/object?
8
00:00:25,000 --> 00:00:29,000
We'll also talk about when and why do we use generators?
9
00:00:29,000 --> 00:00:32,000
Let's start with a problem.
10
00:00:32,000 --> 00:00:37,000
We want to compute the sum of the squares of the integers from 1 to 1 million.
11
00:00:37,000 --> 00:00:44,000
That would be 1 + 4 + 9--and I probably shouldn't write all of these out.
12
00:00:44,000 --> 00:00:47,000
The question is how would you do this?
13
00:00:47,000 --> 00:00:53,000
If you came from CS101, your answer is probably to start with a function.
14
00:00:53,000 --> 00:00:56,000
Maybe your function would have looked something like this
15
00:00:56,000 --> 00:01:00,000
where we make an empty list, append all of the squares to that list,
16
00:01:00,000 --> 00:01:02,000
and then return the sum of that list.
17
00:01:02,000 --> 00:01:06,000
Then you would have called your function with an argument of 1 million,
18
00:01:06,000 --> 00:01:09,000
and you would've gotten your answer.
19
00:01:09,000 --> 00:01:15,000
First let me say this is a find solution. It gets the job done, but it has some problems.
20
00:01:15,000 --> 00:01:20,000
Keeping in mind that we just want to make this calculation once,
21
00:01:20,000 --> 00:01:23,000
what do you think is the biggest problem with this solution.
22
00:01:23,000 --> 00:01:26,000
Do you think it's messy, and it unnecessary clutters up the program?
23
00:01:26,000 --> 00:01:29,000
Do you think it uses memory when it doesn't really have to?
24
00:01:29,000 --> 00:01:33,000
Do you think it runs to slowly? Or do you think the code doesn't work?
25
00:01:33,000 --> 00:01:36,000
You may have several problems with this, so it's somewhat subjective.
26
00:01:36,000 --> 00:01:39,000
Just choose whatever you think is the biggest problem.
PK iAl3 - Python_Glossary/09-Generators_Solution.en.srt1
00:00:00,000 --> 00:00:03,000
For me the biggest problem is that it's messy.
2
00:00:03,000 --> 00:00:06,000
Why would we want this function hanging around, clogging up our program,
3
00:00:06,000 --> 00:00:09,000
if we're only going to use it once?
PK iA &