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
What will be the output of the following Python code?
>>> a={4,5,6} >>> b={2,8,6} >>> a+b
{4,5,6,2,8}
{4,5,6,2,8,6}
Error as unsupported operand type for sets
Error as the duplicate item 6 is present in both sets
>>> a={4,5,6} >>> b={2,8,6} >>> a-b
{4,5}
{6}
Error as unsupported operand type for set data type
>>> a={5,6,7,8} >>> b={7,8,10,11} >>> a^b
{5,6,7,8,10,11}
{7,8}
Error as unsupported operand type of set data type
{5,6,10,11}
>>> s={5,6} >>> s*3
{5,6,5,6,5,6}
{5,6}
Error as multiplication creates duplicate elements which isn’t allowed
Is the following Python code valid?
a={3,4,{7,5}} print(a[2][0])
Yes, 7 is printed
Error, elements of a set can’t be printed
Error, subsets aren’t allowed
Yes, {7,5} is printed
Mutable data type
Allows duplicate values
Data type with unordered values
Immutable data type
>>> a=frozenset([5,6,7])
>>> a
>>> a.add(5)
Yes, now is a {5,5,6,7}
No, frozen set is immutable
No, invalid syntax for add method
Yes, now a is {5,6,7}
>>> a={3,4,5}
>>> a.update([1,2,3])
Error, no method called update for set data type
{1, 2, 3, 4, 5}
Error, list can’t be added to set
Error, duplicate item present in list
>>> a={1,2,3}
>>> a.intersection_update({2,3,4,5})
{2,3}
Error, no method called intersection_update for set data type
{1,4,5}
>>> b=a >>> b.remove(3)
{1,2,3}
Error, copying of sets isn’t allowed
{1,2}
Error, invalid syntax for remove
>>> b=a.copy() >>> b.add(4)
Error, invalid syntax for add
{1,2,3,4}
>>> b=a.add(4)
>>> b
None
Nothing is printed
>>> b=frozenset([3,4,5])
>>> a-b
Error as difference between a set and frozenset can’t be found out
frozenset({1,2})
>>> a={5,6,7} >>> sum(a,5)
5
23
18
Invalid syntax for sum method, too many arguments
>>> {x*2 for x in a|{4,5}}
{2,4,6}
Error, set comprehensions aren’t allowed
{8, 2, 10, 4, 6}
{8,10}
>>> a={5,6,7,8}
>>> b={7,8,9,10}
>>> len(a+b)
8
Error, unsupported operand ‘+’ for sets
6
Nothing is displayed
a={1,2,3}
b={1,2,3,4}
c=a.issuperset(b)
print(c)
False
True
Syntax error for issuperset() method
Error, no method called issuperset() exists
s=set()
type(s)
<’set’>
<class ‘set’>
set
class set
The following Python code results in an error.
s={2, 3, 4, [5, 6]}
TRUE
FALSE