Python Exit Command using exit() Function

The exit() in Python is defined as exit commands in python if in site.py and it works only if the site module is imported so it should be used in the interpreter only. It is like a synonym for quit() to make Python more user-friendly. It too gives a message when printed and terminate a program in Python.

Example: In the provided code, when i is equal to 5, it prints “exit” and attempts to exit the Python interpreter using the exit() function. If i is not equal to 5, it prints the value of i.

Python3




for i in range(10):
    if i == 5:
        print(exit)
        exit()
    print(i)


Output:

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit

Python exit commands: quit(), exit(), sys.exit() and os._exit()

The functions quit(), exit(), sys.exit(), and os._exit() have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exits and no stack traceback is printed. We can catch the exception to intercept early exits and perform cleanup activities; if uncaught, the interpreter exits as usual. In this article, we will see how to exit from the Python program.

Similar Reads

What are Python Exit Commands?

Exit commands in Python refer to methods or statements used to terminate the execution of a Python program or exit the Python interpreter. The commonly used exit commands include `sys.exit()`, `exit()`, and `quit()`. These commands halt the program or interpreter, allowing the user to gracefully terminate the execution. there are some commands in Python for exit here we are discussing these commands in brief the commands are the following...

Python Exit Command using quit() Function

The quit() function works as an exit command in Python if only if the site module is imported so it should not be used in production code. Production code means the code is being used by the intended audience in a real-world situation. This function should only be used in the interpreter. It raises the SystemExit exception behind the scenes. If you print it, it will give a message and end a program in Python....

Python Exit Command using exit() Function

...

sys.exit([arg]) using Python

The exit() in Python is defined as exit commands in python if in site.py and it works only if the site module is imported so it should be used in the interpreter only. It is like a synonym for quit() to make Python more user-friendly. It too gives a message when printed and terminate a program in Python....

os._exit(n) in Python

...