is, == and None
🌍 In real life. Two students can have equal marks without being the same student. And on a form, "phone number: none" isn't a number at all; it's a clear way of saying "there's nothing here".
Python has a test for each question, == and is, and a special value, None, for "nothing here".
The idea in one sentence: use == to compare values, and keep is for checking whether something is None, Python's "no value here".
Its words:
==: equal values?is: the very same object?None: the one "nothing" object;get()gives it back for a missing key, and so does a method that changes a listx is None/x is not None: the right way to test for it
Step by step, for number = phone.get("Bo"):
- "Bo" isn't a key, so
getgives backNone. number is Noneis True: there's only oneNoneobject, andnumberpoints at it.- So the program can say "Bo isn't in the phone book".
phone = {"Ana": "123"}
number = phone.get("Bo")
print(number)
print(number is None)
if number is None:
print("Bo isn't in the phone book")Don't use is to compare numbers or strings. Python sometimes reuses the same object for equal small values and sometimes doesn't, so is can surprise you. == always gives the honest answer.
a = [1]
b = a
c = [1]
print(a is b, a is c, a == c)A handy pattern: start with None to mean "not found yet", and fill it in when you find something.
first_big = None
for n in [3, 8, 2, 11]:
if n > 5:
first_big = n
break
print(first_big)Why is for None? There is exactly one None object in a running program, so "is it that object?" is both the fastest and the most honest test. For numbers and strings, Python sometimes reuses an object and sometimes doesn't, so is can give different answers for equal values; Python even prints a SyntaxWarning if you write x is 5.
Mistakes and edge cases.
- Testing for None with ==, "" or 0.
if number == "":never catches a missing key, becausegetgaveNone, not""(see the bug question). - is for numbers or strings. Use
==. - None in maths.
None + 1givesTypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. Check for None first. - Printing what a method gave back.
print([3, 1].sort())showsNone: sort changes the list and gives back nothing.
Quiz
What does this print?
x = None print(x is None)What does this print?
a = [1] b = [1] print(a == b and a is not b)How should you check whether
resultholds no value?- result is None
- result == 0
- result is 0
What does this print?
d = {} print(d.get("k") is None)What does this print?
a = [1, 2] b = a a = [1, 2] print(a is b)Bo isn't in the phone book, but this never prints not found. Which line has the mistake?
phone = {"Ana": "123"} number = phone.get("Bo") if number == "": print("not found")
Coding tasks
The phone book is
phone = {"Ana": "123", "Bo": "456"}. Ask for a name and useget(). Print the number, orunknownif the name isn't there (check withis None).Start with
a = [1, 2],b = aandc = [1, 2]. Print three lines: the result ofa == c, thena is b, thena is c.Ask for whole numbers on one line. Print the first negative number, or
no negativesif there isn't one. Start withfirst = None.