Sets in Pyhton
In python, a set is another type of collection data type. A Set is a mutable and an unordered collection of elements without duplicates. That means the elements within a set cannot be repeated. This feature used to include membership testing and eliminating duplicate elements.
Creating a Set
A set is created by placing all the elements separated by comma within a pair of curly brackets. The set( ) function can also used to create sets in Python.
Syntax:
Set_Variable = {E1, E2, E3 …….. En}
Example
>>> S1={1,2,3,'A',3.14}
>>> print(S1)
{1, 2, 3, 3.14, 'A'}
>>> S2={1,2,2,'A',3.14}
>>> print(S2)
{1, 2, 'A', 3.14}
In the above examples, the set S1 is created with different types of elements without duplicate values. Whereas in the set S2 is created with duplicate values, but python accepts only one element among the duplications. Which means python removed the duplicate value, because a set in python cannot have duplicate elements.
Creating Set using List or Tuple
A list or Tuple can be converted as set by using set( ) function. This is very simple procedure. First you have to create a list or Tuple then, substitute its variable within set( ) function as argument.
Example
MyList=[2,4,6,8,10]
MySet=set(MyList)
print(MySet)
Output:
{2, 4, 6, 8, 10}
Set Operations
As you learnt in mathematics, the python is also supports the set operations such as Union, Intersection, difference and Symmetric difference.
Union: It includes all elements from two or more sets
Positive Numbers: (5, 6, 8, 3, 1)
In python, the operator | is used to union of two sets. The function union( ) is also used to join two sets in python.
Example: Program to Join (Union) two sets using union operator
set_A={2,4,6,8}
set_B={'A', 'B', 'C', 'D'}
U_set=set_A|set_B
print(U_set)
Output:
{2, 4, 6, 8, 'A', 'D', 'C', 'B'}
Example: Program to Join (Union) two sets using union function
set_A={2,4,6,8}
set_B={'A', 'B', 'C', 'D'}
set_U=set_A.
print(set_U)
Output:
{'D', 2, 4, 6, 8, 'B', 'C', 'A'}
(ii) Intersection: It includes the common elements in two sets
Th e operator & is used to intersect two sets in python. Th e function intersection( ) is also used to intersect two sets in python.
Example: Program to insect two sets using intersection operator
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A & set_B)
Output:
{'A', 'D'}
Example: Program to insect two sets using intersection function
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A.intersection(set_B))
Output:
{'A', 'D'}
(iii) Difference : It includes all elements that are in fi rst set (say set A) but not in the second set (say set B)
Th e minus (-) operator is used to diff erence set operation in python. Th e function difference( ) is also used to diff erence operation.
Example: Program to difference of two sets using minus operator
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A - set_B)
Output:
{2, 4}
Example: Program to difference of two sets using difference function
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A.diff erence(set_B))
Output:
{2, 4}
(iv) Symmetric difference : It includes all the elements that are in two sets (say sets A and B) but not the one that are common to two sets.
The caret (^) operator is used to symmetric difference set operation in python. The function symmetric_difference( ) is also used to do the same operation.
Example: Program to symmetric difference of two sets using caret operator
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A ^ set_B)
Output:
{2, 4, 'B', 'C'}
Example: Program to difference of two sets using symmetric difference function
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A.symmetric_difference(set_B))
Output:
{2, 4, 'B', 'C'}
Qus. 1 : <p>What will be the output of the following Python code?</p><pre>d = {0, 1, 2}<br>for x in d.values():<br> print(x)</pre><div><br></div>
- 0 1 2
- None None None
- error
- none of the mentioned
Qus. 2 : <p>What will be the output of the following Python code?</p><pre>d = {0, 1, 2}<br>for x in d:<br> print(x)</pre><div><br></div>
- 0 1 2
- {0, 1, 2} {0, 1, 2} {0, 1, 2}
- error
- none of the mentioned
Qus. 3 : <p>What will be the output of the following Python code?</p><pre>d = {0, 1, 2}<br>for x in d:<br> print(d.add(x))</pre>
- 0 1 2
- 0 1 2 0 1 2 0 1 2 …
- None None None
- None of the mentioned
Qus. 4 : Which of these about a set is not true?
- Mutable data type
- Allows duplicate values
- Data type with unordered values
- Immutable data type
Qus. 5 : Which of the following statements is used to create an empty set?
- { }
- set()
- [ ]
- ( )
Qus. 6 : If a={5,6,7,8}, which of the following statements is false?
- print(len(a))
- print(min(a))
- a.remove(5)
- a[2]=45
Qus. 7 : If a={5,6,7}, what happens when a.add(5) is executed?
- a={5,5,6,7}
- a={5,6,7}
- Error as there is no add function for set data type
- Error as 5 already exists in the set
Qus. 8 :
What will be the output of the following Python code?
>>> a={4,5,6} >>> b={2,8,6} >>> a-b
- {4,5}
- {6}
- Error as unsupported operand type for set data type
- Error as the duplicate item 6 is present in both sets
Qus. 9 : <p>Is the following Python code valid?</p><pre><span style="font-size: 14px;">a={3,4,{7,5}}<br></span><span style="font-size: 14px;">print(a[2][0])</span></pre>
- Yes, 7 is printed
- Error, elements of a set can’t be printed
- Error, subsets aren’t allowed
- Yes, {7,5} is printed
Qus. 10 : Which of these about a frozenset is not true?
- Mutable data type
- Allows duplicate values
- Data type with unordered values
- Immutable data type
Qus. 11 : <p>What will be the output of the following Python code?</p> <pre><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">>>></span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"=""> a</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">=</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">{</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">5</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">6</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">7</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">}<br></span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">>>></span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"=""> </span><span class="kw2" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" green;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">sum</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">(</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">a</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">5</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">)</span></pre>
- 5
- 23
- 18
- Invalid syntax for sum method, too many arguments
Qus. 12 : <p>What will be the output of the following Python code?</p><pre><span style="font-size: 14px;">a={5,6,7,8}<br></span><span style="font-size: 14px;">b={7,8,9,10} <br></span><span style="font-size: 14px;">print(len(a+b))</span></pre>
- 8
- Error, unsupported operand ‘+’ for sets
- 6
- Nothing is displayed
Qus. 13 : <p>What will the output of following code?</p><pre><span style="font-size: 14px;">a={1,2,3}<br></span><span style="font-size: 14px;">b={1,2,3,4}<br></span><span style="font-size: 14px;">c=a.issuperset(b)<br></span><span style="font-size: 14px;">print(c)</span></pre><div><br></div>
- False
- True
- Syntax error for issuperset() method
- Error, no method called issuperset() exists
Qus. 14 : Set makes use of __________ Dictionary makes use of ____________
- keys, keys
- key values, keys
- keys, key values
- key values, key values
Qus. 15 : Which of the following lines of code will result in an error?
- s={abs}
- s={4, ‘abc’, (1,2)}
- s={2, 2.2, 3, ‘xyz’}
- s={san}
Qus. 16 : Which of the following functions cannot be used on heterogeneous sets?
- pop
- remove
- update
- sum
Qus. 17 : Which of the following functions will return the symmetric difference between two sets, x and y?
- x | y
- x ^ y
- x & y
- x – y
Qus. 18 : The ____________ function removes the first element of a set and the last element of a list.
- remove
- pop
- discard
- dispose
Qus. 19 : If we have two sets, s1 and s2, and we want to check if all the elements of s1 are present in s2 or not, we can use the function:
- s2.issubset(s1)
- s2.issuperset(s1)
- s1.issuperset(s2)
- s1.isset(s2)
Qus. 20 : <p>What is the output of the following code?</p><pre>a = set('dcma')<br>b = set('mlpc')<br>print(a^b)</pre><div><br></div>
- {‘d’, ‘c’, ‘m’, ‘l’, ‘p’, ‘c’}
- {‘m’, ‘l’, ‘p’, ‘c’}
- {‘d’, ‘c’, ‘m’, ‘a’}
- None of These
Qus. 21 : In which of the following data type, duplicate items are not allowed ?
- List
- Dictionary
- Set
- None of the Above
Qus. 22 : Which one the following is a mutable data type ?
- set
- tuple
- string
- None of These
Qus. 23 : Which of the following is not a valid set operation in python?
- Union
- Intersection
- Difference
- None of the above
Programs
python program to find the 2nd largest number from the list of the numbers entered through keyboard.
View Solution
python program that creates a list of numbers from 1 to 20 that are divisible by 4
View Solution
python program to define a list of countries that are a member of BRICS Check whether a county is member of BRICS or not
View Solution
Python program to read marks of six subjects and to print the marks scored in each subject and show the total marks
View Solution
Python program to read prices of 5 items in a list and then display sum of all the prices product of all the prices and find the average
View Solution
Python program to count the number of employees earning more than 1 lakh per annum
View Solution
python program to create a list of numbers in the range 1 to 10 Then delete all the even numbers from the list and print the final list
View Solution
python program to generate in the Fibonacci series and store it in a list Then find the sum of all values
View Solution
python program to swap two values using tuple assignment
View Solution
python program using a function that returns the area and circumference of a circle whose radius is passed as an argument
View Solution
python program that has a list of positive and negative numbers Create a new tuple that has only positive numbers from the list
View Solution
Python Program that generate a set of prime numbers and another set of even numbers
View Solution
python program to find minimum element from a list of elements along with its index in the list
View Solution
Python program to calculate mean of a given list of numbers
View Solution
Python program to search for an element in a given list of numbers
View Solution
python program to count frequency of a given element in a list of numbers
View Solution
python program to calculate the sum of integers of the list
View Solution
python program to creates a third list after adding two lists
View Solution
python program to store the product information in dictionary
View Solution
python program to create a dictionary whose keys are month names and values are their corresponding number of days
View Solution
python program that input a list, replace it twice and then prints the sorted list in ascending and descending order
View Solution
python program to check if the maximum element of the list lies in the first half of the list or in the second half
View Solution
python program to display the maximum elements from the two list along with its index in its list
View Solution
python program to swap list elements with even and locations
View Solution
python program to rotate list elements with next elements
View Solution
python program to find maximum and minimum values in tuple
View Solution
python program to interchange the values of two tuple
View Solution
python program to interchange the values of two tuple
View Solution
python program to enter a list containing number between 1 and 12 then replace all the entries in the list that are greater than 10 with 10
View Solution
python program that reverse the list of integers in place
View Solution
python program to input two list and create third list which contains all elements of the first elements followed by all elements of second list
View Solution
python program to inter list of string and create a new list that consist of those strings with their first characters removed
View Solution
python program to create a list using for loop which consist of integers 0 through 49
View Solution
python program to create a list using for loop which consist square of the integers 1 through 50
View Solution
python program to create a list using for loop which consist a bb ccc dddd that ends with 26 copies of the letter z.
View Solution
python program that takes any two list L and M of the same size and adds their elements together in another third list
View Solution
python program to rotates the elements of list so that fist elements move to second and second to the third
View Solution
python program to move all duplicate element to the end of list
View Solution
python program to display the maximum and minimum values form specified range of indexes of a list
View Solution
python program to compare two equal sized list and print the first index where they differ
View Solution
python program to enter names of employees and their salaries as input and store them in a dictionary
View Solution
python program to count the number of times a character appears in a given string
View Solution
python program to convert a number entered by the user into its corresponding number in words
View Solution
python program Repeatedly ask the user to enter a team name
View Solution
python program that repeatedly asks the user to enter product names and prices
View Solution
Python Program to Create a dictionary whose keys are month name and whose values are number of days in the corresponding month
View Solution
python program to store the detail of 10 students in a dictionary at the same time
View Solution
python program to Create a dictionary with the opposite mapping
View Solution
python program that lists the over lapping keys of the two dictionaries if a key of d1 is also a key of d2 the list it
View Solution
python program that checks if two same values in a dictionary have different keys
View Solution
python program to check if a dictionary is contained in another dictionary
View Solution
python program to create a new dictionary D2 having same keys as D1 but values as the sum of the list elements
View Solution
python program to create a dictionary has three keys assets liabilities and capital
View Solution
python program that create a tuple storing first 9 terms of Fibonacci series
View Solution
python program that receives the index and return the corresponding value
View Solution
Python program that receives a Fibonacci series term and returns a number telling which term it is
View Solution
python program to create a nested tuple to store roll number name and marks of students
View Solution
python program that interactively creates a nested tuple to store the marks in three subjects for five students
View Solution
Python Program to accept three distinct digits and print all possible combinations from the digits
View Solution
Python Program to find the successor and predecessor of the largest element in list
View Solution
Python function that takes a string as parameter and returns a string with every successive repetitive character replaced
View Solution