Python-3.4.3/C3/Sets-in-Python/English
|
|
Show Slide title | Welcome to the spoken tutorial on Sets in Python. |
Show Slide
Objectives
|
In this tutorial, you will learn to,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Slide: Sets | First let us see the overview of sets.
|
Open the terminal | Let us start ipython.
|
Type ipython3 and press Enter | Type ipython3 and press Enter.
|
Type,
a_list = [1, 2, 1, 4, 5, 6, 2] a = set(a_list) a
|
Let us look at how to input sets.
a underscore list is equal to inside square brackets 1 comma 2 comma 1 comma 4 comma 5 comma 6 comma 2
|
Type,
b = {1, 2, 1, 4, 5, 6, 2} b
|
Sets can also be created directly as follows:
b is equal to inside curly braces 1 comma 2 comma 1 comma 4 comma 5 comma 6 comma 2
|
Type, c = set()
|
To create an empty set, type,
c is equal to set open and close brackets
|
Type,
b_list = [[1], 2, 1, 6, 2] b = set(b_list)
|
Sets can contain numbers, strings and tuples.
b underscore list is equal to inside square brackets again inside square brackets 1 comma 2 comma 1 comma 6 comma 2
|
Type,
f = set([1, 2, 3, 5, 8]) p = set([2, 3, 5, 7,11,13]) |
Now let us perform some operations on sets.
p is the set of prime numbers from 1 to 15. |
Type,
f.add(13) f |
Various operations can be performed on sets.
f dot add inside brackets 13
|
Type,
f.add(13) f |
add method has no effect if the element is already present.
f dot add inside brackets 13
|
Type,
p.remove(13) p
|
Next we will learn to remove an element using remove method.
p dot remove inside brackets 13
|
Type,
p.remove(18)
|
If the element is not a member, it raises a KeyError.
p dot remove inside brackets 18
|
Type,
f | p f.union(p) |
The pipe character stands for union.
f pipe p
|
Type,
f & p f.intersection(p) |
The ampersand character stands for intersection.
f ampersand p
|
Type,
f - p
|
Type,
f minus p
|
Type,
f ^ p f.symmetric_difference(p) |
Type,
f caret p
|
Type,
a = set([1, 2, 3, 4]) b = set([1, 2]) b <= a b.issubset(a) |
Sets also support checking of subsets.
a is equal to set inside brackets inside square brackets 1 comma 2 comma 3 comma 4
|
Type,
b >= a b.issuperset(a)
|
Sets also support checking of supersets.
b greater than or equal to a
|
Type,
a <= a a >= a |
Every set is a subset as well as a superset of itself.
a less than or equal to a a greater than or equal to a
|
Type,
for x in a: print(x)
|
Elements of a set can be accessed using for loop.
for x in a colon print inside brackets x
|
Type,
len(a) |
The length and containership check on sets are similar as in lists and tuples.
len inside brackets a
|
Type,
1 in a 7 in a |
Type,
1 in a 7 in a
|
Sets do not support indexing.
| |
Pause 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.remove(num) |
Type,
marks is equal to inside square brackets 20 comma 23 comma 22 comma 23 comma 20 comma 21 comma 23
marks dot remove inside brackets num
|
Type,
duplicates = set(marks) duplicates |
Type,
duplicates is equal to set inside bracket marks
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve.
|
Show Slide
Solutions
|
And the answers,
|
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.
|
Show Slide
Thank You |
This is Priya from IIT Bombay signing off.
Thanks for watching. |