Advanced

A name can hold nothing

A name that was never filled is an error. A name that was filled with nothing is a value, and that difference is how a program records that something has not happened yet.

What you will see

3 None

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Record the absence of a value and fill it later.

1

Store nothing under a name.

best = None

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
2

Ask whether it is still nothing.

print(best is None)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
True
3

Replace it with a real value and ask again.

best = 42 print(best is None)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
False
4

Store the result of a function that only prints. You get nothing.

def show(x): print(x) result = show(3) print(result)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3 None
Printing is for the reader, returning is for the program. A function that prints cannot hand its answer to a name, which is the mistake this step shows.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A name is filled with the value None, a test asks whether it still holds nothing, and a branch follows.
1 best = None stores nothing
↓
2 best is None gives yes
↓
3 the branch runs
↓
4 a real value replaces None later
Why this works
Why this matters now

Every function you write answers with something, and if you did not say what, it answers with nothing. Reading a function means asking whether it returns. That one question catches a whole class of bug where a value is printed and never passed on.

When you have a choice

Nothing versus zero

Zero is a real number you can add to. Empty text is a real empty message. None is the statement that no value was produced. Mixing them up makes a program compute with a fact that was never there, which is why the check is worth writing.

Examples

None is a value

best = None
print(best)

None means "nothing here yet". It is a value, so it prints, and it can be stored. It is not zero and not empty text, it is its own kind with one value.

Test for it

best = None
print(best is None)

The word is asks whether a name holds exactly that nothing. Testing with is None is the readable check, because other empty things can be false for their own reasons.

A function without an answer

def show(x):
    print(x)

print(show(3))

show prints but never says "give back". A function like that answers with None, so storing its result stores nothing useful. That is the difference between showing and returning.

A function with an answer

def twice(x):
    return x * 2

print(twice(3))

return hands a value back to where the call was written. Only then does print have something to show. A function either shows for you or gives a value, and giving is the useful one.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Yes/no values combine
Previous Lesson
Yes/no values combine
Next Lesson
Names live in a region
Names live in a region →