In this tutorial, I will discuss How to exit a Python program in different ways.
Use the existing Python functions such as quit(), sys.exit(), and exit() in your Python program to terminate the program. This function will terminate the currently running program, and the interpreter will be stopped from the line number where this function was called in the first place. Now let’s see the code examples for these function usage.
1. Using sys.exit() Function To Exit A Python Program
The sys module of the Python programming language contains an in-built function that allows you to exit a program and exit the execution process — the sys.exit() function.
The sys.exit() function can be called at any point in time without having to worry about the code becoming corrupted in the process.
Let us see in the below code example usage of sys.exit() to exit a program in Python.
#importing Sys Library import sys #Asking Input From User inputUser = int(input("Enter the Number 10.\n")) if inputUser!=10: sys.exit("Incorrect Number Entered.") else: sys.exit("Match Found.")
Enter the Number 10. 10 Match Found. Enter the Number 10. 5 Incorrect Number Entered.
As you can see in the above code when I entered the matching number 10, the system exited with a message indicating that a match was found, but when I entered the incorrect number, the system exited with a message indicating that an incorrect number was entered.
2. Using quit() Function
Exiting a Python program can be accomplished through the use of the quit() function provided by the Python functions library.
It is necessary to note that once the system encounters the quit() function, the program’s execution is completely terminated.
Let us see in the below code example the usage of the quit() function to exit a program in Python.
#Running A Loop for num in range(1,10): #Printing All The Number print(num) #If Number is equal to 5 Quit #The Program if num==5: print("Quitting...") quit()
1 2 3 4 5 Quitting...
As you can see in the preceding code when the for loop reaches the number 5, it is forced to exit and the interpreter is prevented from executing the code any further.
3. Using exit() Function
In addition to the techniques described above, we can use the Python language’s built-in exit() function to exit and exit the program’s execution loop when running the program.
The exit() function can be thought of as an alternative to the quit() function, which allows us to terminate the execution of a program by calling the function.
Let us see in the below code example the usage of the exit() function.
#Running A Loop for num in range(1,10): #Printing All The Number print(num) #If Number is equal to 3 Exit #The Program if num==3: print("Exit The Program...") exit()
1 2 3 Exit The Program...
As you can see, the exit() function works similarly to Python’s quit() function. As soon as it reaches the number count of 3, it stops the function from being executed further.
I hope you understood how to exit a Python program in 3 ways. Neither the exit() nor the quit() functions are permitted to be used in operational or production code. Because these two functions can only be implemented if the site module is imported, it is necessary to import it.
As a result, of the methods listed above, the sys.exit() method is the one that is most frequently used. If you have any questions, let me know in the comment section. Also, let me know if you know any better method than the one discussed above.