is vs ==
Before we explain the difference between is and
==, have a look at the expressions below and take a
guess at what each one will return before you run them.
print(True == 1)print(' ' == 1)print([] == 1)print(10 == 10.0)print([] == [])
Loading quiz...
== Equality of Value
The == operator checks if two values are equal. However
it is important to understand that equal does not always mean the same
type. For example '1' == 1 returns False
because even though they look similar, '1' is a string
and 1 is an integer. They are not the same type so Python
considers them unequal.
is - Equality of Memory
The is keyword does something completely different. Instead
of asking "are these values equal?", it asks "are these the exact same
object in memory?" Every time Python creates a value it stores it
somewhere in memory. is checks whether two variables are
pointing to the exact same location in memory, not just whether they
contain the same value.
This is why '1' is '1' returns True and
True is True returns True. Python stores
simple values like short strings and Booleans in a shared memory
location and reuses them, so they point to the same place.
But [] is [] returns False. This surprises
a lot of beginners. Even though both lists are empty and look identical,
every time you create a list Python creates a brand new object in a
brand new location in memory. The two lists may contain the same values
but they live in different places, so is returns
False.
The same applies to any list regardless of its contents. Even
[1, 2, 3] is [1, 2, 3] returns False because
the two lists were created separately and occupy different locations
in memory.
To summarise:
==checks if the values are the sameischecks if the location in memory is the same
Try each of the following in the box below and run it each time to see what Python returns:
print('1' == 1)print('1' is '1')print(True is True)print([] is [])print([1, 2, 3] is [1, 2, 3])print([1, 2, 3] == [1, 2, 3])
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named is_vs_equals.py in your part43 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Predict the Output
- Task: Before running the code, write a comment predicting what each expression will return. Then run each one and check if you were right.
print(1 == 1.0)print(1 is 1.0)print(True == 1)print(True is 1)print(None is None)
- Goal: Write a comment next to each result explaining why Python returned that value.
Exercise 2: The List Trap
-
Task: Create two variables
list_a = [1, 2, 3]andlist_b = [1, 2, 3]. - Goal: Run the following checks and write a comment explaining why each one returns what it does:
print(list_a == list_b)print(list_a is list_b)
Exercise 3: Same Object
-
Task: Create a variable
list_a = [1, 2, 3]and assign it to a second variablelist_b = list_a. - Goal: Run the following checks and write a comment explaining why the result is different from Exercise 2:
print(list_a == list_b)print(list_a is list_b)
Exercise 4: Spot the Difference
- Task: Look at the two blocks of code below and predict what each will print before running them.
a = 'hello'b = 'hello'print(a == b)print(a is b)c = ''.join(['h','e','l','l','o'])print(a == c)print(a is c)
-
Goal: Write a comment explaining why
a is banda is cmay return different results even though all three variables contain the word "hello".
Exercise 5: When to use is and When to use ==
-
Task: Create a variable
result = None. -
Goal: Write two checks. First use
==to check ifresultisNoneand then useisto check the same thing. Print both results and write a comment explaining which one is the correct and recommended way to check forNonein Python and why.
Don't Forget to commit and Push!