Here's what you'll cover in the tutorial so firstly we'll have a look at what sets are and how they can be used in python then we'll take a brief digression to.
Look at a very small amount of theory on sets less than a minute or two i promise we'll then look at how you can create sets in python before going on to look at some set operations like getting the.
Intersection and union of two sets we'll then look at some examples of sets firstly how to remove duplicates from a list shopping list secondly how to count the number of.
Unique letters and words in a string of text and thirdly we'll look at how to compare two lists to see which premier league teams were relegated and promoted in.
Successive seasons at the top right of the screen a link will appear about now and you can click on that to see any files or exercises to do with this tutorial.
What they're not and how you might use them so here's some examples of sets this is a set of all the prime numbers less than 10..
This is a set of all the characters from friends who will meet again shortly and this is a set of all the famous ex preston north end football managers but i don't think you needed me to tell you.
That so what sets don't have is two main things they don't have duplicate values and that turns out to be quite useful and also they don't have any concept.
Whatsoever of ordering so how would you use them well there's a couple of main ways one is for removing duplicates from a list.
And the other is for comparing two lists so what we're going to do now is have a look at how you can create sets and then we'll have a look at some practical examples.
Before we look at how you can actually
Use sets in practice let's have a quick look at some set ethereal apologies if this takes you back to unpleasant memories of school day maths.So let's imagine you have two sets you have a set of the people or the characters who have appeared in friends the tv series and you have a set of antarctic ice shelves.
You might think they don't have a great deal in common but you'll be wrong if you look at this diagram it shows that there's an intersection between the two which is occupied by ross so that's.
A non-empty set on the left hand side you can see a list of all the characters who are in the friends date friends tv series but who aren't ice shelves.
And then on the right hand side and i think you've probably got the point by now you can see ice shells which aren't friends characters this is called as you probably know.
Already a venn diagram and it's a useful way of understanding how sets work what we're now going to do is have a look at how this works in python as well.
As in maths don't worry about the scary diagram i'm just putting it up for reference when we do the practical examples you'll barely need to know about it.
But some people will find it useful so the main concept in set theory the main two concepts in set theory are intersection and union so the intersection of two sets is the.
Thing they have in common and that's written in mathematics as a intersection b and in python as a ampersand b it's that simple.
The other key concept is um
The union and the union of two sets is the elements which in both sets and in the intersection so.That's everything and that's written in python with a pipe symbol which you'll find at the bottom left of your keyboard and then there's two more complicated.
Concepts called the complement and the elements which in one set and not in both which you probably won't need most of the time that's all the background theory for.
Sets it's time to start creating some what we're going to do now is to create a couple of sets um i've created a file called working with sets dot py and to create a set let's have a set of.
All the friends you type in the name your variable which is going to refer to it which i'm going to call friends and then you type in not a round bracket not a square bracket but.
A curly bracket and that denotes either a set or as we'll see probably in the next tutorial it can also denote a dictionary so what i'm going to do is paste in my.
List of friends if you're wondering how i did this little bit of magic with the multiple clipboard try if you're running windows try pressing holding down the windows key and pressing v.
It's amazing how much time it saves so i'll just tidy that up you don't need to put this on multiple lines as i've done there um but it probably makes it easy to read normally you'd have to use.
A continuation character like a backslash but for some reason python is quite forgiving when you have items in a list or a set or tuple or whatever it may be.
So my second set is going to be the antarctica shelves which i will call shelves and i'll set that equal to and again i can press windows v although.
It was in my immediate clipboard and i can bring in the list of selected antarctic ice shelves and hopefully if you're watching this in about 10 20 years time they're all still there but.
We shall see so having created a set i could then print them out so to print out a set it's exactly the same as anything else i could print out.
The friends and i could print out the shelves and if i now run that program you'll see it just duplicates or echoes back to me what i've just typed in.
What i can also do is prove what type they are so i'm going to prove these as sets and to do that i can use the evit ever useful type function.
So i'll print out the type of the friends and the type of the shelves just to prove to you that i'm working with sets and not something else.
But you probably believed me at this point so that's how you can create sets now we're going to look at some operations you can perform upon them.
So before i show you some set operations let's quickly comment these lines of code out and then what we're going to do is start by showing the union between the two.
Sets so the union is in everything in both sets so to do this i will create a variable called union and i'll set it equal to the friends.
Also the shelves and what i could then do is print that out and what you'll see is when you run that it gives me characters or ice shelves.
The important thing about this is ross only appears once even though he's in both sets i've told you already that a set cannot contain a duplicate and so any duplicates will be stripped out.
So that's one example so the next thing we'll do is we'll show the intersection so these are things which in one set but not the other so to do this i'll create my variable.
Called intersection you don't have to call them names like this you could call them anything you like and i'll take the friends and to get the.
Intersection i use the ampersand i think that's quite logical they've got to be in both the two sets and again i'll print that out and what you should see when you run.
This it gives you just ross because ross is the only person or i shouldn't call him a person the only thing which is in both the two sets those are the two basic things you'll.
Normally want to do let's now show all the ice shelves which aren't friends there should be two of them i can't actually remember the name i'll be.
Honest so let's create a variable called i shelves not friends which kind of makes the comment.
Redundant but never mind so to do this i'll take the list of shells and i'll subtract from it the friends and as always it's just so.
Nice and logical and exactly the way you'd have wanted it to be written so i can print out my variable called i shells not friends and i should get mcmurdo and ronnie.
Filchner how could i forget that those are the two elements which are in one set but not in the other and of course i could have done it the other way around to get all the friends who weren't ice.