If a function doesn't specify a return value, it returns None . In an if/then conditional statement, None evaluates to False.
In Python, it is possible to compose a function without a return statement. Functions like this are called void, and they return None, Python's special object for "nothing".
exit() method is used to terminate the process with the specified status. We can use this method without flushing buffers or calling any cleanup handlers. After writing the above code (python os. exit() function), the output will appear as a “ 0 1 2 “.
The function calls exit(0) and exit(1) are used to reveal the status of the termination of a Python program. The call exit(0) indicates successful execution of a program whereas exit(1) indicates some issue/error occurred while executing a program.
You could simply use
return
which does exactly the same as
return None
Your function will also return None
if execution reaches the end of the function body without hitting a return
statement. Returning nothing is the same as returning None
in Python.
I would suggest:
def foo(element):
do something
if not check: return
do more (because check was succesful)
do much much more...
you can use the return
statement without any parameter to exit a function
def foo(element):
do something
if check is true:
do more (because check was succesful)
else:
return
do much much more...
or raise an exception if you want to be informed of the problem
def foo(element):
do something
if check is true:
do more (because check was succesful)
else:
raise Exception("cause of the problem")
do much much more...
return None
or return
can be used to exit out of a function or program, both does the same thingquit()
function can be used, although use of this function is discouraged for making real world applications and should be used only in interpreter. import site
def func():
print("Hi")
quit()
print("Bye")
exit()
function can be used, similar to quit()
but the use is discouraged for making real world applications.import site
def func():
print("Hi")
exit()
print("Bye")
sys.exit([arg])
function can be used and need to import sys
module for that, this function can be used for real world applications unlike the other two functions.import sys
height = 150
if height < 165: # in cm
# exits the program
sys.exit("Height less than 165")
else:
print("You ride the rollercoaster.")
os._exit(n)
function can be used to exit from a process, and need to import os
module for that.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With