The == operator in Python is extremely fascinating, because Python is a loosely typed language. This means there exists a notion of equality not only between numbers, but also between numbers and strings, and strings and lists, and lists and dictionaries.

<aside> 💡 The == operator always returns True or False.

</aside>

Equality Operations: Common Examples

Let's first talk of some common examples:

>>> 1 == 1 # integer equality
True
>>> 1 == 2 # integer inequality
False
>>> 1.0 == 1.0 # float inequality
True
>>> {} == {} # dictionary equality
True
>>> 1 == 1.0 # floats and integers can be compared! Equality
True
>>> 1 == "1" # string integer inequality
False
>>> "1" == "1" # string equality
True
>>> "1" == "2" # string inequality
False
>>> False == None
False 
>>> [1] == [1] # list equality
True

And so on.

Equality Operations: Uncommon Examples

What if, instead, we started to compare Functions with Lists?

>>> f = lambda x: x+1
>>> f == [2]
False

As you could have guessed, this returns false!

Now, let's turn this up a notch

>>> f = lambda x: x+1
>>> g = lambda x: x+1
>>> f == g
False

Wait, what? Why is this also returning false? They are the same function, aren't they? Before we address this, we have to make clear a quick aside.


Aside: Everything is An Object

A question you perhaps never asked yourself: why can everything be compared in Python? Why can you check if a list is equal to a dictionary, or a dictionary to a function? As you'll see in later classes, this is not the case in other languages.

<aside> 💡 This is because, under the hood, everything in Python is an Object.

</aside>