Python Tutorial - None and Return

In Python, there is a special constant None (N must be capitalized). Unlike False, it does not represent 0, nor does it mean an empty string, but it means no value, that is, a null value.

The null value here does not represent an empty object, that is, None is different from [], "":

None is []
None is ""

None has its own data type. We can use the type function in IDLE to see its type. The execution code is as follows:

type(None)
           

The output is:

NoneType

As you can see, it is of type NoneType.

It should be noted that None is the only value of the NoneType data type (other programming languages may call this value null, nil, or undefined), that is, we cannot create other variables of type NoneType, but we can assign None to any variable. If you want something stored in a variable not to be confused with any other value, you can use None.

In addition, None is often used in asserts, judgments, and cases where functions have no return value. For example, in the previous chapter we used the print function to output data. In fact, the function's return value is None. Because its function is to display text on the screen without returning any value at all, print returns None.

spam = print('Hello!')
None == spam
           

The output is:

Hello!
True

In addition, for all function definitions without a return statement, Python adds return None to the end and uses a return statement without a value (that is, only the return keyword itself), then returns None.

Python Return

So far, the functions we have created have only processed the incoming data, and the processing ends. But in fact, in some scenarios, we need a function to feed back the result of the process, just as example if the supervisor gave an order to a lower-level employee to print the file. After the employee printed the file in fact the task has not yet completed, as the employee need to return the file back to the supervisor.

In Python, when you create a function with a def statement, you can use a return statement to specify the value that should be returned. The return value can be of any type. It should be noted that the return statement can appear multiple times in the same function, but as long as one is executed, the execution of the function will be ended directly.

In the function, the syntax of the return statement is as follows:

return [return value]

Among them, the return value parameter can be specified or omitted (the empty value None will be returned).

[Example:]

def add (a, b):
    c = a + b
    return c
#Function Assignment to Variable
c = add (3,4)
print (c)
#Function return value as the actual parameter of other functions
print (add (3,4))
           

The output is:

7
7

In this example, the add function can be used to calculate the sum of two numbers or to concatenate two strings. It returns the result of the calculation.

After specifying the return value through the return statement, when calling the function, we can either assign the function to a variable, save the return value of the function with a variable, or use the function as the actual parameter of a function.

[Example:]

def isGreater0 (x):
    if x> 0:
        return True
    else:
        return False
print (isGreater0 (5))
print (isGreater0 (0))
           

The output is:

True
False

As you can see, the function can contain multiple return statements at the same time, but it should be noted that only one is actually executed in the end, and once executed, the function run will end immediately.

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises