निम्नलिखित पायथन प्रोग्रामों का आउटपुट पता करें:
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
[3, 2, 1, 0, 1, 4]
B
[0, 1]
C
[0, 1, 0, 1, 4]
D
error in code
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:
xandl.lhas a default value of an empty list[]. - Inside the function, a loop runs from
0tox-1(inclusive). In each iteration, it appendsi*i(the square ofi) to the listl.
- It takes two parameters:
-
Calling
gfg(2):x = 2andluses the default value[](an empty list).- The loop will run for
i = 0andi = 1:- When
i = 0:0*0 = 0, so0is appended to the list. - When
i = 1:1*1 = 1, so1is appended to the list.
- When
-
Final result: After the loop, the list
lwill contain[0, 1]and that will be printed.
Output:
(B) [0, 1]
Explanation:
- Since the list
lstarts empty and the loop runs twice (fori = 0andi = 1), the final list becomes[0, 1]. -
-
range(2)का मतलब है 0 और 1।i*iकरने पर0*0=0और1*1=1लिस्ट में जुड़ेंगे।
-
Correct Answer: B) [0, 1]