What will be the output of the following Python code?
l=[“good”, “oh!”, “excellent!”, “#450”]
[n for n in l if n.isalpha() or n.isdigit()]
a) [‘good’, ‘oh’, ‘excellent’, ‘450’ ]
b) [‘good’]
c) [‘good’, ‘#450’]
d) [‘oh!’, ‘excellent!’, ‘#450’]
Answer: b
Explanation: The code shown above returns a new list containing only strings which do not have any punctuation in them. The only string from the list which does not contain any punctuation is ‘good’. Hence the output of the code shown above is [‘good’].
Related Posts
What will be the output of the following Python code?
>>> a=[(2,4),(1,2),(3,9)]
>>> a.sort()
>>> aIs the following Python code valid?
>>> a=(1,2,3)
>>> b=a.update(4,)What will be the output of the following Python code?
>>> a=(2,3,1,5)
>>> a.sort()
>>> aIs the following Python code valid?
>>> a=2,3,4,5
>>> aTuples can’t be made keys of a dictionary.
What will be the output of the following Python code?
>>> import collections
>>> a=collections.namedtuple(‘a’,[‘i’,’j’])
>>> obj=a(i=4,j=7)
>>> objWhat will be the output of the following Python code?
>>> a,b=6,7
>>> a,b=b,a
>>> a,b
Join The Discussion