निम्नलिखित पायथन प्रोग्रामों का आउटपुट पता करें:
Find out the output of the following Python programs:
def gfg(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)
gfg(2)
A)
B)
C)
D)
Explanation:
Let's analyze the given Python code step by step:
def gfg(x, l=[]):
for i in range(x):
l.append(i * i)
print(l)
gfg(2)
-
Function
gfg
:- It takes two parameters:
x
andl
.l
has a default value of an empty list[]
. - Inside the function, a loop runs from
0
tox-1
(inclusive). In each iteration, it appendsi*i
(the square ofi
) to the listl
.
- It takes two parameters:
-
Calling
gfg(2)
:x = 2
andl
uses the default value[]
(an empty list).- The loop will run for
i = 0
andi = 1
:- When
i = 0
:0*0 = 0
, so0
is appended to the list. - When
i = 1
:1*1 = 1
, so1
is appended to the list.
- When
-
Final result: After the loop, the list
l
will contain[0, 1]
and that will be printed.
Output:
(B) [0, 1]
Explanation:
- Since the list
l
starts empty and the loop runs twice (fori = 0
andi = 1
), the final list becomes[0, 1]
.