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.
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) –
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-
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-
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
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
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.
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python in 2020
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.