educative.io

Are numbers mutable?

So if I run

num = 20

def multiply_by_10(n):

n *= 10             # Changing the value inside the function

return n

multiply_by_10(num)

print (num) # The original value remains unchanged

you just get the original value 20 but if I run

num=num*10

print(num)

I get 200 why?

No, int type variables are not mutable.

If the actual argument variable represents an immutable object, any modification inside the function will result in creating a different object, which will be local to the called function. It will not affect the original variable.

If you do num = multiply_by_10(num) only then num will be equal to 200.