educative.io

Quiz #5 v = 3 instead of 1?

In the 5th question, the function foo should reset v to be 1. Why does v still equal to 3 when you print it?

Hello @Qingchuan_Lyu,

This is a really good question.

When mutable data is passed to a function, the function can modify or alter it. These modifications will stay in effect outside the function scope as well. An example of mutable data is a list.

In the case of immutable data, the function can modify it, but the data will remain unchanged outside the function’s scope. Examples of immutable data are numbers, strings, etc.

In the quiz question, v is assigned to an immutable number value and so the date will remain unchanged. You can learn more about python and mutating values in this course.

Best Regards,
Ali Ahad | Developer Advocate
Educative.io

2 Likes