Python how to exit program – How to Exit a Program in Python- 3 Easy Methods

Python how to exit program: We can Exit a Python program using various methods/functions. Let us see the 3 Easy methods among them:

  • Using quit() Method
  • Using sys.exit() Method
  • Using exit() Method

The functions quit(), exit(), sys.exit(), and os. exit() have nearly identical behavior in that they raise the SystemExit exception, which causes the Python interpreter to exit with no stack traceback.
We can intercept early exits and conduct cleanup tasks if the exception is caught; otherwise, the interpreter quits normally.

When we launch a Python program, we simply execute all of the code in the file, from top to bottom. Scripts generally exit when the interpreter reaches the end of the file, but we can also use the built-in exit routines to force the program to exit.

1)Using quit() Method

Exit program in python: To exit a Python application, utilize the built-in quit() function provided by the Python functions.

When the system encounters the quit() function, the program’s execution is terminated completely.

It is only functional if the site module is imported, hence it should not be used in production code. Production code denotes that the code is being utilized in a real-world setting by the intended audience. This function should be used exclusively in the interpreter.

It throws the SystemExit exception in the background. If you print it, it will display a message.

Syntax:

quit()

Example

# Loop from 1 to 15 numbers using the for loop
for itr in range(1, 15):
    # Inside the for loop, print the value of iterator value multiplied with 5
    print(itr*5)
    # quit or exit the program
    quit()

Output:

5

Here, the interpreter finds the quit() method and stops the program after the first iteration of the for loop.

2)Using sys.exit() Method

Python 3 exit: The Python sys module includes an in-built function sys.exit() function for exiting the code and exiting the execution process.

The sys.exit() function can be called at any point in time without fear of causing code corruption.

Unlike quit() and exit(), sys.exit() is suitable for use in production programmes because the sys module is always available. The argument here is Optional and can be a number indicating the exit or another form of an object. If it’s an integer, zero means “successful termination.”

Note: It should be noted that a string can also be given to the sys.exit() method.

Syntax:

sys.exit(argument)

Example

Approach:

  • Import sys module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Check if the given number is even or not using the if conditional statement.
  • If it is true, then print some random text and exit the code using the sys.exit() function.
  • Else print “It is an odd number”.
  • The Exit of the Program.

Below is the implementation:

# Import sys module using the import keyword
import sys
# Give the number as static input and store it in a variable.
gvn_numb = 24
# Check if the given number is even or not using the if conditional statement
if gvn_numb % 2 == 0:
    # If it is true, then print some random text and exit the code using the
    # sys.exit() function
    sys.exit("It is an even number.Exit the code")
else:
    # Else print "It is an odd number"
    print("It is an odd number")

Output:

It is an even number.Exit the code

3)Using exit() Method

Quit function python: Aside from the strategies outlined above, we can utilize Python’s built-in exit() function to leave the program’s execution loop.

exit() is defined in site.py and only functions if the site module is imported, therefore it should only be used in the interpreter. It functions as a synonym for quit() in order to make Python more user-friendly. When printed, it, too, conveys a message:

Use exit() or Ctrl-D (i.e. EOF) to exit

Syntax:

exit()

Example

# Loop from 1 to 15 numbers using the for loop
for itr in range(1, 15):
    # Inside the for loop, print the value of iterator value added with 7
    print(itr+7)
    # Exit the program using the exit() method
    exit()

Output:

8

The exit() function can be thought of as an alternative to the quit() function, which allows us to terminate the program’s execution.