`is`, `==`, and the £5 note in your pocket

You've read about how `is` and `==` are not the same thing.

But perhaps, you still find the concept a bit confusing.

So, here we go.
Read on

👇🪡🧵
#Python

/1 Image
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 Image
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:

/14
If you like analogies like this £5 note one…

…you may also like this one which goes through what happens behind the scenes when a Python program runs:

thepythoncodingbook.com/understanding-…

#python
#100daysofcode

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Stephen Gruppetta

Stephen Gruppetta Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @s_gruppetta_ct

Jun 17
Are these two lists exactly the same in every respect?

Are you sure?

Read on…

👇🪡🧵

#python Image
Have a look now… Image
There are always surprises lurking underneath the surface.
Read 7 tweets
Jun 10
Why should I care whether something is mutable or immutable?"

Often, beginners can see these distinctions as unimportant.

But that's the difference between thinking this behaviour is an error or that it makes perfect sense — see code attached

👇🪡🧵

#python

/1 Image
Beginners sometimes see definitions of terms such as mutable and immutable as rather academic.

"Does it matter?"

"If something doesn't work, I'll just try another way."

But once you understand the key principles behind these terms, lots of other things make sense.

/2
Let's see what mutable and immutable means in a short example:

/3 Image
Read 9 tweets
Jun 9
Let's play this game — I promise this thread will get more interesting later on…

…and if you're a beginner or intermediate learner, it will help you identify some "not good" content online (and in some kids' coding books, too…)

👇🪡🧵

#Python

1/
Look at this code.

What's your best guess for what the outputs from the 2 `print()` lines will be?

And what's your degree of confidence?

2/
For example, you can reply:

12 - 100%
Stephen - 100%

if you're absolutely certain those will be the outputs

3/
Read 18 tweets
Jun 9
You know about the `for` loop, I know.

But do you?

Are you sure?

Let's break the `for` loop down to see what's happening just a bit underneath the surface

Read on…

👇🪡🧵

#Python

/1
OK, so I'll assume you're very familiar with this:

>>> numbers = [4, 6, 8, 12, 15]
>>> for number in numbers:
... print(number)

Let's explore what's happening here by creating our own class called `Test`:

/2
"'Test' object is not iterable" is the error message you got.

You cannot use a `Test` object in a `for` loop.

Not yet, anyway.

When a for loop starts, it tries to convert the object you're looping through into an iterator.

/3
Read 20 tweets
Jun 7
User—>High Level Programmer—>Low Level Programmer: An Analogy

Everyone is a user of computer programs.

But many non-programmers don't quite understand what's the role of a programmer.

They see programming as black magic

Let's look at an analogy: A House

👇🪡🧵

#Python

1/
Bob and Kate live in a house.

They're the **users** of the house.

When they needed a house to live in, they bought or rented one.

2/
Bob and Kate don't know anything about how to build a house. That's the builder's job.

The builder is the **programmer**.

The builder learnt the skills needed to build a house just as programmers learn how to code.

3/
Read 15 tweets
Jun 6
Where are all those values I need?

You can ask NumPy.

Just ask `np.where()`?

Let's see how this function works through an example…

👇🪡🧵

#python #numpy

1/
A teacher asks his students to take a test twice, a few weeks apart.

2/
He thinks this is a fair system to use:

• If the difference in scores between the first and second tests is less than five marks, he'll keep the result of the first test

• Otherwise, he'll take the highest mark

3/
Read 14 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(