Difference between revisions of "Python-3.4.3/C3/Sets-in-Python/English"
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| <ce...") |
Nancyvarkey (Talk | contribs) |
||
Line 16: | Line 16: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, you will learn to, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, you will learn to, | ||
− | |||
− | |||
* Create '''sets''' from '''lists''' | * Create '''sets''' from '''lists''' | ||
− | * Perform '''union | + | * Perform '''union, intersection''' and '''symmetric difference operations''' |
* Check if a '''set''' is a '''subset''' of other and | * Check if a '''set''' is a '''subset''' of other and | ||
* Understand various similarities with '''lists''' | * Understand various similarities with '''lists''' | ||
− | |||
− | |||
|- | |- | ||
Line 34: | Line 30: | ||
* '''Python 3.4.3 '''and | * '''Python 3.4.3 '''and | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 42: | Line 36: | ||
Pre-requisites | Pre-requisites | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to | ||
− | |||
* run basic '''Python '''commands on the '''ipython console '''and | * run basic '''Python '''commands on the '''ipython console '''and | ||
* use '''lists''' | * use '''lists''' | ||
Line 56: | Line 49: | ||
− | The set itself is mutable. | + | The '''set''' itself is '''mutable'''. |
− | We can add or remove items from it. | + | We can add or remove '''items''' from it. |
|- | |- | ||
Line 100: | Line 93: | ||
− | We can see that duplicates are removed and the '''set''' contains only unique elements. | + | We can see that duplicates are removed and the '''set''' contains only unique '''elements'''. |
|- | |- | ||
Line 111: | Line 104: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Sets can also be created directly as follows: | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Sets''' can also be created directly as follows: |
Line 122: | Line 115: | ||
− | As before, we see that the set contains only unique elements. | + | As before, we see that the '''set''' contains only unique '''elements'''. |
|- | |- | ||
Line 134: | Line 127: | ||
− | Note that '''d '''''is equal to open and close curly braces | + | Note that '''d '''''is equal to open and close curly braces'' creates an empty dictionary, not an empty '''set'''. |
|- | |- | ||
Line 145: | Line 138: | ||
Highlight '''TypeError''' | Highlight '''TypeError''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Sets''' can contain '''numbers | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Sets''' can contain '''numbers, strings''' and '''tuples.''' |
− | But cannot contain mutable elements such as '''lists''' or '''dictionaries'''. | + | But cannot contain '''mutable elements''' such as '''lists''' or '''dictionaries'''. |
Line 173: | Line 166: | ||
− | Type the sets as shown. | + | Type the '''sets''' as shown. |
Line 189: | Line 182: | ||
− | First we will add an element to a set by using '''add''' | + | First we will add an '''element''' to a '''set''' by using '''add method'''. |
Line 208: | Line 201: | ||
'''f''' | '''f''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''add''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''add method''' has no effect if the '''element''' is already present. |
Line 230: | Line 223: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn to remove an element using '''remove ''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn to remove an '''element''' using '''remove method'''. |
Line 250: | Line 243: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| If the element is not a member, it raises a '''KeyError.''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| If the '''element''' is not a '''member''', it raises a '''KeyError.''' |
Line 258: | Line 251: | ||
− | '''KeyError '''is raised since | + | '''KeyError '''is raised since '''element 18 '''is not present in '''p.''' |
|- | |- | ||
Line 285: | Line 278: | ||
'''f.intersection(p) ''' | '''f.intersection(p) ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The '''ampersand | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The '''ampersand''' character stands for '''intersection'''. |
Line 394: | Line 387: | ||
'''a >= a''' | '''a >= a''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Every set is a subset as well as a superset of itself. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Every '''set''' is a '''subset''' as well as a '''superset''' of itself. |
Line 415: | Line 408: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Elements of a '''set''' can be accessed using '''for | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Elements of a '''set''' can be accessed using '''for loop'''. |
Line 425: | Line 418: | ||
− | The items from the set will not appear in any specific order. | + | The '''items''' from the '''set''' will not appear in any specific order. |
|- | |- | ||
Line 449: | Line 442: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, | ||
− | '''1 in a | + | '''1 in a''' |
− | '''7 in a | + | '''7 in a''' |
Line 474: | Line 467: | ||
Exercise 1 | Exercise 1 | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Given a list of marks, as shown | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Given a list of marks, as shown, list all the duplicate marks. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution. |
|- | |- | ||
Line 508: | Line 498: | ||
− | '''remove''' | + | '''remove method''' only removes the first occurrence of an '''element''' from the '''list'''. |
|- | |- | ||
Line 527: | Line 517: | ||
− | Hence, we have obtained our required solution | + | Hence, we have obtained our required solution. |
|- | |- | ||
Line 539: | Line 529: | ||
− | In this tutorial, we have learnt to, | + | In this tutorial, we have learnt to, |
− | + | ||
− | + | ||
* Make '''sets''' from '''lists '''or by using curly braces | * Make '''sets''' from '''lists '''or by using curly braces | ||
− | * Perform '''union | + | * Perform '''union, intersection''' and '''symmetric difference operations''' |
− | * Check if a set is a subset of other using the ''less than or equal to'' operator | + | * 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 | + | * Understand the various similarities with '''lists''' like length and containership |
− | + | ||
− | + | ||
|- | |- | ||
Line 556: | Line 542: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve. |
# If '''a '''is assigned as follows, what is '''set '''''of'' '''a''' | # If '''a '''is assigned as follows, what is '''set '''''of'' '''a''' | ||
− | # Given '''odd''' and '''squares''' as shown.How do you find the '''symmetric difference''' of these two sets? | + | # Given '''odd''' and '''squares''' as shown. How do you find the '''symmetric difference''' of these two '''sets'''? |
− | # If '''a''' is a set, how do you check if a variable '''b''' exists in '''a'''? | + | # If '''a''' is a '''set''', how do you check if a variable '''b''' exists in '''a'''? |
− | + | ||
− | + | ||
|- | |- | ||
Line 574: | Line 558: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' ''' | ||
− | # '''set '''''of '''''a''' will have all the common elements in the list '''a''', that is 1 ''comma'' 2 ''comma'' 3 ''comma'' 5 ''comma'' 8. | + | # '''set '''''of '''''a''' will have all the common '''elements''' in the '''list''' '''a''', that is 1 ''comma'' 2 ''comma'' 3 ''comma'' 5 ''comma'' 8. |
− | # To find the symmetric difference between two sets, we use the operator ''caret''. | + | # To find the '''symmetric difference''' between two '''sets''', we use the operator ''caret''. |
So we can type '''odd '''''caret '''''squares''' | So we can type '''odd '''''caret '''''squares''' | ||
# To check the containership, we type, '''b in a''' | # To check the containership, we type, '''b in a''' | ||
− | |||
− | |||
|- | |- |
Revision as of 21:14, 17 September 2018
|
|
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,
So we can type odd caret squares
|
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. |