What will be the output of the following Python code?
>>> a=(0,1,2,3,4) >>> b=slice(0,2) >>> a[b]
Invalid syntax for slicing
[0,2]
(0,1)
(0,2)
Is the following Python code valid?
>>> 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,4),(1,2),(3,9)] a.sort() print(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
{ }
set()
[ ]
( )
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}
Error as unsupported operand type for set data type
Error as the duplicate item 6 is present in both sets
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={5,6,7} >>> sum(a,5)
5
23
18
Invalid syntax for sum method, too many arguments
a={5,6,7,8} b={7,8,9,10} print(len(a+b))
8
Error, unsupported operand ‘+’ for sets
6
Nothing is displayed
What will the output of following code?
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
keys, keys
key values, keys
keys, key values
key values, key values
s={abs}
s={4, ‘abc’, (1,2)}
s={2, 2.2, 3, ‘xyz’}
s={san}