0:00

Let's cover sets.

Â They are also a type of collection.

Â Sets are a type of collection.

Â This means that like lists and tuples,

Â you can input different Python types.

Â Unlike lists and tuples, they are unordered.

Â This means sets do not record element position.

Â Sets only have unique elements.

Â This means there is only one of a particular element in a set.

Â To define a set, you use curly brackets.

Â You place the elements of a set within the curly brackets.

Â You notice there are duplicate items.

Â When the actual set is created,

Â duplicate items will not be present.

Â You can convert a list to a set by using the function set,

Â this is called type casting.

Â You simply use the list as the input to the function set.

Â The result will be a list converted to a set.

Â Let's go over an example.

Â We start off with a list.

Â We input the list to the function set.

Â The function set returns a set.

Â Notice how there are no duplicate elements.

Â Let's go over set operations.

Â These could be used to change the set.

Â Consider the set A.

Â Let's represent this set with a circle.

Â If you are familiar with sets,

Â this could be part of a Venn diagram.

Â A Venn diagram is a tool that uses shapes usually to represent sets.

Â We can add an item to a set using the add method.

Â We just put the set name followed by a dot,

Â then the add method.

Â The argument is the new element of the set we would like to add,

Â in this case, NSYNC.

Â The set A now has in NSYNC as an item.

Â If we add the same item twice,

Â nothing will happen as there can be no duplicates in a set.

Â Let's say we would like to remove NSYNC from set A.

Â We can also remove an item from a set using the remove method.

Â We just put the set name followed by a dot,

Â then the remove method.

Â The argument is the element of the set we would like to remove,

Â in this case, NSYNC.

Â After the remove method is applied to the set,

Â set A does not contain the item NSYNC.

Â You can use this method for any item in the set.

Â We can verify if an element is in the set using the in command as follows.

Â The command checks that the item,

Â in this case AC/DC, is in the set.

Â If the item is in the set, it returns true.

Â If we look for an item that is not in the set,

Â in this case for the item Who,

Â adds the item is not in the set,

Â we will get a false.

Â These are types of mathematical set operations.

Â There are other operations we can do.

Â There are lots of useful mathematical operations we can do between sets.

Â Let's define the set album set one.

Â We can represent it using a red circle or Venn diagram.

Â Similarly, we can define the set album set two.

Â We can also represent it using a blue circle or Venn diagram.

Â The intersection of two sets is

Â a new set containing elements which are in both of those sets.

Â It's helpful to use Venn diagrams.

Â The two circles that represent the sets combine,

Â the overlap, represents the new set.

Â As the overlap is comprised with the red circle and blue circle,

Â we define the intersection in terms of and.

Â In Python, we use an ampersand to find the union of two sets.

Â If we overlay the values of the set over

Â the circle placing the common elements in the overlapping area,

Â we see the correspondence.

Â After applying the intersection operation,

Â all the items that are not in both sets disappear.

Â In Python, we simply just place the ampersand between the two sets.

Â We see that both AC /DC and Back in Black are in both sets.

Â The result is a new set album; set three,

Â containing all the elements in both albums set one and album set two.

Â The union of two sets is the new set of

Â elements which contain all the items in both sets.

Â We can find the union of the sets album set one and album set two as follows.

Â The result is a new set that has all the elements of album set one and album set two.

Â This new set is represented in green.

Â Consider the new album set, album set three.

Â The set contains the elements AC/DC and Back in Black.

Â We can represent this with a Venn diagram,

Â as all the elements and album set three are in album set one.

Â The circle representing album set one

Â encapsulates the circle representing album set three.

Â We can check if a set is a subset using the issubset method.

Â As album set three is a subset of the album set one,

Â the result is true.

Â There is a lot more you can do with sets.

Â Check out the lab for more examples.

Â