What will be the output of the following Python code?
>>> a=(1,2,3,4) >>> del(a[2])
Now, a=(1,2,4)
Now, a=(1,3,4)
Now a=(3,4)
Error as tuple is immutable
>>> a=(2,3,4) >>> sum(a,3)
Too many arguments for sum() method
The method sum() doesn’t exist for tuples
12
9
Is the following Python code valid?
>>> a=(1,2,3,4) >>> del a
No because tuple is immutable
Yes, first element in the tuple is deleted
Yes, the entire tuple is deleted
No, invalid syntax for del method
Array of tuples
List of tuples
Tuples of lists
Invalid type
>>> a=(0,1,2,3,4) >>> b=slice(0,2) >>> a[b]
Invalid syntax for slicing
[0,2]
(0,1)
(0,2)
>>> a,b,c=1,2,3 >>> a,b,c
Yes, [1,2,3] is printed
No, invalid syntax
Yes, (1,2,3) is printed
1 is printed
>>> a,b=1,2,3
Yes, this is an example of tuple unpacking. a=1 and b=2
Yes, this is an example of tuple unpacking. a=(1,2) and b=3
No, too many values to unpack
Yes, this is an example of tuple unpacking. a=1 and b=(2,3)
>>> a=(1,2) >>> b=(3,4) >>> c=a+b >>> c
(4,6)
(1,2,3,4)
Error as tuples are immutable
None
>>> a,b=6,7 >>> a,b=b,a >>> a,b
(6,7)
Invalid syntax
(7,6)
Nothing is printed
>>> import collections >>> a=collections.namedtuple('a',['i','j']) >>> obj=a(i=4,j=7) >>> obj
a(i=4, j=7)
obj(i=4, j=7)
(4,7)
An exception is thrown
>>> a=2,3,4,5 >>> a
Yes, 2 is printed
Yes, [2,3,4,5] is printed
Yes, (2,3,4,5) is printed
>>> a=(2,3,1,5)
>>> a.sort()
>>> a
(1,2,3,5)
(2,3,1,5)
Error, tuple has no attribute sort
>>> 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
>>> 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