exit() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The built-in Python procedures exit(), quit(), sys.exit(), and os. exit() are most frequently used to end a program. This article will discuss the uses of these built-in functions along with examples.

Syntax of exit() in Python

exit() Function

We can use the in-built exit() function to quit and come out of the execution loop of the program in Python.

Syntax:

exit() is defined in site module and it works only if the site module is imported so it should be used in the interpreter only.

quit() Function

quit() function is another in-built function, which is used to exit a python program. When the interpreter encounters the quit() function in the system, it terminates the execution of the program completely.

Syntax:

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

sys.exit() Function

sys.exit() is a built-in function in the Python sys module that allows us to end the execution of the program.

Syntax:

We can use the sys.exit() function whenever you want without worrying about code corruption.

os.exit() Function

The os. exit() function can terminate a process with a specific status without flushing stdio buffers or invoking cleanup handlers.

Syntax:

Following an os.fork() system call, this function is often utilized in a child process.

Parameters of exit() in Python

The exit() and quit() functions don't take any parameter as input.

In sys.exit() function, an integer indicating the exit or another kind of object can be used as the optional argument. The optional argument can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. It also accepts a text argument which is printed on the screen once the function is executed.

os.exit() function generally takes a numerical value as input that denotes the exit status. In general, os.exit(0) is used for successful termination.

Return Values of exit() in Python

exit() Function

If we use the exit() function and print it then it prints an exit message.

quit() Function

If we use the quit() function and print it then it prints a quit message

sys.exit() Function

The sys.exit() function if executed print the argument on the screen that was passed in it.

os.exit() Function

It does not return anything and exits the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.

Exceptions of exit() in Python

The sys.exit() function is responsible for throwing the SystemExit exception. To avoid being unintentionally caught by code that catches the exception, it inherits from BaseException rather than the exception.

This enables the exception to ascend and results in the interpreter quitting correctly. The Python interpreter terminates if the exception is not handled, but no stack traceback is displayed.

The same optional argument supplied to sys.exit() is accepted by the function Object() { [native code] } (). If the value is None, the exit status is zero; if it is another type (such as a string), the object's value is printed; and the exit status is one. If the value is an integer, it defines the system exit status (given to C's exit() function).

The cleanup handlers (the final clauses of try statements) can be executed, and a debugger can run a script without the risk of losing control. A call to sys.exit() is converted into an exception. If an immediate exit is positively required (for instance, in the child process following a call to os.fork(), the os. exit() function can be utilised.

Example of exit() in Python

exit() Function

After writing the above code (python exit() function), once you run the code it will just give us the exit message. Here, if the value of the “number” is less than 88 then the program is forced to exit, and it will print the exit message.

Output

quit() Function

After writing the above code (python exit() function), once you run the code it will just give us the exit message. Here, if the value of the “number” is less than 88 then the program is forced to exit, and it will print the exit message.

Output

sys.exit() Function

Output

After writing the above code (python sys. exit() function), the output will appear as a “ number is less than 88 “. Here, if the number are less than 88 then it will exit the program as an exception occurred and it will print SystemExit with the argument.

os.exit() Function

Output:

What is exit() in Python?

The are various methods that help us to exit from a python script or code. These functions are helpful when we need to handle an error or exception in the python code or we need to exit from the code if some conditions are not satisfied. exit(), quit(), sys.exit(), and os. exit() are most frequently used python functions to end a program.

The quit() and exit() are used interchangeably, and they don't accept any argument as the input. We can write quit() or exit() at any line of the program, and when that line gets executed, the program terminates.

Out of these functions, the sys.exit() function is preferred mostly. We can't use the exit() and quit() in production code, and the os.exit() function is for exceptional cases only, like the fork() system calls when we need to exit immediately from the code. Also, the os.exit() functions allow us to take an argument as input that is helpful in case we want to display it at the time of output to highlight the cause of exiting from the python program.

More Examples

Exit and Come Out of the Execution Loop of the Program in Python

After writing the above code (python exit() function), Ones you will print “ i ” then the output will appear as a10 20 30 “. Here, if the value of “i” becomes “4” then the program is forced to exit, and it will print the exit message.

Output

Quit and Come Out of the Execution Loop of the Program in Python

After writing the above code (python exit() function), Ones you will print “ i ” then the output will appear as a “ 10 20 30 “. Here, if the value of “i” becomes “4” then the program is forced to exit too, and it will print the exit message.

Output

Use of sys.exit() Function

After writing the above code (python exit() function), Ones you will print “ i ” then the output will appear as a “ 10 20 30 “. Here, if the value of “i” becomes “4” then the program is forced to exit, and it will print the exit message.

Output

Use of os._exit() Function

After writing the above code (python os.exit() function), the output will appear as a “ number is less than 8 “. Here, if the number is less than 8 then it will exit the program as an exception occurred and it will print the argument.

Output:

Conclusion

  • We can use the in-built exit() function to quit and come out of the execution loop of the program in Python.
  • When the interpreter encounters the quit() function in the system, it terminates the execution of the program completely.
  • The sys.exit() function if executed prints the argument on the screen that was passed in it and the program is terminated.
  • The os.exit() function can terminate a process with a specific status without flushing stdio buffers or invoking cleanup handlers.
  • sys.exit() is preferred mostly among all the exit functions, since the exit() and quit() functions cannot be used in production code and the os.exit() function is for special cases only like the fork() system calls when we need to exit immediately from the code.