This Python None lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
None means no value. Functions that do not return anything give None. A missing dict value can use None as a placeholder.
Test for None with is, not with ==. is checks identity, which is what you want for this single object.
Example 1: Start here
None is not the same as 0 or an empty string.
def announce(message): # define the function; it runs when you call it
if not message: # run the indented lines only when this is true
return None # send this value back to the caller
print(message)
print(announce(""))
announce("Batch starts at 9") # call the function
Example 2: A missing value
None means there is no value yet. Use is, not ==, to test it.
score = None # no value yet
print(score is None) # is checks the same object, not just equal contents
Example 3: A function without return
If you do not return anything, the function result is None.
def show(): # define the function; it runs when you call it
print("hi")
print(show() is None) # is checks the same object, not just equal contents
Example 4: Use None as a default
None is a useful default when the caller may omit the argument.
def greet(name=None): # define the function; it runs when you call it
if name is None: # run the indented lines only when this is true
name = "learner" # text
print("Hello", name) # a comma prints each value, with a space between
greet() # call the function
greet("Asha") # call the function
