0

I am currently learning python and got stuck at one of the program. Here is the snippet

test_board = ['#','X','O','X','O','X','O','X','O','X']
print(test_board)
def place_marker(board, marker, position):
    board[position] = marker

place_marker(test_board, '$', 8)
print(test_board)

The output of above program is

['#', 'X', 'O', 'X', 'O', 'X', 'O', 'X', 'O', 'X']
['#', 'X', 'O', 'X', 'O', 'X', 'O', 'X', '$', 'X']

I am confused that how did the value of variable test_board got updated ? I didn't set the global variable nor I am returning the value. Can someone please help me understand ?

Kapil
  • 832
  • 2
  • 14
  • 29
  • 2
    In python, list are mutable object – Osman Mamun Feb 22 '19 at 04:52
  • 1
    When you pass a parameter to a function, you get a reference to the object that was given by the caller. If that object is mutable you can change it as you did with `board[position] = ...`. Now that you know that, the links given at the top should make more sense. – Mark Ransom Feb 22 '19 at 04:56
  • Yes. Thank you so much. Now I can proceed. – Kapil Feb 22 '19 at 04:56

0 Answers0