Exit Commands in Python

Python provides a wide range of built-in functions and commands to carry out a specific operation. For instance, the “exit command” is one of the inbuilt functions of Python that enables exit at any time from the Python script. The developers tend to make use of the “exit commands” to stop the further execution of the code when a specification condition is met. Python provides a list of numerous exit commands that will be explained in this write-up. The list of the exit commands is provided below:

Let’s start with the first one:

Using quit() Function

In Python, the inbuilt function “quit()” is used to exit a program. You can use the “quit()” function without importing any libraries. When the “quit()” function is called in the program, the system terminates the execution and shows the result on the terminal screen.

Let’s understand the quit() function via the following code:

Code

for value in range(0,8):
    if value == 5:
        print(quit)
        quit()
    print(value)

In the above code:

  • The “for loop” is applied with a range function (0, 8).
  • The “if” statement is used within a loop that will keep printing the value until the limit value “5” is reached.
  • When the value becomes equal to “5”, the program is forced to quit.

Note The “print()” statement with quit argument is optional to use before the “quit()” function.

Output

In the above output, the quit() function terminates the program when the value becomes “5”.

Using exit() Function

In Python, we have an alternative function named “exit() function”. This function is similar to the “quit()” function and has very simple and basic syntax. When the exit() function is called the python script will be terminated and discontinued. Now let’s understand it with an example given below:

Code

#using exit() function
for x in range(1,8):
    print(x*4)
    exit()

In the above code:

  • The “for loop” is applied on a range (1,8).
  • The “print()” function is used to display the answer to the multiplication of each number with the range (1, 8).
  • The “exit()” function is used at the end to terminate the program.

Output

The “exit()” function terminates the program after completing only the first iteration.

Using sys.exit() Function

The “sys.exit()” is an in-built function used to finish a program. To use “sys.exit()”, we need to import the inbuilt module “sys”. We can use this function in any Python script at any place in the code without being worried about code errors.

Let’s see an example given below.

Code

import sys
value_1 = 5
if value_1!=10:
    sys.exit("The Given value don't match")
    print('This line will not execute')
else:
    print("Value Is Equal")

In the above code:

  • The “sys” module is imported.
  • The variable named “value_1” is initialized.
  • If the variable value is equal to “10” then else the statement print “Value Is Equal ” and if the variable value is not equal then the “sys.exit” function exit the program and print the message “The Given value don’t match”.The second print statement does not execute because the program is already terminated.

Output

The above output shows that the input variable value was not matched and the “sys.exit()” function exited the program.

Using os_exit() Function

In Python, there is an “os module”  which is mostly used for the functionality of the operating system. We used the “os.exit()” function in the Python script to terminate the program. To use the “os.exit()” function, the “os” module needs to be imported at the beginning of the Python program.

Let’s understand this by an example given below.

Code

import os
for number in range(10):
    if number == 7:
        print(exit)
        os._exit(0)
    print(number) 

In the above code:

  • The “os” module is imported.
  • The for loop is applied on “range(10)”.
  • When the value becomes equal to “7”, then the print statement prints the exit command error on the console, and the “os.exit()” function terminates the program.
  • The values until “7” will be printed on the console.

Note The “print()” statement with exit argument is optional to use before the “os.exit()” function.

Output

In the above output, the range value is printed from “0-6”. The time value reaches “7”, and the “os.exit()” function closes the program.

Using SystemExit

The “SystemExit” is used to terminate the program when an error occurs. An error or exception is found during the execution time of the code. We use “raise” keywords before the “SystemExit” command to raise an exception. The SystemExit will not be caught by the code during execution because this function is inherited from “BaseException”. In general, the “SystemExit” is just an exception that will be raised during the execution of the program.

Let’s understand the “SystemExit” Command with an example given below.

Code

for numbers in range(10):
    if numbers == 4:
        print(exit)
        raise SystemExit
    print(numbers)

In the above code:

  • The for loop applies on “range(10)”.
  • When the value becomes equal to “4” then “SystemExit” raises and the program exits immediately.
  • The “print()” function prints the number before the system encounters “SystemExit”.

Output

The “SystemExit” raises when the value reaches “4” and thus the values are not printed further.

That’s all from this guide of exit commands.

Conclusion

In Python, we used several “Exit” commands like “quit()”, ”exit()”, “sys.exit()”, “os_exit()” and “SystemExit” to terminate the program. The “exit()” and “quit()” will not be used in production code (code that is used by the Targeted audience ) and only works when they will be imported using the site module. Most of the functions are inbuilt and have simple syntax to implement in the code. The “sys.exit()” is the most preferred method among all of the others. In this post, we have demonstrated various exit commands in Python.