Sets in Pyhton

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) DifferenceIt 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>&nbsp; &nbsp; print(x)</pre><div><br></div>

  1. 0 1 2
  2. None None None
  3. error
  4. 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>&nbsp; &nbsp; print(x)</pre><div><br></div>

  1. 0 1 2
  2. {0, 1, 2} {0, 1, 2} {0, 1, 2}
  3. error
  4. 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>&nbsp; &nbsp; print(d.add(x))</pre>

  1. 0 1 2
  2. 0 1 2 0 1 2 0 1 2 …
  3. None None None
  4. None of the mentioned
Qus. 4 : Which of these about a set is not true?

  1. Mutable data type
  2. Allows duplicate values
  3. Data type with unordered values
  4. Immutable data type
Qus. 5 : Which of the following statements is used to create an empty set?

  1. { }
  2. set()
  3. [ ]
  4. ( )
Qus. 6 : If a={5,6,7,8}, which of the following statements is false?

  1. print(len(a))
  2. print(min(a))
  3. a.remove(5)
  4. a[2]=45
Qus. 7 : If a={5,6,7}, what happens when a.add(5) is executed?

  1. a={5,5,6,7}
  2. a={5,6,7}
  3. Error as there is no add function for set data type
  4. 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


  1. {4,5}
  2. {6}
  3. Error as unsupported operand type for set data type
  4. 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>

  1. Yes, 7 is printed
  2. Error, elements of a set can’t be printed
  3. Error, subsets aren’t allowed
  4. Yes, {7,5} is printed
Qus. 10 : Which of these about a frozenset is not true?

  1. Mutable data type
  2. Allows duplicate values
  3. Data type with unordered values
  4. 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;"="">&gt;&gt;&gt;</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;"="">&gt;&gt;&gt;</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>

  1. 5
  2. 23
  3. 18
  4. 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}&nbsp;<br></span><span style="font-size: 14px;">print(len(a+b))</span></pre>

  1. 8
  2. Error, unsupported operand ‘+’ for sets
  3. 6
  4. 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>

  1. False
  2. True
  3. Syntax error for issuperset() method
  4. Error, no method called issuperset() exists
Qus. 14 : Set makes use of __________ Dictionary makes use of ____________

  1. keys, keys
  2. key values, keys
  3. keys, key values
  4. key values, key values
Qus. 15 : Which of the following lines of code will result in an error?

  1. s={abs}
  2. s={4, ‘abc’, (1,2)}
  3. s={2, 2.2, 3, ‘xyz’}
  4. s={san}
Qus. 16 : Which of the following functions cannot be used on heterogeneous sets?

  1. pop
  2. remove
  3. update
  4. sum
Qus. 17 : Which of the following functions will return the symmetric difference between two sets, x and y?

  1. x | y
  2. x ^ y
  3. x & y
  4. x – y
Qus. 18 : The ____________ function removes the first element of a set and the last element of a list.

  1. remove
  2. pop
  3. discard
  4. 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:

  1. s2.issubset(s1)
  2. s2.issuperset(s1)
  3. s1.issuperset(s2)
  4. 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>

  1. {‘d’, ‘c’, ‘m’, ‘l’, ‘p’, ‘c’}
  2. {‘m’, ‘l’, ‘p’, ‘c’}
  3. {‘d’, ‘c’, ‘m’, ‘a’}
  4. None of These
Qus. 21 : In which of the following data type, duplicate items are not allowed ?

  1. List
  2. Dictionary
  3. Set
  4. None of the Above
Qus. 22 : Which one the following is a mutable data type ?

  1. set
  2. tuple
  3. string
  4. None of These
Qus. 23 : Which of the following is not a valid set operation in python?

  1. Union
  2. Intersection
  3. Difference
  4. 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 reverses an array of integers

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 increment the elements of list

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


Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad