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.
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)More Python Exercises:
Python String Exercises