What will be the output of the following Python code?
>>>t = (1, 2)
>>>2 * t
(1, 2, 1, 2)
[1, 2, 1, 2]
(1, 1, 2, 2)
[1, 1, 2, 2]
>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2
True
False
Error
None
>>>my_tuple = (1, 2, 3, 4)
>>>my_tuple.append( (5, 6, 7) )
>>>print len(my_tuple)
1
2
5
numberGames = {}
numberGames[(1,2,4)] = 8
numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
sum = 0
for k in numberGames:
sum += numberGames[k]
print len(numberGames) + sum
30
24
33
12
Tuple
Integer
List
Both tuple and integer
Error, tuple slicing doesn’t exist
[2,3]
(2,3,4)
(2,3)
>>> a=(1,2,(4,5)) >>> b=(1,2,(3,4)) >>> a<b
Error, < operator is not valid for tuples
Error, < operator is valid for tuples but not if there are sub-tuples
>>> a=("Check")*3 >>> a
(‘Check’,’Check’,’Check’)
* Operator not valid for tuples
(‘CheckCheckCheck’)
Syntax error
>>> 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
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
>>> 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