×

Exit program in python

Exit program in python

The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback is printed. In the uncaught or the interpreter exit case, we can catch the exception to intercept early exit and perform clean-up activities.

A python program is always executed from top to bottom, and the execution normally exits when the interpreter reaches the end of the file. We may also stop the execution anytime by calling the exit program explicitly with the built-in exit functions.

quit()

We cannot use this function in production code (the code is used by the intended audience in a real-world situation) because it works only if the site module is imported. It should only be used in the interpreter. This function raises the SystemExit exception. If we print it, we will get a message.

The following is a source code to use the quit() function:

#Python program to demonstrate quit()   
for i in range(8):           
# If i becomes 5 then the program quits     
if i == 5:         
# prints the quit message         p
rint(quit)        
 quit()     
print(i) 

Output

Here is the output:

Exit program in python

Exit()

Exit() function return SystemExit exception. It is defined in site.py and works only if the site module is imported so that we used it in the interpreter only. This function behavior is the same as quit(), but it makes python more user-friendly.

The following is a source code to use the exit() function:

 #Python program to demonstrate exit()   
for i in  range(5):    
#if the value of i becomes 3 then program is forced to exit     
if i ==3:   
print(exit)                                                                                             exit()       
print(i)  

Output

Here is the output:

Exit program in python

sys.exit(argument)

The sys.exit() is considered good to be used in production code as compared to quit() and exit() function. It is an inbuilt function which always contains in the sys module.

#Python program to demonstrate exit() 
import sys x = 150 if x != 100:    
 sys.exit("Values do not match")  
else:    
 print("Validation of values completed!!")

Output

Here is the output:

Exit program in python

Related Topics

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Python Console

Console in Python is referred as the command line Interpreter- (CLI) and also knows as Shell and it functions as taking input from the human user and interpreting it through...

6 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Sort a dataframe based on a column in Python

Sorting the dataframe based on a column requires pandas which is An open-source library called Python Pandas is described as offering high-performance data processing in Python. For both professionals and...

4 minutes read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Falcon Python

Introduction of Python Python is the fastest and the smartest programming language and it is an object oriented language. Python has libraries that can be importedeasily and perform many operations; to...

3 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

How to Install Python In Windows

How To Install Python In Windows? Python is a language that every aspirant developer looks forward to. One thing we must keep in our mind is how to begin our hands-on...

3 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.