Python Tips -05

What is the output of the following Python code snippet?

def fun(a):

    a.append(100)

    print(a)

a=[10,20,30]

print("Before calling function fun, the value of a=",a)

fun(a)

print("After calling function fun, the value of a=",a)

OUTPUT

Before calling function fun, the value of a= [10, 20, 30]
[10, 20, 30, 100]
After calling function fun, the value of a= [10, 20, 30, 100]

Explanation:

Here the list a is passed as a parameter to the function namely fun. Since the list is a mutable type, by default it is passed as a reference. So any changes to the parameter a within the function fun will affect the original list a. So the value of the list after function call is [10,20,30,100].

Comments

Popular posts from this blog

Python Tips -06

C Puzzle

Python Tips - 03