<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FSets%2FEnglish</id>
		<title>Python/C3/Sets/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FSets%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Sets/English&amp;action=history"/>
		<updated>2026-05-15T09:36:25Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Sets/English&amp;diff=478&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial on '…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Sets/English&amp;diff=478&amp;oldid=prev"/>
				<updated>2012-11-29T06:08:33Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial on &amp;#039;…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello friends and welcome to the tutorial on 'Sets'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Create sets from lists.&lt;br /&gt;
# Perform union, intersection and symmetric difference operations.&lt;br /&gt;
# Check if a set is a subset of other.&lt;br /&gt;
# Understand various similarities with lists like length and containership.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Getting started with Lists&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  ipython&lt;br /&gt;
| Now, What are sets? Sets are data structures which contain unique elements. In other words, duplicates are not allowed in sets.&lt;br /&gt;
&lt;br /&gt;
First let us invoke our ipython interpreter&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;a_list = [1, 2, 1, 4, 5, 6, 2]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 a = set(a_list)&lt;br /&gt;
 a&lt;br /&gt;
| Lets look at how to input sets. type&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;f10 = set([1, 2, 3, 5, 8])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;p10 = set([2, 3, 5, 7])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| We can see that duplicates are removed and the set contains only unique elements. Now let us perform some operations on sets. For this, we shall first create a pair of sets&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f10 | p10&lt;br /&gt;
| f10 is the set of fibonacci numbers from 1 to 10. p10 is the set of prime numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
Various operations can be performed on sets. For example, The | (pipe) character stands for union&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f10 &amp;amp; p10&lt;br /&gt;
| It gave the union of f10 and p10&lt;br /&gt;
&lt;br /&gt;
The '&amp;amp;' character stands for intersection.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f10 - p10&lt;br /&gt;
| It gave the intersection of f10 and p10&lt;br /&gt;
&lt;br /&gt;
similarly,f10 - p10 gives all the elements that are in f10 but not in p10&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f10 ^ p10&lt;br /&gt;
| and f10 ^ p10 gives all the elements in f10 union p10 but not in f10 intersection p10.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;b = set([1, 2])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;b &amp;lt; f10&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| In mathematical terms, it gives the symmetric difference.&lt;br /&gt;
&lt;br /&gt;
Sets also support checking of subsets.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;f10 &amp;lt; f10&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| It gives a &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; since b is a proper subset of f10. Similarly,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;f10 &amp;lt;= f10&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| It gives a &amp;lt;tt&amp;gt;False&amp;lt;/tt&amp;gt; since f10 is not a proper subset. hence the right way to do would be&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  for i in f10:&lt;br /&gt;
     print i,&lt;br /&gt;
| we get a &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; since every set is a subset of itself.&lt;br /&gt;
&lt;br /&gt;
Sets can be iterated upon just like lists and tuples.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  len(f10)&lt;br /&gt;
| It prints the elements of f10.&lt;br /&gt;
&lt;br /&gt;
The length and containership check on sets is similar as in lists and tuples.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  1 in f10&lt;br /&gt;
 2 in f10&lt;br /&gt;
| It shows 5. And&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| prints &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;False&amp;lt;/tt&amp;gt; respectively&lt;br /&gt;
&lt;br /&gt;
The order in which elements are organized in a set is not to be relied upon, since sets do not support indexing. Hence, slicing and striding are not valid on sets.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| '''&amp;lt;nowiki&amp;gt;Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23]&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
list all the duplicates&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
| Duplicates marks are the marks left out when we remove each element of the list exactly one time.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;marks = [20, 23, 22, 23, 20, 21, 23]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 marks_set = set(marks)&lt;br /&gt;
 for mark in marks_set:&lt;br /&gt;
     marks.remove(mark)&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  duplicates = set(marks)&lt;br /&gt;
 duplicates&lt;br /&gt;
| We are now left with only duplicates in the list &amp;lt;tt&amp;gt;marks&amp;lt;/tt&amp;gt; Hence,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| We obtained our required solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of the tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Make sets from lists.&lt;br /&gt;
# Perform union, intersection and symmetric difference operations. by using the operators ''|'', ''&amp;amp;'' and ''^'' respectively.&lt;br /&gt;
# Check if a set is a subset of other using the ''&amp;lt;nowiki&amp;gt;&amp;lt;&amp;lt;/nowiki&amp;gt;'' and ''&amp;lt;nowiki&amp;gt;&amp;lt;=&amp;lt;/nowiki&amp;gt;'' operators.&lt;br /&gt;
# Understand various similarities with lists like length and containership.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# If &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;a = [1, 1, 2, 3, 3, 5, 5, 8]&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;. What is set(a)&lt;br /&gt;
** &amp;lt;nowiki&amp;gt;set([1, 1, 2, 3, 3, 5, 5, 8])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
** &amp;lt;nowiki&amp;gt;set([1, 2, 3, 5, 8])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
** &amp;lt;nowiki&amp;gt;set([1, 2, 3, 3, 5, 5])&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
** Error&lt;br /&gt;
# &amp;lt;tt&amp;gt;'''&amp;lt;nowiki&amp;gt;odd = set([1, 3, 5, 7, 9])&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;squares = set([1, 4, 9, 16])&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;.'''&amp;lt;br/&amp;gt; How do you find the symmetric difference of these two sets?&lt;br /&gt;
# &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; is a set. how do you check if a variable &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt; exists in &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt;?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# set(a) will have all the common elements in the list &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt;, that is &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;set([1, 2, 3, 5, 8])&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# To find the symmetric difference between two sets, we use the operator ''^''.&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 odd ^ squares&lt;br /&gt;
&lt;br /&gt;
3. To check the containership, we say,&lt;br /&gt;
&lt;br /&gt;
 b in a&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>