You must own this product to watch this lesson.

# Comparing Objects

Copied!
Happy Pythoning!

rorydaulton

This is a pretty good lesson. But I think you could have made the difference between `is` and `==` even more clear by changing the value of `a` after `b = a; c = list(a)` then showing the new values of `a`, `b`, and `c`. The `is` between `a` and `b` means that the value of `b` was also changed, but since `c` does not meet the `is` test, its value is not changed.

This is something I saw repeatedly at Stack Overflow: new Python programmers do not understand just when changing the value of one name will or will not change the value of another name. So understanding this consequence of the difference between `is` and `==` is very practical. If you update this video, I recommend that you add showing the new values of `b` and `c` at the end.

varelaautumn

I found the `id` to be the most helpful thing for me understanding `is` vs `==`.

I think these two examples lay out the main differences:

``````# Lists (mutable)
a = [1,2,3]
b = a
c = [1,2,3]

print(f'id of c: {id(a)}')
print(f'id of c: {id(b)}')
print(f'id of c: {id(c)}')
print(f'a is b: {a is b}')
print(f'a is c: {a is c}')

>>> 'id of a: 2081950383616'
>>> 'id of b: 2081950383616'
>>> 'id of c: 2081950373760'
>>> 'a is b: True'
>>> 'a is c: False'
``````
``````# Tuples (immutable)
a = (1,2,3)
b = a
c = (1,2,3)

print(f'id of a: {id(a)}')
print(f'id of b: {id(b)}')
print(f'id of c: {id(c)}')

>>> 'id of a: 2081946553216'
>>> 'id of b: 2081946553216'
>>> 'id of c: 2081946553216'
>>> 'a is b: True'
>>> 'a is c: True'
``````

The `id` represents a unique number pointing to a specific memory address of the object.

Tuples are immutable, just like an integer. Tuple(1,2,3) is Tuple(1,2,3) is no different than saying “1 is 1”. They can’t change so there is no reason for that unique value to have anything but 1 location in memory.

Lists on the other hand are mutable. You may have 5,000 independent variables which are all initially set to the value [1,2,3]. If all those 5,000 variables were pointing to the same memory address which held the list value [1,2,3]. Then altering any one of those 5,000 variables, would change every other variable at that memory address. Obviously that’d be a messy disaster.

So instead every list which happens to share the same value still maintains their own memory address/id, so they can mutate independently of others which happen to have the same value. Hence in the example, “a is not c”. They point to two different lists at two different memory addresses.

The reason b is a, is because b was set equal to a. When you say ‘b = a’, you’re pointing b to the same memory address that a is pointing at. Both point to the same list which happens to contain [1,2,3]. So they have the same id, and hence ‘b is a’.

It’s all about what memory address they’re pointing at, and not what value they are. (Though if the object is immutable, there’s only going to be one memory address for any specific value, “1 is 1”, “5 is 5”, “tuple(5,4) is tuple(5,4)”, “ ‘b’ is ‘b’ “, “True is True”, “False is False”, etc)

Dan B

I really think a quiz would help me learn this.

Robot

Nawh, Dan don’t beat that dead cat. The dead ringer for the first one.

You must own this product to join the conversation.