Difference between return, return None, and no return description in Python

It's messed up.

Suddenly, I did a little research on Python's return.

There is no difference in behavior, but it seems that there is a flow to use which return.

Just return

return is used to break out of the loop.

For example, if you have only one golden apple out of 20 and you want to find it

for apple in apples:
	if "golden" in apple.color:
		print "I found it!"
		return

It will be like this. There is only one golden apple, so it's okay to break out of the loop when you find it. In this case, just use return.

return None return None is used when the returned value is used later.

For example, when you want to return the price of a golden apple

if is_golden(apple):
	return apple.price
else:
	return None

It will be. For non-golden apples, no price information is needed, so None is returned.

return No description

If you don't need to use the returned value later, you don't need to write return.

if is_golden(apple):
	print "It's a golden apple!"

In this case, there is no need to return (the returned value is not reused), so the return description is unnecessary.

Summary

--return is when exiting the loop --return None is when the return value is reused --No need return if the return value is not reused

Recommended Posts

Difference between return, return None, and no return description in Python
Difference between == and is in python
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Difference between return and print-Python
Difference between Ruby and Python in terms of variables
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Python module num2words Difference in behavior between English and Russian
List concatenation method in python, difference between list.extend () and β€œ+” operator
Differences in authenticity between Python and JavaScript
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
Difference in how to write if statement between ruby ​​and python
[Python] Difference between class method and static method
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Differences in multithreading between Python and Jython
File open function in Python3 (difference between open and codecs.open and speed comparison)
Difference in writing method to read external source code between Ruby and Python
[python] Calculation of months and years of difference in datetime
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
[Python] Explain the difference between strftime and strptime in the datetime module with an example
Install OpenCV 3 (core + contrib) in Windows & Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & Easy operation check
Difference between process and job
Find the difference in Python
Difference between regression and classification
Stack and Queue in Python
Difference between np.array and np.arange
Difference between MicroPython and CPython
Unittest and CI in Python
Maxout description and implementation (Python)
Difference between ps a and ps -a
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Get the current date and time in Python, considering the time difference
MIDI packages in Python midi and pretty_midi
View photos in Python and html
Sorting algorithm and implementation in Python
Memorandum (difference between csv.reader and csv.dictreader)
Manipulate files and folders in Python
About dtypes in Python and Cython
(Note) Difference between gateway and default gateway
Cooperation between python module and API
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Check and move directories in Python
Difference between sort and sorted (memorial)
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
Function synthesis and application in Python