Skip to content

Instantly share code, notes, and snippets.

@damianooldoni
Created August 17, 2018 21:35
Show Gist options
  • Save damianooldoni/04106c7e01e3980a3176219cbc137243 to your computer and use it in GitHub Desktop.
Save damianooldoni/04106c7e01e3980a3176219cbc137243 to your computer and use it in GitHub Desktop.
The Equals sign cheat sheet showing the behavior of "=" with basic classes (integers, tuples, lists, sets, dictionaries)
# integers, tuples: "=" makes a copy of the object. Changes of the original object after "=" instruction doesn't affect the copy.
a = 2
b = a
a = 3
print(a) # 3
print(b) # 2
# tuples
a = (2, 3)
b = a
a = (3, 4)
print(a) # (3, 4)
print(b) # (2, 3)
# lists, sets and dictionaries: "=" creates a reference to the original object. Changes after "=" instruction affect both objects
a = [2, 3]
b = a
a.append(4)
print(a) # [2, 3, 4]
print(b) # [2, 3, 4]
b.append(5)
print(a) # [2, 3, 4, 5]
print(b) # [2, 3, 4, 5]
# sets
a = {2, 3}
b = a
a.add(4)
print(a) # {2, 3, 4}
print(b) # {2, 3, 4}
b.add(5)
print(a) # {2, 3, 4, 5}
print(b) # {2, 3, 4, 5}
# dictionaries:
a = {2: "Two", 3: "Three"}
b = a
a[4] = "Four"
print(a) # {2: 'Two', 3: 'Three', 4: 'Four'}
print(b) # {2: 'Two', 3: 'Three', 4: 'Four'}
b[5] = "Five"
print(a) # {2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five'}
print(b) # {2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment