×

Python exit commands

exit(), quit(), sys.exit(), os._exit()

In this tutorial, we will study exit commands used in the Python programming language.

Python is undoubtedly the choice of programmer and this is because of the in-built functions provided by it. Exit command is one of them. An exit command is used in python to make it easier and more manageable. These are all in-built functions provided to ease our work. Python commands exit(), quit(), sys.exit(), and os._exit() perform similar functionality that is to raise an exception named SystemExit exception by the utility of which python dismisses or closes the program completely.

quit () command –

This command is an in-built function in python to get out of the python program. The program gets dismissed once the quit () function is encountered.

This function should only be used in the interpreter and not in the production code. It raises the SystemExit exception in the background and we can also see it by printing it.

Now, let us understand an example to know the concept better.

Example 1: The following figure shows an implementation of the quit () command.

Python exit commands
Python exit commands

Fig 2: Figure showing the output of the above implemented a quit function

Explanation: In the above code, the loop runs till 20 but we get the output from 1 to 9 only.  This is so because at i = 10, the quit function gets executed which prevents the further execution of the program and hence terminates the program.

exit () command –

This command is also an in-built function to get out of the python program. Once the exit () function is encountered, the program gets dismissed. It is just the other name of the quit () command.  This function is also used in the interpreter and not in the production code. It is necessary to import the site module to be able to use the exit command as it is defined in the site.py module.

Now let us understand an example to know the concept better.

Example 1: Implementation to show the usage of the exit() function.

Python exit commands

Output:

Python exit commands

Explanation: In the above code, the loop runs till 20 but we get the output from 1 to 9 only. It is so because, at i = 10, the exit function gets executed which prevents the further execution of the program and leads to termination.

sys.exit () command –

This command contains the in-built function to dismiss the program. It can be used in the production code unlike exit () and quit () as the sys module is always offered.

In this function, A string can also be passed in addition to the integer to the sys. exit () method. It also raises an exception called the SystemExit exception. This is normally used in the child process after os. fork ()

Now let us understand an example to know the concept better.

Example: Below is animplementation to show the working of the sys.exit() command.

Python exit commands

Output:

Python exit commands

Explanation: In the above code, the age is 27 and the condition is if the age is greater than 18 which is true in this case and thus we receive the output “Age is less than 18“  and our program gets dismissed. Here, we can see the string is being passed.

os. exit () command –

This command in Python is used to exit the process with specified status without calling cleanup handlers, flushing stdio buffers, etc. It is required to import the os module to be able to use this command. It is used only in unusual cases such as immediate exit.

Now, let us understand an example to know the concept better.

Example: The below figure shows the implementation to show the usage of the os._exit() command.

Python exit commands

Output –

Python exit commands

Use exit() or ctrl -Z plus Return to exit

Explanation: In the above code, the output is 0, 1, 2 as the os._exit function is encountered.

Summary

In this tutorial, we studied what is exit function. The four main exit functions are provided by the Python programming language to make the language easier and more accessible. We have seen the implementations of each of the four functions.


Related Topics

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

Features of Python

Python is a powerful, easy to learn, popular programming language. It has effective data structures and its elegant syntax makes it user-friendly language. Below are the key features of Python: 1. Python...

4 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Adding a key-value pair to dictionary in Python

Python dictionaries are collections of unsorted key-value pairs. This article will examine a method for adding new key-value teams to an existing dictionary. Dictionary in Python With the aid of curly brackets...

3 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Python Abstract Class

An Introduction to Abstract Classes An Abstract Class is one of the most significant concepts of Object-Oriented Programming (OOP). An Abstract class can be deliberated as a blueprint or design for...

6 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.