1

The Code

def f(x, l=[] ):
    for i in range(x):
        l.append(i*i)
        print(l)

f(2)
f(2, [3,2,1])
f(3)

Result of the program:

[0]
[0, 1]
[3, 2, 1, 0]
[3, 2, 1, 0, 1]
[0, 1, 0]
[0, 1, 0, 1]
[0, 1, 0, 1, 4]

I can't figure out why f(3) prints:

[0, 1, 0]
[0, 1, 0, 1]
[0, 1, 0, 1, 4]

instead of

[0]
[0, 1]
[0, 1, 4]

Why does f(2, [3,2,1]) changes the result of f(3), but f(2) does not interfere with f(2, [3,2,1])?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Samuel Le
  • 83
  • 5

0 Answers0