4 Ways of Exiting the Program with Python Exit Function

There are many times when we want to exit from the program before the interpreter does so, and for this purpose, we have python exit function. Apart from exit we also have some functions like quit(), sys.exit() and os._exit(). Let’s learned about each of their advantages and disadvantages. 

During a simple execution of the program (without the use of the functions mentioned above), when the interpreter reaches the end of the program/script, it exits from the program. But when we use functions like exit and quit, it gets out automatically at that time. 

Working with Python Exit Functions 

 Sometimes we need the program to stop before the interpreter reaches the end of the script, like if we encounter something which is not required. So, let’s understand what functions can be used below in 4 ways –

  1. Python Exit()
  2. quit()
  3. Sys.exit() Function
  4. os._exit Function

1. Python Exit()

This function can only be implemented when the site.py module is there (it comes preinstalled with Python), and that is why it should not be used in the production environment. It should only be used with the interpreter.  

In the background, the python exit function uses a SystemExit Exception. It means that when the interpreter encounters the exit(), it gives the SystemExit exception. Also, it does not print the stack trace, which means why the error occurred.  

If we perform print(exit) – 

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

Following is the code for exiting the program if we encounter a voter with age less than 18. 

for age in ages: 
    if age < 18: 
        print(age,"not allowed") 
python exit
19 allowed 
45 allowed 
12 not allowed 

If we run the program in ipython the output is- 

2. Python exit using quit()

This function works exactly as the exit(). There is not a single difference. It is to make the language more user-friendly. Just think, you are a newbie to the python language, what function you will think of should be used to exit the program? Exit or quit, right? This is what makes Python an easy to use language. Like the python exit function, the python quit() does not leave any stack trace and should not be used in real life.  

Suppose, we want to exit the program when we encounter a name in marks list- 

for i in marks: 
   if type(i) != int : 
        print("Oops!! Encountered a non-int value:",i) 
python exit
Oops!! Encountered a non-int value: ashwini 

3. Sys.exit() Function in Python

This function is beneficial and can be used in the real world or production environment because it is a function of the sys module available everywhere. We should use this function for controlling the terminal a=when we have big files.  

import sys 
age = 15 
if age < 18: 
    sys.exit("Voting not allowed because the age is less than 18") 
    print("Voting allowed") 
Voting not allowed because the age is less than 18 

4. os._exit Function In Python

This function calls the C function =_exit() which terminates the program immediately. Also, this statement “can never return”. 

Difference between exit(0) and exit(1) 

The main difference between exit(0) and exit(1) is that exit(0) represents success with any errors and exit(1) represents failure.

Must Read:


The exit function is a useful function when we want to exit from our program without the interpreter reaching the end of the program. Some of the functions used are python exit function, quit(), sys.exit(), os._exit(). We should use these functions according to our needs.

Try to run the programs on your side and let me know if you have any queries.

Happy Coding!

2.3 3 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Star wars Jedi Sebastien E.
Star wars Jedi Sebastien E.
3 months ago

I want to end the code, not the shell.

Pratik Kinage
Pratik Kinage
3 months ago

May I know what exactly are you looking for?
exit() and sys.exit() both exit your code and don’t have to do anything with the shell. Are you using python IDLE to run python? If yes, in that case, it will exit from the window.