Codectionary / Developer documentation / Python

None

None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.

Syntax

variable = None

Examples

Initializing a Variable

Using None as a placeholder before a real value is available.

user = None
print(user)  # None

# Later in the program, assign a real value
user = "Alice"
print(user)  # Alice

Default Parameter Value

Using None as a safe default for mutable or optional parameters.

def greet(name=None):
    if name is None:
        print("Hello, stranger!")
    else:
        print(f"Hello, {name}!")

greet()          # Hello, stranger!
greet("Priya")   # Hello, Priya!

Checking for None

Using 'is' and 'is not' to test for None, the recommended way instead of equality operators.

result = None

if result is None:
    print("No result yet")

data = {"name": "Fola"}
value = data.get("age")  # key doesn't exist

if value is not None:
    print(f"Age: {value}")
else:
    print("Age not provided")

Implicit Return Value

A function with no return statement (or a bare return) implicitly returns None.

def log_message(msg):
    print(f"LOG: {msg}")
    # no return statement

result = log_message("Server started")
print(result)  # None

Best practices

  • Always use 'is None' or 'is not None' rather than '== None', since None is a singleton and identity comparison is both faster and more correct
  • Use None as a default argument for optional parameters instead of mutable defaults like [] or {}, which are evaluated only once and shared across calls
  • Remember that dict.get() returns None by default when a key is missing, rather than raising an error like square-bracket access does
  • Don't confuse None with False, 0, or an empty string/list - they are all falsy in a boolean context but are not the same value
  • Use None to represent "not yet known" or "not applicable", and reserve empty collections for "known to be empty"

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.
False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.
Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.
print()
print() is Python's built-in function for writing output to the console. It can accept any number of values, automatically converts them to strings, and separates them with a space by default. Its sep and end keyword arguments give fine control over formatting, making print() useful for everything from simple debugging to building formatted console output.