Skip to Content

4 Ways To End A Program In Python

4 Ways To End A Program In Python

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.

Syntax:

import sys

sys.exit(argument)

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.

quit() function:

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

Syntax: quit()

Its usage is explained in the following example:

 

for i in range(5)
   if i==3
       quit()
  print(i)

 

Output:

0

1

2

 

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

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.

Syntax: exit()

The usage of this function is explained in the following example:

 

for i in range(10)
   if i==6
       exit()
  print(i)

 

Output:

0

1

2

3

4

5

 

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

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)

 

Output:

0

1

2

3

 

When the value reaches 4 the program gets terminated. Also, this function is often used when a sudden termination of the program is required.

 

Conclusion

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!