निम्नलिखित कोड का परिणाम क्या है ?
What is the output of the following code ?
WM=[‘b’ * x for x in range(4)]
print(WM)
A)
B)
C)
D)
Explanation:
Let's break down the code:
WM = ['b' * x for x in range(4)]
print(WM)
-
The list comprehension
['b' * x for x in range(4)]
iterates over the range of numbers from0
to3
. -
In each iteration, it multiplies the string
'b'
by the current value ofx
. -
For
x = 0
:'b' * 0
results in an empty string''
. -
For
x = 1
:'b' * 1
results in'b'
. -
For
x = 2
:'b' * 2
results in'bb'
. -
For
x = 3
:'b' * 3
results in'bbb'
.
So, the list WM
will be: ['', 'b', 'bb', 'bbb']
.
Answer: (A) ['', 'b', 'bb', 'bbb'].