Have you ever wondered how to end or execute a program in Python?
Don’t worry we got your back and will tell you the 4 different ways how you can end a program in Python.
We will list 4 easy functions for you to terminate a Python program. These code snippets are so simple so anyone can understand and implement them straight away.
Let’s get into the different ways on how to end a program in Python,
4 Ways to end a program in Python
1. sys.exit() Function
There is an exit function with the name sys.exit() in the Python sys module that can be used whenever a user wants to exit a program.
It just requires importing the sys module in the code and the user can freely use this function to exit the program anytime.
The arguments to be passed to this function are optional, if it’s passed with an integer then a 0 value will ensure successful termination otherwise if it’s a non-zero value it will result in an unusual termination of the program.
Also, the value of the integer requires being in 0-127 for most of the systems. One thing to be noted here is that it is implemented by raising the exception of SystemExit also it is possible to stop the program by terminating at an outer level.
Since this exit function just raises an exception, so it will not stop from terminating when called from the main and will easily exit the program.
A specific way of using this is explained in the following example:
import sys experience = input(ìEnter your age:î) If experience < 5: sys.exit(ìYou are not eligibleî) else: print(ìYes! You are eligible!î)
The output here depends upon the value; if the value is less than 5 it will just print:
You are not eligible
On the screen and exit the program right away. But if the value is greater than 5 it will print:
Yes! You are eligible!
Note: This exit function will only work when called from the main thread and no other thread is running along with it.
The quit function is also one of the best methods to terminate a program. It’s a built-in function that requires no library to be imported for working.
2. quit() Function
Its usage is explained in the following example:
for i in range(5) if i==3 quit() print(i)
The program gets terminated using the quit function when the value of i reaches 3. The quit function actually raises the exception of the SystemExit also it works only if the site module is introduced.
Note: It is most preferred that this function should not be used in real-world programs and only be used in interpreter code.
3. exit() Function
This exit() function works in the same way as quit() does. So it also works only when the site module is imported and it is preferred not to use it in real world programs. It should only be used in interpreter only.
The usage of this function is explained in the following example:
for i in range(10) if i==6 exit() print(i)
When the value reaches 6 the program gets terminated. It’s almost similar to quit() and was introduced as it seems a bit more user-friendly.
4. os._exit(arg) Function
This function is also one of the simplest methods to terminate a Python program. This function requires the os module to be imported.
Whenever there is a need of terminating the program with a stated message or status without calling fflush which will flush the buffered output which is declared in stdio, clean-up handler which just automatically gets executed after the block out of a thread, and similar things like this.
Syntax: import os os._exit(argument)
import os for i in range(10) if i==4 os.exit(os.EX_OK) # os.EX_OK means zero and it will terminate successfully with 0 value print(i)
When the value reaches 4 the program gets terminated. Also, this function is often used when a sudden termination of the program is required.
Among all these exit function sys.exit() is considered to be one of the most useful ones.
Since it can be used in any real-world program while quit() and exit() can’t be used there. Also, os._exit() is used only in some specific situations when an immediate exit is required.
Comment below if you have any kind of question or queries. Till then, happy programming!
Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!