In python, the variable only refers to the object and does not represent the object itself. The variable name points to that part of your computer's memory where the object is stored. What is output of the codes below and explain it?def func(m): m[0] = 100 m = [1, 2, 3] return mn = [4, 5, 6]func(n)print(n)print(func(n))
时间: 2024-03-08 07:50:48 浏览: 13
The output of the code will be:
```
[100, 5, 6]
[1, 2, 3]
```
Here's why:
- Initially, the variable `n` is assigned to a list `[4, 5, 6]`.
- When the function `func` is called with the argument `n`, the parameter `m` is also assigned to the same list object as `n`.
- Inside the function, the first element of `m` is modified to `100`.
- Then, a new list `[1, 2, 3]` is assigned to the variable `m`. This does not affect the variable `n`, which still points to the original list `[4, 5, 6]`.
- The function returns the value of `m`, which is the new list `[1, 2, 3]`.
- After the function call, the original list `n` has been modified to `[100, 5, 6]`.
- Finally, the `print` statements output the values of `n` and `func(n)` respectively, which are `[100, 5, 6]` and `[1, 2, 3]`.