We often want to exit from the program before the interpreter does so, and for this purpose, we have Python exit function. Apart from the exit, we also have some functions like quit(), sys.exit() and os._exit(). Let’s learn 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 that is not required. So, let’s understand what functions can be used below in 4 ways –
- Python Exit()
- quit()
- Sys.exit() Function
- 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 is why the error occurred.
If we perform print(exit) –
Output-
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.
ages=[19,45,12,78]
for age in ages:
if age < 18:
print(age,"not allowed")
exit()
else:
print(age,"allowed")
Output-
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 do you think 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 the marks list-
marks=[78,89,92,"ashwini",56]
for i in marks:
if type(i) != int :
print("Oops!! Encountered a non-int value:",i)
quit()
Output-
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")
else:
print("Voting allowed")
Output- 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:
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Conclusion
The exit function is useful 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!
I want to end the code, not the shell.
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.
Hi,
Thank you for your article which is informative and helpful.
You suggested using sys. exit() in production. I ran your code of sys.exit() and I got the message: warn(“To exit: use ‘exit’, ‘quit’, or Ctrl-D.”, stacklevel=1)
I used Jupyter to run the code. My python version is 3.7.4.
Am I missing something?
Sophia
Yes, there is a difference when you use IPython Notebook (Jupyter). sys.exit() only raises the SystemExit exception. Normally, Python would stop executing the program when this error is raised, but IPython keeps running even after this which causes a Traceback to this Exception :(.
There is no correct way of closing the Jupyter code programmatically. The best way is to raise an exception to close the block.
Hope this helps!
Regards,
Pratik