Let's assume you and I both have a £5 note in our pocket
<replace with currency of choice>
and
<read "bill" instead of "note" if you wish>
/2
We meet up and go to a coffee shop nearby to buy two coffees which cost £5 altogether
Does it matter which £5 note we use to pay?
No, all the barista cares about is that our_payment == 5
So _you_ can pay. Thank you!
/3
In this instance, the value of the £5 note is what matters, not which actual physical note we use.
/4
But let's assume we're chatting and we spot a £5 note on the floor.
We both check our pockets and we realise we've both lost our £5 note.
Is the note on the floor mine or yours?
/5
Its value is no longer what matters now.
We both had a £5 note.
What matters is whether this is the actual physical note that was in my pocket or yours.
/6
Luckily, we're both geeks and we keep a record of the serial numbers of all the notes we carry with us on our phone.
I know, that's quite sad.
And quite unlikely, too.
But bear with me.
/7
So, we both get our phones out to check the serial numbers on record against the serial number on the £5 note on the floor.
You can see the serial number in two places on the £5 note.
/8
Sorry, the serial number of the note shows it's _mine_!
This ** is ** my note. In this case, it wasn't `==` that matters but `is`.
/9
With a Python object, the built-in `id()` function gives you the "serial number", which is unique for each object, just as the serial number on a bank note is unique.
/10
In programming, as in the case with bank notes, you often care more about the value of an object rather than its identity.
So, you'll often need `==` just like the barista in the coffee shop didn't care about the £5 note's serial number.
/11
But, there are times when you want to know that an object is exactly the same object and not just equal in value.
In these cases, you'll need to use `is`.
/12
As a general rule, use `==` by default unless you know you need `is`.
/13
Ok, no need to quabble about who the £5 note belongs this.
You can keep it, as long as you retweet the first tweet below: