Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's bring it all together with some more practice with the main set operations!
Code examples
# What do both omelets have in common?
denver.intersection(bacon)
denver & bacon
# What is unique to one of the omelets?
denver.difference(bacon)
denver - bacon
# What is unique to the other omelet?
bacon.difference(denver)
bacon - denver
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've just learned four main
set operations in Python and
0:00
their corresponding shorthand operators.
0:03
I know that was a lot all at once and
I hope that my examples helped you out.
0:06
And remember that I have teacher's
notes for you below the video.
0:10
I have an exercise so that we can
practice more with some of these methods.
0:16
I cleaned up my script a little bit so
0:21
you can see our original five
sets of omelette ingredients.
0:23
For this exercise,
0:29
I'm going to be opening my script in
interactive mode or interpreter mode.
0:30
In the terminal we can invoke this
mode by running our script and
0:35
passing in the -i command before
passing in our file name.
0:40
And what this does is that it opens
our script, it opens our code so
0:47
that we can interactively play with it,
it's really fun.
0:52
And what I mean by interactively play with
is that in the interpreter down here,
0:56
I actually have access to everything
that I've typed out in my file up here.
1:04
I'll show you with the dir command.
1:11
I can see that I've got bacon, denver,
smothered, turkey and vegetarian.
1:17
These variables are all loaded into the
interpreter when I pass in the -i command.
1:23
And what that allows me to do is
that when I'm in my interpreter,
1:29
I can use the tab key to
autocomplete typing out all of my
1:35
variable names so
that I can type a little bit faster.
1:41
And what else can I do?
1:49
I can also Invoke Tab completion.
1:51
What that means is that when I put
a dot at the end of a Python object,
2:00
so my smothered variable, and
I press the Tab key two times,
2:05
I get a list of all of the methods
that belong to that object.
2:10
So in this example my smothered
variable is a set data type.
2:15
And when I've tabbed auto-completed here,
2:21
I've retrieved a list of all of
the methods that belong to the set class.
2:24
And we've learned a lot
of these methods already.
2:30
In fact, I've taught a lot of these
already in our previous videos.
2:33
So if you ever need a reminder of
what methods belong in what class,
2:37
try hopping into interpreter mode to
interactively play with your code.
2:42
It's very, very helpful and it's very fun.
2:48
So for this exercise, we'll be
playing around in the interpreter with
2:51
our five variables already loaded in and
we'll walk through this exercise.
2:55
So let's pretend that we're helping
a guest in our restaurant make an order.
3:01
They say, I've looked at your menu, and
3:07
I'm having a hard time making
a decision to choose something to eat.
3:10
Can you help me order?
3:15
Can you help me decide please, between
the Denver omelet and the bacon omelet?
3:17
Let's imagine using a Venn diagram as we
think about how to answer that question.
3:23
First, we can tell them about
the intersection of the two omelets.
3:29
What ingredients that both
omelets have in common.
3:34
The overlapping middle
part of the Venn diagram.
3:37
Then we can tell them about what
ingredients are unique to one of
3:41
the omelets.
3:45
First, let's say the Denver omelet,
that's on the left side.
3:47
And finally, we'll tell them about
the unique ingredients of the bacon omelet
3:51
which will be on the right side.
3:55
The hope is that by presenting the
information on their menu in a different
3:58
way, we can help them make
a decision by comparing and
4:03
contrasting the similarities of
the ingredients between the two omelets.
4:06
So we'll work it out in python
now in our interpreter.
4:12
I'll move this up so that there's
more room for us to play around.
4:16
So first,
what do both omelets have in common?
4:21
Second, what is unique
to one of the omelets?
4:34
And then what is unique
to the other omelet?
4:44
In a Venn diagram of these two omelettes,
4:53
which ingredients are members
of the intersection?
4:56
Remember, they wanted to get
the Denver and the bacon.
5:01
So I can say denver.intersection(bacon).
5:06
And this is why I like interpreter
mode because when I hit Return,
5:12
I get instant feedback.
5:16
I don't have to open my script and
run it in the console again,
5:18
I can just start coding and
receive feedback right away about my code.
5:22
So the ingredients that are in common of
the denver omelet and the bacon omelet
5:27
Are cheese, onions and egg.
5:35
What ingredients are unique
to the denver omelette?
5:40
For the difference in this example,
5:43
we'll say that the denver omelette is
the leftmost set on the Venn diagram.
5:45
So we'll make the difference
method call on the denver set.
5:50
Take the difference of bacon.
5:56
And we see that the denver omelet
has green pepper, ham, and tomato.
5:59
Now, what ingredients
are unique to the bacon omelet?
6:05
This time I'll put bacon
on the left side and
6:08
I'll use my shorthand notation to take
the difference of bacon and denver.
6:12
Bacon minus denver gives me avocado,
mushroom and bacon.
6:17
So to help our customer
answer the question,
6:25
I can tell them that both of
these omelets have in common.
6:28
Let's find that again, bacon and denver.
6:33
They both have cheese, onion and egg.
6:37
The denver omelette has green pepper,
ham and tomato.
6:40
And the bacon omelette has avocado,
mushrooms and bacon.
6:43
And our customer says, you know what,
I don't like mushrooms.
6:48
I would like to order the denver omelette,
please.
6:53
Cool, we take that order back to
the kitchen and everybody's happy!
6:57
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up