In this section, we are going to talk about Frozenset and Set in Python With Example. We will go through different concepts such as how to create set, how to add element in set, how to update set, how to print set, different operations in set, different operators and built-in methods in set, what is frozenset, add or update or delete element in frozenset in python etc. Finally, we will go through different multiple choice questions of frozenset and set in python.
Get our other posts on python for free of cost.
Getting Started
A set is data structure that contains unordered, unique and immutable collection of items.
In simpler words,
Sets are containers used to store multiple items in a single variable in Python. They represent another type of built-in sequential object apart from List, Tuple and Dictionary. They have the particularity of being unchangeable, unordered and unique items (ie. does not allow duplicated items).
In other way,
After set definition, it is impossible to modify any of its items, impossible to retrieve an item by its position and each item can only be present once.
Let’s see how to create set in python with example.
Create Set in Python
First to make an operation in set we have to define it. So set in python can be created by several means –
- Using python built-in method – set()
- Using Comma separated list of elements and braces
- Using set comprehension
-
Using Python built-in method – set()
We can use built-in method – set() to generate a new set in python as shown below –
set('content') set(['a', 'b', 'foo']) set([1, 2, 3, 4, 5]) set({'key1' : 1, 'key2' : 2})
set(‘content’) generates a set that contains elements ‘c’, ‘o’, ‘n’, ‘t’, ‘e’.
Similarly,
set([‘a’, ‘b’, ‘foo’]) generates a set that contains elements – ‘a’, ‘b’ and ‘foo’.
set([1, 2, 3, 4, 5]) generates a set that contains elements – 1, 2, 3, 4 and 5
set({‘key1’ : 1, ‘key2’ : 2}) generates a set that contains elements – ‘key1’ and ‘key2’.Example,
set1 = set('content') set2 = set(['a', 'b', 'foo']) set3 = set([1, 2, 3, 4, 5]) set4 = set({'key1' : 1, 'key2' : 2}) print(set1) print(set2) print(set3) print(set4)
Output:
{'c', 'e', 'o', 't', 'n'} {'a', 'foo', 'b'} {1, 2, 3, 4, 5} {'key1', 'key2'}
-
Using Comma separated list of elements and braces
We can also create set using a comma-separated list of elements within braces. For example,
set1 = {'john', 'jack'} set2 = {'key1' : 1, 'key2' : 2} set3 = {1, 2, 3, 4, 5} print(set1) print(set2) print(set3)
Here,
set1, set2 and set3 are sets created using curly braces and comma.When above program is run, output is –
{'jack', 'john'} {'key1': 1, 'key2': 2} {1, 2, 3, 4, 5}
-
Using set comprehension
Set in python can also be created using set comprehension. Set comprehension is a method for creating set in python using elements from other iterables like list, tuples etc.
Syntax for Set Comprehension in Python
newSet = { expression for element in iterable }
Here,
newSet is newly created set by set comprehension.
iterable is iterable object or data structure from which elements are used to create set.
expression any mathematical expression derived from the element.For example,
newSet = {item for item in 'India country'} print(newSet)
Here,
newSet is newly created set using elements from India country.When you run above program, output is –
{'a', 'I', 'd', 'n', 'c', 'u', 'i', 'o', 'y', 't', 'r', ' '}
Conditional Set Comprehension in Python
We can also apply some condition while creating set using set comprehension in python.
Syntax for conditional set comprehension –
newSet= { expression for element in iterable if condition }
Here,
newSet is newly created set by set comprehension.
iterable is iterable object or data structure from which elements are used to create set.
expression any mathematical expression derived from the element.
condition is conditional statement which we apply while creating set using element.For example,
newSet = {item for item in 'India country' if item not in 'abc'} print(newSet)
Here,
We are creating set using elements from India country only if element is not present in abc.When you run program, output is –
{'d', 'u', 't', 'n', 'y', ' ', 'I', 'i', 'r', 'o'}
A set cannot have mutable elements like lists, sets or dictionary as its elements. For example,
# A set can not have mutable items. # Here, [30, 40] is mutable list. #It will throw error. newSet = {10, 20, [30, 40]} print(newSet)Output:
TypeError: unhashable type: 'list'Now, let’s see how to create empty set in python with example.
How to create am empty set in Python ?
In python, creating creating an empty set is a bit tricky because if you are going to use empty curly braces {}, it will create an empty dictionary in python.
To make an empty set in python, we can use set() method without any arguments. For example,
x = set()Here,
x is an empty set.Now, Guess the output of below program,
x = {} print(type(x)) x = set() print(type(x))Output:
<class 'dict'> <class 'set'>
Now, let’s see how to print elements of set in python with example.
Print Element of Set in Python
For displaying elements of set when set is created, we make an iteration on set by using for loop and print() function.
color = set( ['yellow', 'white', 'black', 'green', 'red' ]) for element in color : print(element)
Output :
black green red yellow white
Note : This output is unordered because we say above that set object ignore the order of its items.
Now, let’s see how to add element to set in python with example.
Add New Element to Set in Python
When a set is created, we cannot modify any of its items but we can add another item to a set. Then to add another item to a set we use the add() method.
For example,
color = set(['yellow', 'white', 'black', 'green', 'red' ]) color.add('blue') print (color)
Here, we added new element blue to set color.
Output :
{'blue', 'yellow', 'white', 'red', 'green', 'black'}
We can also add tuple to set using add() method as shown below –
color = set(['yellow', 'white', 'black', 'green', 'red' ]) mytuple = ('cyan', 'orange') color.add(mytuple) print(color)
Output :
{'yellow', 'white', ('cyan', 'orange'), 'red', 'green', 'black'}
We can also add elements from list using for loop and add() method as shown below –
color = set( ['yellow', 'white', 'black', 'green', 'red' ]) for element in ['blue', 'purple', 'cyan'] : color.add( element ) print( color )
Output :
{'cyan', 'blue', 'yellow', 'white', 'purple', 'red', 'green', 'black'}
Note : add() method can only add one item to set object. The only way to add multiple item to set by using add() method is loops. Tuple element can be add to set contrary to List and Dictionary which are not hashable.
On other hand to add two or more elements to set object we use update() method.
Now, let’s see how to update elements of set in python with example.
Update Elements of Set in Python
update() method can add string, set, list, tuple and dictionary to set.
color = set( ['yellow', 'white', 'black', 'green', 'red' ]) list1 = ['cyan', 'orange', 'purple' ] color.update(list1) print(color)
Output :
{'cyan', 'yellow', 'white', 'orange', 'purple', 'red', 'green', 'black'}
Let’s see another example,
day = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) week = { 'saturday', 'sunday' } day.update(week) print(day)
Output :
{'sunday', 'friday', 'saturday', 'tuesday', 'monday', 'thursday', 'wednesday'}
Let’s see another example,
number1 = set( [1, 2, 3, 4, 5, 6, 7 ]) number2 = { 8 : 'eight', 9 : 'nine', 10 : 'ten' } number1.update(number2) print(number1)
Output :
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Now, let’s see how to remove elements of set in python with example.
Remove Element from Set in Python
Python Set elements can be removed by using below methods –
-
Using remove() Method to Remove Element
remove() element can be used to remove from set in python as below –
day = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) day.remove('monday') print(day)
Output:
{'friday', 'tuesday', 'thursday', 'wednesday'}
-
Using discard() Method to Remove Element
Let’s see another example using discard() method –
day = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) day.discard('wednesday') print(day)
Output:
{'friday', 'tuesday', 'monday', 'thursday'}
Note : if removed element doesn’t exist, remove() method raise a KeyError contrary to discard() method which make set unchanged.
-
Using pop() Method to Remove Element
Let’s see another example using pop() method –
day = set(['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) day.pop() print(day)
Output:
{'tuesday', 'wednesday', 'thursday', 'monday'}
Note : pop() method remove the last element from the set and return it. The last element can be any element of the set because set is unordered.
What if we need to clear all elements of set ?
Let’s see how clear elements of set in python with example.
How to Clear All Elements of Set ?
All elements of set can be removed by –
-
Using clear() Method
We can reset or clear all elements of set using clear() method as shown below –
day = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) day.clear() print(day)
Output :
set()
-
Using del Method
Sets can be removed completely by using del method as shown below –
day = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) del day print(day)
Output:
NameError : name 'day' is not defined
Note : The difference between clear() method and del method is that clear() method remove all the elements of sets whereas del method remove set completely
Now, let’s see different operations of set in python with example.
Different Python Set Operations
Several operations which can be performed between different sets in python are –
-
union() Operation in Python Set
union() operation joins all elements of first and second set and returns new set.
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) b = { 'saturday', 'sunday' } # Calculates a union b result1 = a.union(b) # Calculates b union a result2 = b.union(a) print(a) print(b) print(result1) print(result2)
Here, a.union(b) creates new set with elements present in a and b both.
Here, b.union(a) creates new set with elements present in b and a both.
So, result1 and result2 both contains set whose elements are present in both a and b.
Output:{'monday', 'friday', 'wednesday', 'tuesday', 'thursday'} {'saturday', 'sunday'} {'wednesday', 'saturday', 'monday', 'friday', 'tuesday', 'sunday', 'thursday'} {'saturday', 'wednesday', 'monday', 'friday', 'tuesday', 'sunday', 'thursday'}
Note : union() method works like update() method. They don’t allow duplicate element in the new set.
-
intersection() Operation in Python Set
Intersection of two sets is a new set whose elements are common in both sets.
So, intersection() method returns new set containing elements present in both sets.
For example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) b = { 'saturday', 'sunday' } # Calculates a intersection b result1 = a.intersection(b) # Calculates b intersection a result2 = b.intersection(a) print(a) print(b) print(result1) print(result2)
Output:
{'tuesday', 'monday', 'friday', 'thursday', 'wednesday'} {'saturday', 'sunday'} set() set()
Here, a.intersection(b) creates a new set whose elements are present in sets a and b both.
Since there are no element common in a and b, resultant value is empty set.
Let’s see another example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) b = { 'saturday', 'sunday', 'monday', 'wednesday' } # Calculates a intersection b result1 = a.intersection(b) # Calculates b intersection a result2 = b.intersection(a) print(a) print(b) print(result1) print(result2)
Output:
{'thursday', 'monday', 'friday', 'wednesday', 'tuesday'} {'sunday', 'saturday', 'monday', 'wednesday'} {'monday', 'wednesday'} {'monday', 'wednesday'}
Here, monday and wednesday are common elements in a and b. So, resultant set contains element monday and wednesday.
-
difference() Operation in Python Set
Difference of first Set (A) from second Set (B) is a new set whose elements are present in first set (A) but not in second Set (B).
Similarly,
Difference of second Set (B) from first Set (A) is a new set whose elements are present in second set (B) but not in first Set (A).
difference() method in python performs difference operation between two sets and return new set as a result.
For example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) b = { 'saturday', 'sunday', 'monday', 'wednesday' } # Calculates elements present in a but not in b. result1 = a.difference(b) # Calculates elements present in b but not in a. result2 = b.difference(a) print(a) print(b) print(result1) print(result2)
Output:
{'monday', 'tuesday', 'thursday', 'wednesday', 'friday'} {'monday', 'saturday', 'wednesday', 'sunday'} {'tuesday', 'friday', 'thursday'} {'saturday', 'sunday'}
-
symmetric_difference() Operation in Python Set
Symmetric difference of two sets (A and B) is set of elements which are either present in A or in B but not in both.
It means elements which are common to set A and B are not included.
symmetric_difference() in python perform symmetric operations between two sets and return new set containing elements either in A or B.
For example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) b = { 'saturday', 'sunday', 'monday', 'wednesday' } # Calculates all elements excluding common elements between a and b both. result1 = a.symmetric_difference(b) # Calculates all elements excluding common elements between a and b both. result2 = b.symmetric_difference(a) print(a) print(b) print(result1) print(result2)
Output:
{'tuesday', 'wednesday', 'friday', 'thursday', 'monday'} {'sunday', 'saturday', 'monday', 'wednesday'} {'tuesday', 'friday', 'thursday', 'saturday', 'sunday'} {'tuesday', 'friday', 'saturday', 'thursday', 'sunday'}
Now, let’s see different operators of set in python with example.
Different Operators of Set in Python
union() method, intersection() method, difference() method, symmetric_difference() method are also used like mathematical operators. They are represented by |, &, – and ^ respectively.
-
| Operator in Python Set
| Operator is same as union() method in python.
For example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) b = { 'saturday', 'sunday' } # Calculates a union b using union() method. result1 = a.union(b) # Calculates b union a using operator. result2 = a | b print(a) print(b) print(result1) print(result2)
{'tuesday', 'wednesday', 'monday', 'friday', 'thursday'} {'saturday', 'sunday'} {'saturday', 'monday', 'thursday', 'friday', 'tuesday', 'sunday', 'wednesday'} {'saturday', 'monday', 'thursday', 'friday', 'tuesday', 'sunday', 'wednesday'}
-
& Operator in Python Set
& Operator is same as intersection() method in python.
For example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) b = { 'saturday', 'sunday', 'monday', 'wednesday' } # Calculates a intersection b using intersection() method. result1 = a.intersection(b) # Calculates b intersection a using operator. result2 = a & b print(a) print(b) print(result1) print(result2)
Output:
{'tuesday', 'monday', 'friday', 'thursday', 'wednesday'} {'wednesday', 'sunday', 'monday', 'saturday'} {'wednesday', 'monday'} {'wednesday', 'monday'}
-
– Operator in Python Set
– Operator is same as difference() method in python.
For example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) b = { 'saturday', 'sunday', 'monday', 'wednesday' } # Calculates elements present in a but not in b using difference() method. result1 = a.difference(b) # Calculates elements present in b but not in a using operator. result2 = a - b print(a) print(b) print(result1) print(result2)
Output:
{'wednesday', 'tuesday', 'monday', 'friday', 'thursday'} {'saturday', 'wednesday', 'sunday', 'monday'} {'friday', 'tuesday', 'thursday'} {'friday', 'tuesday', 'thursday'}
-
^ Operator in Python Set
^ Operator is same as symmetric_difference() method in python.
For example,
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) b = { 'saturday', 'sunday', 'monday', 'wednesday' } # Calculates all elements excluding common elements between a and b both. result1 = a.symmetric_difference(b) # Calculates all elements excluding common elements between a and b both. result2 = a ^ b print(a) print(b) print(result1) print(result2)
Output:
{'friday', 'wednesday', 'tuesday', 'thursday', 'monday'} {'sunday', 'wednesday', 'saturday', 'monday'} {'sunday', 'friday', 'tuesday', 'saturday', 'thursday'} {'sunday', 'friday', 'tuesday', 'saturday', 'thursday'}
Now, let’s see different methods of set in python with example.
Different Methods of Set in Python
In this section, we will discuss about different methods of set in python such as :
Set Methods | Description |
add() |
|
update() | Adds all iterable to set. i.e. it adds string, set, list, tuple and dictionary to set. |
remove() | Removes a specific element from set. |
discard() | Removes a specific item or element from set. |
pop() | Removes the last element from set. The last element can be any item from set because set is unordered. |
clear() | Removes all element from set. |
union() | Returns a set that contains all different element figuring in the old sets. |
intersection() | Returns a set that contains only element both present in the two old set. |
copy() | Copy all element from one set to another set. |
difference() | Returns a set that contains the difference (element figure in specified set and not in another) between two or more sets. |
intersection_update() | Removes the elements in a set that are not present both in this set and another set. |
isdisjoint() | Returns boolean. Returns True if there not element both present in two sets. Returns False if there is at least one element both present in two sets. |
issubset() | Returns boolean. Returns True if all elements of one set are presents in another set. Returns False if all elements of one set are not presents in another set. |
difference_update() | Removes the elements that are both presents in two sets. |
issuperset() | Returns boolean. Returns True if another set contains all the elements of specified set. Returns False if another set doesn’t contain all the elements of specified set. |
symmetric_difference() | Returns a new set that contains only elements that are not presents in both sets. |
symmetric_difference_update() | Returns elements that are not presents in both sets. |
Length and Type of Set in Python
We use len() function and type() function to get the size of a set and type of set respectively.
-
len() Method in Python
len() in python returns number of elements present in set.
For example,
day = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) print(len(day))
Output:
5
-
type() Method in Python
type() in python returns data type of set.
For example,
day = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) print(type(day))
Output:
<class 'set'>
How to Check if Element Exists in Set Or Not ?
If we want to check whether an element exists in given set or not, we can do so using in keyword as shown below –
a = set( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) x = 'saturday' # Checks if x is present in a or not. If yes, returns True. otherwise, returns False. result1 = x in a # Checks if x is not present in a or not. If no, returns True, otherwise, returns False. result2 = x not in a print(a) print(x) print(result1) print(result2)
Output:
{'monday', 'friday', 'tuesday', 'thursday', 'wednesday'} saturday False True
Built-in Methods in Set
Here are some of the common built-in methods in set used for performing some tasks –
Methods | Description |
all() | Returns True if all items of set are True. Otherise, returns False. |
len() | Returns number of elements present in set. |
max() | Returns largest number in set. |
min() | Returns smallest number in set. |
sorted() | Returns a new list whose elements are taken from set and present in sorted order. |
sum() | Returns sum of all elements present in set. |
any() | Returns True if any element in True in set. |
enumerate() | Returns a enumerate object that contains element as index-value pair. |
Now, let’s see frozenset in python with example.
FrozenSet in Python
A frozenset is an immutable set. It cannot be modified after you create it.
In other words, elements of frozenset can not be changed once assigned.
Set is unhashable. So, it can n’t be used as dictionary.
But, frozenset is hashable. So, it can be used as dictionary.
Now, let’s see how to create frozenset in python with example.
Create frozenset in Python
To create a frozenset we use the built-in function called frozenset().
frozenset() can create hashable set with any iterable.
For example,
day = frozenset( ['monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) print(day)
Here, we have created frozenset in python whose elements are ‘monday’, ‘tuesday’, ‘wednesday’, ‘thursday’ and ‘friday’.
Output :
frozenset({'tuesday', 'wednesday', 'friday', 'thursday', 'monday'})
Let’s see another example,
day = frozenset( {1 : 'monday', 2 : 'tuesday', 3 : 'wednesday', 4 : 'thursday', 5 : 'friday' }) print(day)
Output:
frozenset({1, 2, 3, 4, 5})
Let’s see another example,
day = frozenset('monday') print(day)
Output:
frozenset({'m', 'y', 'o', 'a', 'd', 'n'})
Now, let’s see different operations of frozen set in python with example.
frozenset Methods for different Operations
Like set, frozenset have different methods to perform set operations –
- union() method
- intersection() method
- difference() method
- symmetric_difference() method
- isdisjoint() method
-
union() Method in frozenset
Like union() in set, union() in frozenset joins elements of two sets and returns new set containing those elements.
For example,
color = frozenset( ['yellow', 'white', 'black', 'green', 'red' ]) mytuple = frozenset(('cyan', 'orange')) result = color.union(mytuple) print(color) print(result)
Output:
frozenset({'red', 'green', 'yellow', 'white', 'black'}) frozenset({'orange', 'green', 'yellow', 'red', 'white', 'black', 'cyan'})
-
intersection Method in frozenset
Like intersectin() method in set, intersection() in frozenset returns new set whose elements are common in both sets.
For example,
color = frozenset( ['yellow', 'white', 'black', 'green', 'red' ]) mytuple = frozenset(('white', 'black')) result = color.intersection(mytuple) print(color) print(result)
Output:
frozenset({'green', 'white', 'yellow', 'black', 'red'}) frozenset({'white', 'black'})
-
difference Method in frozenset
difference() method in frozenset returns new set whose elements are present in first set but not in second set.
For example,
color = frozenset( ['yellow', 'white', 'black', 'green', 'red' ]) mytuple = frozenset(('white', 'black')) result = color.difference(mytuple) print(color) print(result)
Output:
frozenset({'yellow', 'black', 'white', 'red', 'green'}) frozenset({'green', 'yellow', 'red'})
-
symmetric_difference Method in frozenset
Like symmetic_difference in set, symmetic_difference() in frozenset returns new set whose elements are either present in first set or in second set but not in both set.
For example,
color = frozenset( ['yellow', 'white', 'black', 'green', 'red' ]) mytuple = frozenset(('white', 'black')) result = color.symmetric_difference(mytuple) print(color) print(result)
frozenset({'yellow', 'green', 'black', 'white', 'red'}) frozenset({'red', 'yellow', 'green'})
-
isdisjoint Method in frozenset
Returns true if there is no common elements in both set.
color = frozenset(['yellow', 'white', 'black', 'green', 'red' ]) mytuple = frozenset(('white', 'black')) result = color.isdisjoint(mytuple) print(color) print(result)
Output:
frozenset({'green', 'red', 'white', 'yellow', 'black'}) False
Now, let’s see different method of frozenset in python with example.
Python Frozenset Methods
Methods | Description |
union() | Returns a new set that contains distinct element figuring in two sets. |
difference() | Returns set that contains the difference between two or more sets. The difference is the items present in specified set and not in another. |
intersection() | Returns set that contains items both present in two sets. |
symmetric_difference() | Returns a new set that contains items not present in both sets. symmetric_difference() method oppose intersection() method |
isdisjoint() | Returns boolean. Returns True if there is not any element present in two sets. Returns False if there is at least one element both present in two sets. |
Note : Frozensets don’t have add() method, update() method, remove() method, discard() method, pop() method because these methods are used to modify set.
How to get frozenset length and type ?
Like set we use len() function to get length of frozenset and type() function for its type.
-
len() method of frozenset
len() method returns number of elements in frozenset.
For example,
day = frozenset({'monday', 'tuesday', 'wednesday'}) print(len(day))
Output:
3
-
type() method of frozenset
type() method returns data type of frozenset.
For example,
day = frozenset({'monday', 'tuesday', 'wednesday'}) print(type(day))
Output:
<class 'frozenset'>
Multiple Choice Questions On Set in Python
1. Guess the output of the program
course = set( ['biology', 'mathematic', 'chimic', '', 'history' ]) print(len(course))
a.) 5
b.) SyntaxError
c.) 10
d.) 3
2. Choose the correct answer.
course = set(['biology', 'mathematic', 'chimic', '', 'history' ]) print(course[2])
a.) mathematic
b.) chimic
c.) history
d.) TypeError, set object is not subscriptable
3. What is the right syntax for creating set ?
a.) set() b.) [ ] c.) () d.) { }
4. Guess the output of the program
a = {1, 2, 3, 4} b = {3, 4, 5, 6} print(a | b)
a.) {3, 4}
b.) {1, 2, 3, 4, 5, 6}
c.) set()
d.) {3, 4, 5, 6}
5. What of these statement(s) is/are wrong about set ?
a.) set allows duplicated items b.) set items are unordered c.) set is immutable
6. Guess the output of the program
set1 = {1, 2, 3, 4, 5} set1.add(5) print(set1)
a.) {1, 2, 3, 4, 5}
b.) set object has no attribute add.
c.) {1, 2, 3, 4, 5, 5}
d.) {1, 2, 3, 4, 5, 5, 5}
7. Guess the output of the program
set = frozenset({'monday', 'tuesday', 'wednesday'}) set.add('friday') print(set)
a.) {‘monday’,’tuesday’,’wednesday’,’friday’}
b.) frozenset object has no attribute add
c.) {‘monday’,’tuesday’,’wednesday’}
d.) {‘friday’}
8. Look for the correct output of this Python code.
set1 = {'monday', 'tuesday', 'wednesday'} set2 = {'monday', 'wednesday'} print(set1 ^ set2)
a.) {‘tuesday’}
b.) {‘monday’, ‘wednesday’}
c.) {‘monday’,’tuesday’}
d.) {‘monday’, ‘tuesday’, ‘wednesday’}
9. Choose the correct output of this Python code.
day1 = {'monday', 'tuesday', 'wednesday'} day2 = {'monday', 'wednesday', 'saturday', 'sunday' } print(day2 – day1)
a.) {‘tuesday’}
b.) {‘sunday’, ‘saturday’}
c.) {‘monday’,’wednesday’,‘tuesday’}
d.) {‘monday’, ‘wednesday’, ‘saturday’, ‘sunday’ }
10. Guess the output of the program
day1 = {'monday', 'tuesday', 'wednesday'} day2 = {'monday', 'wednesday', 'saturday', 'sunday' } print(day1 + day2)
a.) {‘monday’, ‘tuesday’, ‘wednesday’, ‘saturday’, ‘sunday’}
b.) unsupported operation type for sets.
11. Guess the output of the program
day1 = {'monday', 'tuesday', 'wednesday'} day2 = {'monday', 'wednesday', 'saturday', 'sunday' } print(day1 & day2)
a.) {‘monday’, ‘wednesday’}
b.) {‘wednesday’,’saturday’,’sunday’}
c.) {‘monday’, ‘tuesday’}
d.) {‘monday’, ‘wednesday’, ‘saturday’, ‘sunday’ }
12. Pick out the correct output based on this Python code.
day1 = {'monday', 'tuesday', 'wednesday'} day2 = {'monday', 'wednesday', 'saturday', 'sunday' } print(day1 == day2)
a.) {‘monday’, ‘wednesday’}
b.) True
c.) False
d.) {‘monday’, ‘wednesday’, ‘saturday’, ‘sunday’ }
13. Guess the output of the program
day1 = {'monday', 'tuesday', 'wednesday'} day2 = {'monday', 'wednesday', 'saturday', 'sunday' } print(day1.isdisjoint(day2))
a.) {‘monday’, ‘wednesday’}
b.) False
c.) True
d.) {‘monday’, ‘tuesday’, ‘wednesday’}
14. Choose the correct output.
day1 = {'monday', 'tuesday', 'wednesday'} print(day1.remove('saturday'))
a.) {‘monday’, ‘tuesday’, ‘wednesday’}
b.) KeyError: ‘saturday’
c.) {‘monday’}
d.) {‘monday’, ‘wednesday’}
15. Guess the output of the program.
day1 = {'monday', 'tuesday', 'wednesday'} print(day1.discard('saturday'))
a.) {‘monday’, ‘tuesday’, ‘wednesday’}
b.) ValueError
c.) {‘monday’, ‘wednesday’}
d.) {‘wednesday’}
That’s end of our post on Frozenset and set in python with example.
Source – Official documentation
You must be logged in to post a comment.