How to write return in python

Usage of return in python

1、 The return statement is to return the execution result to the calling place and return the control of the program together

The program will return after the first return encountered (exit the def block), and will not run the second return.

E.g:

defhaha(x,y):
ifx==y:
returnx,y
print(haha(1,1))
Result: This kind of return will return tuples(1,2)

2、 But it does not mean that there can only be one return statement in a function body, for example:

deftest_return(x):
ifx 0:
returnx
else:
return0
print(test_return(2))

3、 The function does not return, and returns a None object by default.

There is no return in the recursive function:

defrecurve(a,b):
ifa%b==0:
returnb
else:gcd(b,a%b)

Analysis: There is no exit if there is no return in the else, this program is running internally, and the program has no return value.

4、 In interactive mode, the result of return will be automatically printed out, but when running as a script alone, the print function is required to display it.

What is the interactive mode in python: there are 3 symbols () at the end. It is called the Python command prompt (prompt).

Enter a line of python code to execute the code, this mode is called Python interactive mode (interactive mode).

Knowledge point expansion:

Python implements return to return multiple values

The return statement of a function can only return one value, which can be of any type.

Therefore, we can "return a tuple type to indirectly return multiple values".

def F1( x, y ):
 a = x % y
 b =(x-a)/ y
 return( a,b )  #Can also be written as return a,b(c, d )=F1(9,4)  #Can also be written as c, d =F1(9,4)
print c ,d

The results show: 1, 2

So far, this article on how to write return in python is introduced. For more information about how to write return in python, please search for ZaLou.Cn's previous articles or continue to browse related articles below. I hope you will support ZaLou more in the future. Cn!

Recommended Posts

How to write return in python
How to write win programs in python
How to write try statement in python
Teach you how to write games in python
How to write a confession program in python
How to write python configuration file
How to wrap in python code
How to omit parentheses in Python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to represent null values in python
How to save text files in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
Write gui in python
How to use the round function in python
How to use the zip function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
How to delete files and directories in python
How to install the downloaded module in python
How to read and write files with Python
How to perform continuous multiplication calculation in python
How to write Pythonic code
How to learn python quickly
How to uninstall python plugin
How to understand python objects
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How to create a Python virtual environment in Ubuntu 14.04
How to install Helm in Ubuntu
How to use hanlp in ubuntu
How to use python thread pool
How to find the area of a circle in python
How to save the python program
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
How to install HDP2.6 in Centos7.2
How to solve python dict garbled