x = 1
def f1(y):
y += 1
return y
x1 = f1(x)
print('x = ', x, ' x1 = ', x1)answer: x = 1 x1 = 2
x_list = ['1','2']
def f2(y_list):
y_list[0] = '0'
return y_list
x1_list = f2(x_list)
print('x_list = ', x_list, ' x1_list = ', x1_list)answer:x_list = ['0', '2'] x1_list = ['0', '2']
why in the first case the global variable is preserved and not in the second case?
