Python-3.4.3/C3/Sets-in-Python/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Sets in Python.
Show Slide

Objectives


In this tutorial, you will learn to,
  • Create sets from lists
  • Perform union, intersection and symmetric difference operations
  • Check if a set is a subset of other and
  • Understand various similarities with lists
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisites

To practise this tutorial, you should know how to
  • run basic Python commands on the ipython console and
  • use lists

If not, see the relevant Python tutorials on this website.

Slide: Sets First let us see the overview of sets.


Sets are unordered collections of unique elements.


The set itself is mutable.


We can add or remove items from it.

Open the terminal Let us start ipython.


Open the terminal.

Type ipython3 and press Enter Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type,

a_list = [1, 2, 1, 4, 5, 6, 2]

a = set(a_list)

a


Highlight the output

Let us look at how to input sets.


Type,

a underscore list is equal to inside square brackets 1 comma 2 comma 1 comma 4 comma 5 comma 6 comma 2


Type, a is equal to set inside brackets a underscore list


Then type, a


We can see that duplicates are removed and the set contains only unique elements.

Type,

b = {1, 2, 1, 4, 5, 6, 2}

b


Sets can also be created directly as follows:


Type,

b is equal to inside curly braces 1 comma 2 comma 1 comma 4 comma 5 comma 6 comma 2


Type, b


As before, we see that the set contains only unique elements.

Type, c = set()


Show text d = {}

To create an empty set, type,

c is equal to set open and close brackets


Note that d is equal to open and close curly braces creates an empty dictionary, not an empty set.

Type,

b_list = [[1], 2, 1, 6, 2]

b = set(b_list)


Highlight TypeError

Sets can contain numbers, strings and tuples.


But cannot contain mutable elements such as lists or dictionaries.


Type,

b underscore list is equal to inside square brackets again inside square brackets 1 comma 2 comma 1 comma 6 comma 2


Now type, b is equal to set inside brackets b underscore list


As you can see, it gives a TypeError.

Type,

f = set([1, 2, 3, 5, 8])

p = set([2, 3, 5, 7,11,13])

Now let us perform some operations on sets.


For this, we will first create a pair of sets.


Type the sets as shown.


Here, f is the set of fibonacci numbers from 1 to 10.

p is the set of prime numbers from 1 to 15.

Type,

f.add(13)

f

Various operations can be performed on sets.


First we will add an element to a set by using add method.


Type,

f dot add inside brackets 13


Now type, f


As you can see, 13 is added to the set f.

Type,

f.add(13)

f

add method has no effect if the element is already present.


Type,

f dot add inside brackets 13


Type, f


As you can see, 13 is not added this time.

Type,

p.remove(13)

p


Next we will learn to remove an element using remove method.


Type,

p dot remove inside brackets 13


Now type, p


As you can see, 13 is removed from the set p.

Type,

p.remove(18)


If the element is not a member, it raises a KeyError.


Type,

p dot remove inside brackets 18


KeyError is raised since element 18 is not present in p.

Type,

f | p

f.union(p)

The pipe character stands for union.


Type,

f pipe p


Or type, f dot union inside brackets p


It gave the union of f and p

Type,

f & p

f.intersection(p)

The ampersand character stands for intersection.


Type,

f ampersand p


Or type, f dot intersection inside brackets p


It gave the intersection of f and p.

Type,

f - p


f.difference(p)

Type,

f minus p


Or type, f dot difference inside brackets p


It gave all the elements that are in f but not in p.

Type,

f ^ p

f.symmetric_difference(p)

Type,

f caret p


Or type, f dot symmetric underscore difference inside brackets p


It gives all the elements in f union p but not in f intersection p.


In mathematical terms, it gives the symmetric difference.


<<PAUSE>>

Type,

a = set([1, 2, 3, 4])

b = set([1, 2])

b <= a

b.issubset(a)

Sets also support checking of subsets.


Type,

a is equal to set inside brackets inside square brackets 1 comma 2 comma 3 comma 4


Type, b is equal to set inside brackets inside square brackets 1 comma 2


Now type, b less than or equal to a


Or type, b dot issubset inside brackets a


It gives a True since b is a subset of a.

Type,

b >= a

b.issuperset(a)


Sets also support checking of supersets.


Type,

b greater than or equal to a


Or type, b dot issuperset inside brackets a


We get False since b is not a superset of a.

Type,

a <= a

a >= a

Every set is a subset as well as a superset of itself.


Type,

a less than or equal to a

a greater than or equal to a


It gives True in both cases since a is a superset and subset of itself.

Type,

for x in a:

print(x)


Elements of a set can be accessed using for loop.


Type,

for x in a colon

print inside brackets x


The items from the set will not appear in any specific order.

Type,

len(a)

The length and containership check on sets are similar as in lists and tuples.


Type,

len inside brackets a


It shows 4.

Type,

1 in a

7 in a

Type,

1 in a

7 in a


It prints True and False respectively

Sets do not support indexing.


Hence, slicing and striding are not valid on sets.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Exercise 1

Given a list of marks, as shown, list all the duplicate marks.
Switch to terminal Switch to the terminal for the solution.
Type,

marks = [20, 23, 22, 23, 20, 21, 23]


marks_set = set(marks)


for num in marks_set:

marks.remove(num)

Type,

marks is equal to inside square brackets 20 comma 23 comma 22 comma 23 comma 20 comma 21 comma 23


marks underscore set is equal to set inside brackets marks


for num in marks underscore set colon

marks dot remove inside brackets num


remove method only removes the first occurrence of an element from the list.

Type,

duplicates = set(marks)

duplicates

Type,

duplicates is equal to set inside bracket marks


Now type, duplicates


We now have duplicate marks in the set duplicates.


Hence, we have obtained our required solution.

Show Slide

Summary slide


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt to,

  • Make sets from lists or by using curly braces
  • Perform union, intersection and symmetric difference operations
  • Check if a set is a subset of other using the less than or equal to operator
  • Understand the various similarities with lists like length and containership
Show Slide

Evaluation


Here are some self assessment questions for you to solve.


  1. If a is assigned as follows, what is set of a
  2. Given odd and squares as shown. How do you find the symmetric difference of these two sets?
  3. If a is a set, how do you check if a variable b exists in a?
Show Slide

Solutions


And the answers,
  1. set of a will have all the common elements in the list a, that is 1 comma 2 comma 3 comma 5 comma 8.
  2. To find the symmetric difference between two sets, we use the operator caret. So we can type odd caret squares
  3. To check the containership, we type, b in a
Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgement

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.


For more details, visit this website.

Show Slide

Thank You

This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst