Python exit function | quit function in Python [4+ Examples]

In this Python tutorial, I will tell you about the Python exit function with a few examples. Also, I will cover what the quit function in Python is.

Being a Python developer, we often come into a situation where we need to terminate the execution of the code or program gracefully.

This is where the exit() and quit() functions in Python come into play. While they have a similar role to perform, they also have some important differences.

While explaining what is exit function in Python, I will also cover below topics in the article:

  • Python sys.exit() function
  • Python os.exit() function
  • Python raise SystemExit
  • Program to stop code execution in Python
  • Difference between exit() and sys.exit() in Python
  • Python quit vs exit function.

Python exit function

The exit() function in Python is used to exit the running Program. We can use the exit function to stop the execution of the program at any point.

When we call the Python exit() function, the program will immediately stop running the code and exit.

Here’s the syntax of the exit function in Python: It accepts an optional argument representing the exit status.


Here, the status parameter is an optional parameter in the Python exit() function. This represents the exit status of the code.

It is an integer value; for example, an exit status 0 indicates successful termination, while any non-zero value signifies an error or abnormal termination.

The primary purpose of exit() in Python is to terminate the execution of a code immediately. Consider the following example:

print("Before exit()")
print("After exit()")

Output: The “Before exit()” will be printed, but “After exit()” will not, as the exit() function in Python interrupts the code’s execution.

Before exit()

After implementing the code in the PyCharm editor, refer to the screenshot below.

Python exit function

Python quit function

The quit() function in Python is also a built-in function used to exit a Python program. When the Python quit function is encountered in the system, terminates the execution of the code completely.

It should not be used in production code, and the quit function should only be used in the interpreter.

The quit() function does not accept any arguments. It has a simple syntax:


Here’s how you can use it:

for value in range(0,5):
    if value == 3:

If the value of “value” becomes “3,” the Python program is forced to quit, which will print the quit message.

Use quit() or Ctrl-Z plus Return to exit

Below is the screenshot mentioned after implementing the code in the Pycharm editor.

python quit

Python exit vs quit function

Let’s see some difference between the exit() and quit() functions in Python:

FeaturePython exit() functionPython quit() function
ImportedNo import requiredNo import required
Intended UseTypically, in codes and non-interactive environmentTypically, in interactive sessions such as Python shells or IPython.
Accepts ArgumentsOne optional argument as an integer called statusNo Arguments required
Control Over ExitMore control and flexibility over exit statusLess control and flexibility over exit status
Commonly UsedExit is used inside a loop in Python.Quit is usually used outside a loop in Python.
Python quit vs exit

Here is the pictorial representation of the Python exit() vs quit() fucntions.

Python quit vs exit

Python sys.exit() function

In Python, the sys.exit() function is used to terminate the execution of a code or program. It is a part of the sys module and provides more control and flexibility over exiting Python processes compared to quit() or exit().

The syntax of the Python sys.exit() function is as follows:

import sys


The sys.exit() function in Python accepts an optional integer argument representing the exit status, the same as the exit() command in Python.

For example:

import sys

Months = 12
if Months < 20:
    sys.exit("Months is less than 20")
    print("Months is not less than 20")

If the months are less than 20, the sys.exit() will exit the program as an exception occurred and print SystemExit with the argument.

You can refer to the below screenshot python sys.exit() function.

exit function in python

Python os.exit() function

The os._exit() function in Python is a low-level function provided by the os module. Unlike exit() or quit(), which raise an exception to exit the interpreter, os._exit() terminates the process without calling cleanup handlers or performing any other cleanup actions.


import os
for i in range(5):
    if i == 3:

After writing the above code (Python os.exit() function), the output will appear as a “ 0 1 2 “. Here, it will exit the program, if the value of ‘i’ equal to 3 then it will print the exit message.

You can refer to the below screenshot python os.exit() function.

python exit

Python raise SystemExit

The SystemExit is an exception raised in Python when the program is running and needs to be stopped.

Here is an instance where the SystemExit is raised in Python:

for i in range(8):
    if i == 5:
        raise SystemExit

After writing the above code (Python raise SystemExit), the output will appear as “ 0 1 2 3 4 “. Here, we will use this exception to raise an error. If the value of ‘i’ is equal to 5, it will exit the program and print the exit message.

You can refer to the screenshot below for Python raise SystemExit.

exit python

Program to stop code execution in Python

To stop code execution in Python, first, we have to import the sys module, and then we can call the exit() function to stop the program from running. We can also pass the string to the Python exit() method.

Here’s how you can do it:

import sys
my_list = []
if len(my_list) < 5:
  sys.exit('list length is less than 5')

Output: If you want to prevent it from running, if a certain condition is not met, then you can stop the execution. Here, the length of “my_list” is less than 5, so it stops the execution.

list length is less than 5

You can refer to the below screenshot program to stop code execution in python.

exit function python

Difference between exit() and sys.exit() in Python

Let’s see the Python exit function vs Python sys.exit() function in a tabular form:

FeaturePython exit functionPython sys.exit function
Module UsageBuilt-in functionRequires importing ‘sys’ module
Exception TypeRaises ‘SystemExit’Raises ‘SystemExit’
Syntaxexit([status])import sys

InvocationDirectly callableCalled from the sys module in Python
Python exit function vs Python sys.exit() function
  • exit() – If we use exit() in a code and run it in the shell, it shows a message asking whether I want to kill the program. The exit() is bad for production code because it relies on the site module.
  • sys.exit() – But sys.exit() is better because it closes the program and doesn’t ask. It is considered good to use in production code because the sys module will always be there.

The pictorial view of the Python exit function vs sys.exit() function is presented below:

sys.exit vs exit in python


After reading this article, I hope you got a clear picture of the Python exit function and the Python quit function and how they differ. I have taken some basic examples to explain these concepts.

Now, you must also know other topics like what the sys.exit() function is in Python, the os.exit() function, the difference between exit in Python and sys.exit(), etc, in detail.

You may also like to read: