Simple 5 ways to exit a Python Progam

In this post, we are going to understand 5 Simple ways to exit a Python Progam with code examples that include quit(), sys.exit(), exit(), and many more. We will learn each of these ways with the help of detail code examples.

1. Using quit() function


The Python’s inbuilt quit() function can be used to exit a program in Python. As soon as the program encounters quit() function it terminates the program.

This function is not used in production and is only used in the interpreter programs or development.

Code Example to Exit a Program

for x in range(1,5):
    print('value of x:',x)
    quit()

Output

value of x: 1

2. Using sys.exit() function


To exit a Python program we can use the sys module’s exit() function. This is a cross-platform function. sys.exit() is considered the best way to terminate a program. Because it does not raise an exception while executing.

Syntax

system.exit(agrument)

Parameter

Argument: It has optional parameter arguments that can be zero or another type of object. In the case of integer datatype zero is considered successful termination of the program.

Code example to Exit Program

import sys 

for x in range(1,5):
    print('value of x:',x)
    sys.exit("Program Terminated:")

Output

value of x: 1
SystemExit: Program Terminated:

sytem.exit() with if condition

The system.exit() function can be used with if condition when we have to terminate a program based on conditions.

Code example to exit a Program with if condition

In this example, the value of x is -1 which means it’s less than 0. So the condition is TRUE and if block is executed and as soon as it encounter the sys.exit() function it will terminate the process execution.

import sys
x= -1
for x in range(-1,5):
 if(x<0):
     sys.exit(f'Progarm Terminated at value of x:{x}')
 else :
  print('Value is not less than 0')


Output

SystemExit: Progarm is exit at value of x:-1

3. Using exit() function


The Python’s inbuilt exit() function can be used to exit a script or the program.

Code example to exit a Program

In this example, the first iteration of for loop has the value of x=1, and as soon as the program encounters the exit() function it terminates the program execution. So as you can see the loop ran only once before we hit the exit() function.

for x in range(1,5):
    print('value of x:',x)
    exit()

Output

value of x: 1

4. Using os.exit() function


The OS module exit() function can also be used to terminate the program in Python with the defined status.

Code Example to terminate a Program

In this code example, the value of x is 100 which makes the if condition TRUE. We are printing the exit message which it displays on the console. The next statement is os._exit() to terminate the program execution. As soon the os._exit(0) is executed it will terminate the program.

Program Example

import os

marks = 100
if marks  == 100:
        print(marks)
        print(exit)
        os._exit(0)
        

else :
    print("Value is not 100")

Output

100
Use exit() or Ctrl-Z plus Return to exit

5. Using Python raise SystemExit


When a running program needs to stop the SystemExit exception is raised. So we can use this to exit from the Program,

Program Example

In this example we are checking if the value is 12, we are printing the exit message, and by using raise SystemExit exit the program.

for val in range(10,20):
    print(val)   
    if val == 12:   
        print(exit)
        raise SystemExit
    

Output

10
11
12
Use exit() or Ctrl-Z plus Return to exit
    

Summary :

In this post we have explored 5 Simple ways to exit a Python Progam with code example that includes quit(),sys.exit(),exit(),os.exit() function, and Python raise SystemExit.