🎓 A1Learner
🔍
0 of 116 lessons completed

Python None

None is the special value that means no value yet, like a blank line on a form. A check with is None asks whether a variable still holds None.

No value yet

The variable exists. It just holds nothing yet.

score = None
print(score)
print(type(score))

Checking for None

The word is asks if two things are the same value. Use it to test for None.

name = None
print(name is None)
print(name is not None)

When a value arrives

Later code can store a real value in the same variable.

player = None
player = "Ada"
print(player is None)
print(player)

None is not zero

Zero is a real number. An empty string is real text. None is neither of them.

print(None == 0)
print(None == "")

Example

current_player = None
print("Player:", current_player)
print(current_player is None)
current_player = "Mia"
print("Player:", current_player)
print(current_player is None)

Key points

  • None: the special value that means no value yet
  • NoneType: the type of None, shown by type(None)
  • Use is None to check if a variable still holds None
  • Use is not None to check if a variable holds a real value
  • None is not 0 and not an empty string

In this example, current_player starts as None. The first print shows that blank as None. The next line checks with is None and prints True. Then the variable gets the name "Mia". The last two lines print the new name. The check now prints False, because a real value has arrived.

Note

In Python, there is only one None value.

Every blank variable holds that same None.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which check is the Python way to ask if ticket still has no value?