All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Exit A Program In Python

Last Updated : Mar 11, 2024

How To Exit A Program In Python

In this tutorial we will show you the solution of how to exit a program in python, Python is one of the most versatile and dynamic programming languages used out there.

It is the most commonly used programming language nowadays, and for good reason.

Python gives a programmer the option and the allowance to exit a Python program whenever he/she wants. Let’s get started.

Step By Step Guide On How To Exit A Program In Python :-

When we run a program in Python, we simply execute all the code in the file, from top to bottom.

Scripts normally exit when the interpreter reaches the end of the file, but we may also call for the program to exit explicitly with the built-in exit functions.

Some of them are listed below that you can use :

  • Using the exit() function
  • Using the quit() function
  • Using the sys.exit() function
  • Using the raise SystemExit command
import sys
import time
#exit()
for a in range(1,5):
    print(a+2)
    exit()
#quit()
for a in range(1,5):
    print(a+4)
    quit()
#sys.exit()
a = 5
if a != 12:
    sys.exit("Values don't match unfortunately")
else:
    print("They match")
#KeyboardInterrupt
try:
    x=0
    while 1==1:
        x=x+2
        print(x)
        time.sleep(1)
except KeyboardInterrupt:
    print("Raising SystemExit")
    raise SystemExit
  1. In the first two lines there are two import statements that are importing two different modules named “sys” and “time” that will be helpful in this article.
  2. In the next line there is a comment saying “exit()” which is the first approach you can use. If you want to execute this code, you have to execute it in bits and pieces. You have to comment out all the other code except for the particular method you want to execute.
  3. In the next line, there is a for loop that will iterate through 1 to 5 and has an iterative variable named “a”. In the next line, the value of the variable “a” will be incremented by 2 and displayed in the output window. After that, the exit() function will be called and the program will be exited.
  4. In the next line there is a comment saying “quit()”, which is the next method you can use to exit a Python program. In the next line, there is a for loop, the same as above, with no difference.
  5. The value of the variable "a" will be incremented by 4 and displayed in the output window in the following line. After that, the quit() will be executed and the program will exit.
  6. In the next line there is a comment saying “sys.exit()”, which is the next approach in this article. In the next line, there is a variable named “a” that has a value of “5” assigned to it.
  7. In the next line, there is an if condition that checks whether the value of the variable “a” is not equal to 12 or not. If the condition is true, then the “sys.exit()” function will be executed with the message “Values don't match unfortunately” displayed on the output window and the program will exit.
  8. If the condition is false, then it will display the message “They match” in the output window.
  9. In the next line, there is another comment saying “KeyboardInterrupt”. That is the next approach you can use. In the next line, there is a try block, that will be executed first. In the try block there is a variable initialization named x with a value of 0.
  10. In the next line, there is a while loop that is an infinite loop. In the next line, the value of the variable “x” will be incremented by 2 in every iteration and will be displayed in the output window with the help of the print statement.
  11. In the next line there is a “time.sleep()” function that will put the program to sleep for 1 second. The values of the variables will be displayed after 1 second of delay. In the next line, there is an “except” block that lets us handle any errors. It will get executed when you press “CTRL + C” on your keyboard while the program is being executed.
  12. This will exit the program with the message saying “Raising SystemExit”, which is displayed using the print statement in the next line. The last line is the raise keyword’s main function is to raise an exception. The kind of error you want to raise can be defined.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now exit a program in Python.

The best and simplest way is to use the quit() function but you can apply any of the methods mentioned above.

I hope this tutorial on how to exit a program in python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪