Python 3D Academyবাংলা

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 list
  • x is None / x is not None: the right way to test for it

Step by step, for number = phone.get("Bo"):

  1. "Bo" isn't a key, so get gives back None.
  2. number is None is True: there's only one None object, and number points at it.
  3. 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, because get gave None, not "" (see the bug question).
  • is for numbers or strings. Use ==.
  • None in maths. None + 1 gives TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. Check for None first.
  • Printing what a method gave back. print([3, 1].sort()) shows None: sort changes the list and gives back nothing.

Quiz

  1. What does this print?

    x = None
    print(x is None)
  2. What does this print?

    a = [1]
    b = [1]
    print(a == b and a is not b)
  3. How should you check whether result holds no value?

    • result is None
    • result == 0
    • result is 0
  4. What does this print?

    d = {}
    print(d.get("k") is None)
  5. What does this print?

    a = [1, 2]
    b = a
    a = [1, 2]
    print(a is b)
  6. 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

  1. The phone book is phone = {"Ana": "123", "Bo": "456"}. Ask for a name and use get(). Print the number, or unknown if the name isn't there (check with is None).

  2. Start with a = [1, 2], b = a and c = [1, 2]. Print three lines: the result of a == c, then a is b, then a is c.

  3. Ask for whole numbers on one line. Print the first negative number, or no negatives if there isn't one. Start with first = None.