Skip to content

Instantly share code, notes, and snippets.

@vchan14
Last active June 20, 2020 23:23
Show Gist options
  • Save vchan14/a81348127b17f03a4aed3376ffd12fa6 to your computer and use it in GitHub Desktop.
Save vchan14/a81348127b17f03a4aed3376ffd12fa6 to your computer and use it in GitHub Desktop.
Mutable vs Immutable
def addTwo(val):
val += 2
x = 1
addTwo(x)
print(x)
# Output 1
# Expected 3
favoriteNumbers = [1, 2, 3, 4]
# Update the first element to 0
favoriteNumbers[0] = 0
# Add 5 to the list
favoriteNumbers += [5]
print(favoriteNumber)
# Output [0, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment