While writing a program in python, you might need to end a program on several occasions after a condition is met. In this article, we will discuss different ways to terminate a program in python.
Terminate a Program Using the quit() Function
The quit()
function is an inbuilt function that you can use to terminate a program in python. When the quit()
function is executed, it raises the SystemExit
exception. This results in the termination of the program. You can observe this in the following example.
number = 10
if number >= 10:
print("The number is:", number)
quit()
print("This line will not be printed")
Output:
The number is: 10
The quit()
function is defined in the site module and it works only if the module is installed. Therefore, I would recommend you not to use this function in real-world applications. However, you can use the quit()
function while writing smaller programs.
Terminate a Program Using the exit() Function
The exit()
function is also defined in the site module. It works in a similar way to the quit()
function and terminates the program when executed as shown in the following example.
number = 10
if number >= 10:
print("The number is:", number)
exit()
print("This line will not be printed")
Output:
The number is: 10
Again, I would advise you to not use this function in the production environment due to its module dependency.
Terminate a Program Using the sys.exit() Function
The sys
module is included in the core python installation. You can import the sys
module as follows.
import sys
To terminate a program using the sys module, we will use the sys.exit() function. The sys.exit() function when executed, takes a string message as the optional input argument and prints the message before terminating the program as shown below.
import sys
number = 10
if number >= 10:
print("The number is:", number)
sys.exit()
print("This line will not be printed")
Output:
The number is: 10
However, passing any input argument other than 0 means that the program has an abnormal termination.
Using the SystemExit exception
All the above functions raise the SystemExit
exception to terminate the program. So, we can also raise the SystemExit
program explicitly to terminate a program in python as follows.
import sys
number = 10
if number >= 10:
print("The number is:", number)
raise SystemExit
print("This line will not be printed")
Output:
The number is: 10
Conclusion
In this article, we have seen different ways to terminate a program in python. The best way to terminate a program is to use the sys.exit()
function as discussed by many people on StackOverflow. So, I would suggest you use this function to terminate a program. To learn more about python programming, you can read this article on list comprehension. You might also like this article on string concatenation in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.