Skip to content

Module 3 · Variables and Data Types — topic 13 of 15

None

None is Python’s value for “nothing” or “no result yet”. Learn where None appears, how to test for it with is, and how to use it for optional values and default arguments.

Beginner 10 min read·Lesson 41 of 306 in Python Programming

Learning objectives

  • Explain what None represents
  • Test for None correctly with is
  • Use None for missing or optional values

Definition

None is Python’s value for “nothing” or “no result yet”. Learn where None appears, how to test for it with is, and how to use it for optional values and default arguments.

Introduction

None is a special value that means “nothing here”. It is not zero, not an empty string and not False; it is the absence of a value. Its type is NoneType, and there is only ever one None object.

Why it matters

You will meet None constantly: functions without a return statement return None, list.append() returns None, and dictionary .get() returns None for a missing key. Using None deliberately for “not set yet” makes programs clearer than inventing fake values such as -1.

Detailed explanation

Functions that end without return, or with a bare return, give back None.

Methods that change an object in place, such as list.sort(), return None to remind you that they don’t produce a new value.

Test for None with is None or is not None. Because there is only one None object, identity is the exact and recommended check.

None is falsy, but so are 0 and "". Use is None when you need to tell “missing” apart from a real zero or empty value.

None is commonly used as a stand-in for “not set yet” and as a default for optional function parameters.

Printing None shows the word None, and str(None) is the string "None". Be careful when building messages: an unexpected None in output usually means a function that should have returned a value didn’t, often because the return statement was forgotten.

Syntax

Python
result = None
if result is None:
result = compute()
if value is not None:
use(value)
  • Assign None to mean “no value yet”.
  • Check with is None, not == None.
  • is not None checks that a value exists.

Code examples

Functions without return give None

Python
def greet(name):
print("Hello", name)
result = greet("Zoya")
print(result)
print(type(result).__name__)
Output
Hello Zoya
None
NoneType

Missing dictionary keys

Python
prices = {"tea": 20}
print(prices.get("coffee"))
print(prices.get("coffee") is None)
Output
None
True

More examples

None versus zero

Python
scores = {"Arjun": 0, "Bela": None, "Charu": 18}
for name, score in scores.items():
if score is None:
print(name, "- absent, no score")
elif score == 0:
print(name, "- attended, scored zero")
else:
print(name, "- scored", score)
Output
Arjun - attended, scored zero
Bela - absent, no score
Charu - scored 18

Step-by-step explanation

  • None marks a student who didn’t take the test; 0 is a real score.
  • is None picks out the absent student precisely.
  • A plain if not score would treat Arjun’s 0 as absent too, which would be wrong.
  • Other scores fall through to the else branch.

Expected output

Running “None versus zero” prints exactly:

TextOutput
Arjun - attended, scored zero
Bela - absent, no score
Charu - scored 18

Practical applications

  • Database NULL values arrive in Python as None.
  • Optional form fields that the user left empty are stored as None.
  • Functions use None as a default to mean “use the standard behaviour”.

Best practices

  • Use None for “no value”, not magic numbers such as -1.
  • Test with is None / is not None.
  • Document when a function may return None.

Common mistakes

Using a falsy check when 0 is valid

Avoid

if not score:
print("missing")

Prefer

if score is None:
print("missing")

not score is also True for 0, so a real zero would be treated as missing.

Comparing with == None

Avoid

if value == None:

Prefer

if value is None:

is None is the idiomatic, exact check; style tools flag == None.

Key takeaways

  • None represents the absence of a value; its type is NoneType.
  • Functions without return and in-place methods return None.
  • Check for None with is.

Try it yourself

  1. 1
    Write a function that returns the first even number in a list, or None if there isn’t one. Test it with two lists.
  2. 2
    Explain the difference between score = 0, score = "" and score = None in one sentence each.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic