Hi @Nitheshcr
Thanks for contacting the Educative Team. In the case of immutable data, the function can modify it, but the data will remain unchanged outside the function’s scope. Immutable objects are strings or numbers. The function cannot update the value of these objects until we update it outside the function scope by assigning the return value from the function to the variable.
For Example, if we change the value of an integer inside a function, original value will remain unchanged :
num = 20
def multiply_by_10(n):
n *= 10
num = n # Changing the value inside the function
print("Value of num inside function:", num)
return n
multiply_by_10(num)
print("Value of num outside function:", num) # The original value remains unchanged
But if we assign the return value of the function to variable, the original value will get changed.
num = 20
def multiply_by_10(n):
n *= 10
num = n # Changing the value inside the function
print("Value of num inside function:", num)
return n
num= multiply_by_10(num)
print("Value of num outside function:", num) # The original value remains unchanged
I hope it helps. Happy Learning