To exit a Python program, you can use the exit()
function or the sys.exit()
function. Here’s how you can use both methods:
- Using the
exit()
function:
pythonCopy codeexit()
- Using the
sys.exit()
function (requires importing thesys
module):
pythonCopy codeimport sys
sys.exit()
These functions will immediately terminate your Python program and return you to the command prompt or the environment from which you ran the program. You can also provide an exit status code as an argument to these functions to indicate the reason for the program’s exit. For example:
pythonCopy codeimport sys
sys.exit(1) # Exit with an exit status code of 1 (indicating an error)
By convention, a return code of 0 usually indicates a successful exit, while non-zero codes typically indicate an error or some other specific condition.