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 Python program and different ways to terminate it.
To exit a Python program, we will explore the following methods:
- Quit()
- Exit()
- Sys.exit()
- Os._exit()
Also Read: How to Convert Python Tuples to Lists
How to Exit Python Program or Terminate a Python Script
Os._exit(), Sys.exit(), Quit(), and Exit() have same functionality as they raise the SystemExit exception. Once the Python interpreter encounters the SystemExit exception, it quits or terminates the Python program.
However, there are quite subtle differences in how these Python methods terminate the program.
Let’s see each of these Python exit commands to terminate a program.
Also Read: How to Handle String Index Out of Range Error In Python
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 are using 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 we are creating two nested tuples and going to display 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 Read: Python 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 Read: How 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 exit 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))
Also Read: How to Check If Dict Has Key in Python [5 Methods]
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 Read: How to Convert Binary to Decimal in Python [5 Methods]
To use the sys.exit() method, we first have to 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 or not 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 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
Also Read: Python Program To Display Fibonacci Series Up To N Terms
Method 4: os._exit()
Os._exit() functions is pretty similar to 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 any error or exception.
Also Read: How 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 or not 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, and it 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")
Also Read: How To Use Python For Browser Games Development?
Output:
No Output.
Wrapping Up
In this tutorial, you explored four different 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. Subscribe to our newsletter and receive all the Python tutorials straight to your inbox.
Next Read:
- 7 Best Python IDE for Windows [Code Editors]
- How To Create Keylogger In Python
- How To Make A Simple Python 3 Calculator Using Functions
- 10 Best Python Libraries for Image Processing
- Python 3 Program To Find The Sum of An Array
- Dictionary In Python 3 | Learn With Examples
- Learn Python Online With These 12 Best Free Websites
- Python 3 Program To Solve A Quadratic Equation
- Python 3 Program to Find the Square Root of A Number
- 35 Funny And Best Python Programming Memes
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!