Knowing how to end a program is important and can be handy in various scenarios. For instance, if you’re creating a simple game, you’re able to quit on specific user input or when a certain condition is met. Discover the differences between the three ways of stopping a Python program.

1. Using quit() or exit()

One of the simplest ways of ending a Python program is to use either of the built-in methods,quit()orexit(). When you call eitherquit()orexit(), the program will terminate.

Thequit()andexit()functions do exactly the same thing, it’s just a matter of personal preference which you use.

Python code output screenshot

Here’s how you can use the quit() function to end a Python program:

The sample program above will print integers from 0 to 8 on the console. Oncenumis 9, the program exits. You can also use thequit()command to exit the Python Integrated Development and Learning Environment (IDLE), which allows you torun Python scripts interactively.

Python code output screenshot

Note that bothquit()andexit()rely on thesitemodule so you shouldn’t use them in production environments. The next method,sys.exit(), is a better option.

2. Using sys.exit()

When you callsys.exit()in your program, Python raises aSystemExitexception. It accepts an optional argument to specify an integer exit code (0 by default). You can also pass an object which will result in an exit code of 1 and will print the object’s string value to standard error.

The output of the program will be as follows:

Like thequitandexitmethods,sys.exit()also raises aSystemExitexception. This means that you can callsys.exit()orraise SystemExit(). Both accept an optional argument.

You should learn aboutthe most important Python functionsif you don’t understand how the code above works.

3. Using os._exit()

The third way of exiting a program is the specialos._exit()method. You can pass it an optional argument to specify an exit status code.os._exit()comes from theosmodule and terminates the process immediately without performing the normal cleanup activities that occur when a Python program exits.

Because this function exits without performing normal cleanup, you should only use it in special instances. According to thePython documentation, a good example is in a child process after a fork (a new process created usingos.fork()).

Here’s an example of how to use theos._exit()function to end a program:

The code above will output the following:

Which Method Should You Use to Terminate a Program in Python?

Python provides various methods to end a program. However, most of these methods do the same thing. This should reduce the burden of decision-making if you need to add an exit mechanism to your program whenlearning Python.

In summary,sys.exit()should be your go-to method for ending a program in Python. It raises aSystemExitexception with an optional exit code.

Thequit()andexit()methods are more appropriate when using the Python REPL. You should avoid them in production because they depend on thesitemodule, which may not always be available. You should rarely need to useos._exit(), unless you’re working with code that forks processes.