Skip to main content

As per my knowledge pythonthe Python interpreter creates references from 0 to 256 for int objects,at at the start itself. Here I got output 3,[44,23,3][44,2,3], for the list I understood, but why did the same thing did not happen with the int object.I expect? I expected the output as 1,[44,2to be 1,3][44,2,3].Can Can anyone please explain to me why this behavior to me?. I use pythonPython 3.

    def change(var, lst):
        var = 1
        lst[0] = 44
    k = 3
    a = [1, 2, 3]
    change(k, a)
    print(k)
    print(a)
 
output got:

Output:

3  
[44,2,3]
expected output: 

Expected output:

1, 
[44,2,3]

As per my knowledge python interpreter creates references from 0 to 256 for int objects,at the start itself. Here I got output 3,[44,2,3], for the list I understood, but why the same thing did not happen with int object.I expect output as 1,[44,2,3].Can anyone explain to me why this behavior?. I use python 3.

    def change(var, lst):
        var = 1
        lst[0] = 44
    k = 3
    a = [1, 2, 3]
    change(k, a)
    print(k)
    print(a)
 
output got:3 [44,2,3]
expected output: 1,[44,2,3]

As per my knowledge the Python interpreter creates references from 0 to 256 for int objects, at the start itself. Here I got output 3,[44,2,3], for the list I understood, but why did the same thing not happen with the int object? I expected the output to be 1,[44,2,3]. Can anyone please explain this behavior to me? I use Python 3.

def change(var, lst):
    var = 1
    lst[0] = 44
k = 3
a = [1, 2, 3]
change(k, a)
print(k)
print(a)

Output:

3 
[44,2,3]

Expected output:

1 
[44,2,3]
Post Closed as "Duplicate" by deceze python
Source Link
user9121250
user9121250

Why different reference is created for int object when passed to function in python?

As per my knowledge python interpreter creates references from 0 to 256 for int objects,at the start itself. Here I got output 3,[44,2,3], for the list I understood, but why the same thing did not happen with int object.I expect output as 1,[44,2,3].Can anyone explain to me why this behavior?. I use python 3.

    def change(var, lst):
        var = 1
        lst[0] = 44
    k = 3
    a = [1, 2, 3]
    change(k, a)
    print(k)
    print(a)

output got:3 [44,2,3]
expected output: 1,[44,2,3]