Some Understanding of return and print in python, pythonreturnprint
Preface
I recently started to learn python. I only had some c basics and I forgot about it. Now I want to learn and share my ideas ~ I read the return usage while reading the book, but it is mixed with print later. I always feel that the return value of the function can be directly displayed, but this is not the case. Next, let's take a look at the understanding of return and print in python. Let's take a look at the detailed introduction.
See an example:
Code (1)
x = 1y = 2def add (x, y): z = x + y return zprint (add(x,y))
Code (2)
x = 1y = 2def add (x, y): z = x + y print zprint (add(x,y))
Output result (Same)
>>> 3
Here, the result of code 1 is obtained by printing the return value, and the result of Code 2 is printed in the functionadd (x, y)
,add (x, y)
When the print z statement is executed, the returned value is None, so the output result should be
3None
The return value can only be printed. For example
def a(): print('ss')def b(): return 'ss'
Run directlya()
And runs directly with results.b()
There is no result, only runprint(b())
To display 'ss '.
In addition, assign a function to a variable to obtain the return value.b()
, Add
c=b()c
There is a result, because c getsb()
The return value is 'ss '.
In the interactive mode, the return result is automatically printed, and the print function is required for the script to be run independently.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.