Is the following Python code valid?
>>> a=(1,2,3) >>> b=a.update(4,)
Yes, a=(1,2,3,4) and b=(1,2,3,4)
Yes, a=(1,2,3) and b=(1,2,3,4)
No because tuples are immutable
No because wrong syntax for update() method
What will be the output of the following Python code?
>>> a=[(2,4),(1,2),(3,9)] >>> a.sort() >>> a
[(1, 2), (2, 4), (3, 9)]
[(2,4),(1,2),(3,9)]
Error because tuples are immutable
Error, tuple has no sort attribute
Mutable data type
Allows duplicate values
Data type with unordered values
Immutable data type
nums = set([1,1,2,3,3,3,4,4])
print(len(nums))
7
Error, invalid syntax for formation of set
4
8
a = [5,5,6,7,7,7]
b = set(a) def test(lst): if lst in b: return 1 else: return 0 for i in filter(test, a):
print(i,end=" ")
5 5 6
5 6 7
5 5 6 7 7 7
5 6 7 7 7
{ }
set()
[ ]
( )
>>> a={5,4} >>> b={1,2,4,5} >>> a<b
{1,2}
True
False
Invalid operation
print(len(a))
print(min(a))
a.remove(5)
a[2]=45
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
>>> 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
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
>>> 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
Error, invalid syntax for remove
>>> b=a.copy() >>> b.add(4)
Error, invalid syntax for add
{1,2,3,4}