22.4 C
New York
Wednesday, February 21, 2024
HomeProgrammingPythonHow to Exit Python Program

How to Exit Python Program [4 Methods]

In this tutorial, you will learn how to exit Python program. We have four different methods to terminate a Python program or code.

A Python program gets terminated as soon as the interpreter reaches the end of the file. However, we can also terminate a Python script using various exit commands. This tutorial will discuss how to exit a Python program and different ways to terminate it.

To exit a Python program, we will explore the following methods:

  1. Quit()
  2. Exit()
  3. Sys.exit()
  4. Os._exit()

How to Exit a Python Program or Terminate a Python Script

Os._exit(), Sys.exit(), Quit(), and Exit() have the same functionality as they raise the SystemExit exception. Once the Python interpreter encounters the SystemExit exception, it quits or terminates the Python program.

However, quite subtle differences exist in how these Python methods terminate the program.

Let’s see each of these Python exit commands to terminate a program.

Method 1: quit()

Quit() is an in-built Python function that you can use to terminate a Python program immediately. You don’t have to import any Python library to use the quit() function.

Also, you should not use this method in the production code or live environment where real-world users use the product, as it can end the program abruptly.

The Python interpreter quits the program whenever it encounters the quit() method.

Syntax:

quit()

quit()

Example:

Consider creating two nested tuples and displaying their items without using the quit() function.

In this case, the Python interpreter will execute both the print() statements, and you will see the output.

Also ReadPython For Loop Index With Examples

Code:

#crete a nested tuple with 5 integer elements in each of the two tuples
my_integer_tuple=((23,45,67,54,45),(233,455,627,514,459))

#display 
print(my_integer_tuple)


#crete a nested tuple with 5 integer elements in each of the two tuples
my_string_tuple=(("c","cpp","java","big-data","html"),("c","cpp","java","big-data","html"))

#display 
print(my_string_tuple)

Output:

((23, 45, 67, 54, 45), (233, 455, 627, 514, 459))
(('c', 'cpp', 'java', 'big-data', 'html'), ('c', 'cpp', 'java', 'big-data', 'html'))

Now, let’s use quit() after printing the first tuple. After that, it will terminate the program and not return the second tuple.

Code:

#crete a nested tuple with 5 integer elements in each of the two tuples
my_integer_tuple=((23,45,67,54,45),(233,455,627,514,459))

#display 
print(my_integer_tuple)

#eterminate the program
quit()

#crete a nested tuple with 5 integer elements in each of the two tuples
my_string_tuple=(("c","cpp","java","big-data","html"),("c","cpp","java","big-data","html"))

#display 
print(my_string_tuple)

Output:

((23, 45, 67, 54, 45), (233, 455, 627, 514, 459))

Also ReadHow to Reverse an Array In Python [Flip Array]

Method 2: exit()

Exit() is another in-built Python function similar to the quit() method that lets you terminate a program. Python interpreter exits the code as soon as it encounters the exit() method.

Syntax:

exit()

Example:

Consider we are creating two nested tuples and going to display without exit()

So, both the print() statements work and the output is displayed.

#crete a nested tuple with 5 integer elements in each of the two tuples
my_integer_tuple=((23,45,67,54,45),(233,455,627,514,459))

#display 
print(my_integer_tuple)


#crete a nested tuple with 5 integer elements in each of the two tuples
my_string_tuple=(("c","cpp","java","big-data","html"),("c","cpp","java","big-data","html"))

#display 
print(my_string_tuple)

Output:

((23, 45, 67, 54, 45), (233, 455, 627, 514, 459))
(('c', 'cpp', 'java', 'big-data', 'html'), ('c', 'cpp', 'java', 'big-data', 'html'))

Now, let’s use exit() after printing the first tuple. After that, it will terminate the program and not return the second tuple.

#create a nested tuple with 5 integer elements in each of the two tuples
my_integer_tuple=((23,45,67,54,45),(233,455,627,514,459))

#display 
print(my_integer_tuple)

#terminate the program
exit()

#crete a nested tuple with 5 integer elements in each of the two tuples
my_string_tuple=(("c","cpp","java","big-data","html"),("c","cpp","java","big-data","html"))

#display 
print(my_string_tuple)

Output:

((23, 45, 67, 54, 45), (233, 455, 627, 514, 459))

Method 3: sys.exit([arg])

Unlike exit() and quit() methods, sys.exit([arg]) is safe to use in the production code as the sys module is always available. It also raises the SystemExit exception.

The optional argument arg can be an integer giving the exit or any other object type. If the argument is zero, then the program termination is considered successful. You can also pass a string argument to the sys.exit() function.

Also ReadHow to Convert Binary to Decimal in Python [5 Methods]

Using the sys.exit() method, we import the sys module using the following statement.

import sys

Syntax:

sys.exit("statements")

After executing the statements, it will stop and return an error.

Example:

We will check whether the given number equals 100 without sys.exit().

#check the 100 is equal to 100 or not 
if(100==100): 
  print("Equal") 
else: 
  print("Not")

Output:

Equal

Now, place the sys.exit() under the if condition. It will raise an exception, and after that, it will display the result.

Code:

#import the sys module
import sys

#chekc the 100 is equal to 100 or not
if(100==100):
   sys.exit("Equal")
else:
  print("Not")

Output:

An exception has occurred, use %tb to see the full traceback.

SystemExit: Equal

Method 4: os._exit()

Os._exit() function is similar to the sys.exit() method. However, you can use it to exit any Python program with specified status without flushing stdio buffers, calling cleanup handlers, etc.

Usually, this method terminates child processes after the os.fork() system call. The standard way to exit a Python program is to use the sys.exit() method.

This Python exit function is also safe to use in the production environment as it doesn’t raise errors or exceptions.

Also ReadHow To Automate Google Search With Python

As the os._exit() method is available in the os module, we will first have to import the os module using the statement below.

import os

Syntax:

os._exit("statements")

After executing the above statement, the Python interpreter will stop the program and return nothing. Now, let’s understand this using an example.

Example:

We will check whether the given number equals 100 without os._exit().

Code:

#check the 100 is equal to 100 or not 
if(100==100): 
  print("Equal") 
else: 
  print("Not")

Output:

Equal

Now, place the os._exit() under the if condition, which will return nothing.

#import the os module
import os

#chekc the 100 is equal to 100 or not
if(100==100):
   os._exit(0)
else:
  print("Not")

Output:

No Output.

Wrapping Up

In this tutorial, you explored four methods to terminate a Python program. Depending upon your scenario and requirements, you may use any exit command or method to quit a Python program from executing.

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES