My understanding of variable scope in Python was that if you pass a global variable into a function, the global variable's value will not be changed by any operations done on it within the scope of the function. For example, if I had a function:
def add_two_to(num):
num += 2
return num
and I ran:
my_number = 5
new_number = add_two_to(my_number)
I would expect the value of new_number to be 7, and the value of my_number to remain 5. When I run this code in a Jupyter notebook, that is exactly what happens.
But for some reason, when I run the following code:
def place_on_board(board):
board[1] = 'X'
return board
game_board = [0,1,2]
new_board = place_on_board(game_board)
Both new_board and game_board now have the value [0,'X',2]. This is happening regardless of where I declare game_board. Why is my place_on_board function overwriting the global variable game_board? It seems to go against everything I thought I understood about variable scope.