×

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 Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

3 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes 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.

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.

Reading a File Line by Line in Python

Introduction In this tutorial, we will learn about reading files in python line by line. Before reading the files, let us know a little information about the files first. Files A file is...

6 minutes read.

Python Empty Tuple

How to Create an Empty Tuple Tuple A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String...

3 minutes read.

Rank Based Percentile GUI Calculator using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities...

3 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.

String indices must be integers in Python

Lists, tuples, and strings are examples of iterable objects in Python whose items or characters can be retrieved by their index numbers. For instance, you might take the following action to...

3 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Python String center() method

Python String center() method The center() method will center align the string, using a specified character (space is default) as the fill character. Syntax string.center(width[, fillchar]) Parameter width This parameter represents the length of the returned string. fillchar...

1 minute read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 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 learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.