Exit program in python
Exit program in python
The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback is printed. In the uncaught or the interpreter exit case, we can catch the exception to intercept early exit and perform clean-up activities.
A python program is always executed from top to bottom, and the execution normally exits when the interpreter reaches the end of the file. We may also stop the execution anytime by calling the exit program explicitly with the built-in exit functions.
quit()
We cannot use this function in production code (the code is used by the intended audience in a real-world situation) because it works only if the site module is imported. It should only be used in the interpreter. This function raises the SystemExit exception. If we print it, we will get a message.
The following is a source code to use the quit() function:
#Python program to demonstrate quit() for i in range(8): # If i becomes 5 then the program quits if i == 5: # prints the quit message p rint(quit) quit() print(i)
Output
Here is the output:

Exit()
Exit() function return SystemExit exception. It is defined in site.py and works only if the site module is imported so that we used it in the interpreter only. This function behavior is the same as quit(), but it makes python more user-friendly.
The following is a source code to use the exit() function:
#Python program to demonstrate exit() for i in range(5): #if the value of i becomes 3 then program is forced to exit if i ==3: print(exit) exit() print(i)
Output
Here is the output:

sys.exit(argument)
The sys.exit() is considered good to be used in production code as compared to quit() and exit() function. It is an inbuilt function which always contains in the sys module.
#Python program to demonstrate exit() import sys x = 150 if x != 100: sys.exit("Values do not match") else: print("Validation of values completed!!")
Output
Here is the output:
