How to end a program in Python – with example

  • Reading time:17 mins read

How to end a program in Python

There are quite a number of reasons why a programmer may want a program to stop running at some point.

Some of the most common scenarios that warrant the stoppage of a program are if a certain condition has been met or violated and we do not wish to continue executing the rest of the program.

 

 

Furthermore, when iterating we may also wish to stop the programs from executing if a certain number of iterations has been met.

However, most programs would naturally close down when the end of the program is reached. Python being a dynamic language in nature provides various methods that we can easily use to stop further execution of a program.

Depending on the environment being used there are different methods that can be used to stop code execution.

 

how to end a program in Python

 

How to end a program in Python interactive shell

Python offers a command-line interface that we can use to execute simple python programs.

The python interactive shell can be accessed from the terminal also known as the command prompt in windows by typing the commandpython3” or “pythonif you’re running Python on your system. 

 

 

Python provides built-in functions that we can use to exit the python interactive shell once we have finished running our programs. These functions include the quit() and exit() function.

The quit() and the exit() functions

Using the quit() function we can easily exit the python interactive shell by calling the function anywhere in the python interactive shell as shown below. This allows us to revert back to the normal terminal interface.

 

 

The exit() function is a cross-platforms function that is used to terminate program execution in Python.

 

We can also use the exit() function in the Python interactive shell to end program execution and opt out of the Python interactive shell as shown below.

 

 

How to end a program in Python within an IDE

These keywords are not limited to use in the interactive shell, they can also be used within programs written in IDEs such as VS Code.

In the example below we have initialized two variables namely: a and b and an if statement that checks if the values of a are greater than that of b and quits the program if so. Otherwise the program prints a message that it is carrying on.

 

 

In this case, when we hit the conditional we see that the code would actually carry on. This is because in this case 15 is not greater than 25.

However, if we make the statement True i.e change the value of the variable a to be greater than that of b then in that case the code under the if statement will be executed, exiting the program.

 

How to end a program in Python

 

We can also add a print statement prior to the quit() function that lets us know that the program is exiting as shown below.

 

 

We can also use the exit() function in place of the quit() function and it will work in a similar manner.

 

 

These are the two easiest ways that we can actually use to exit or end our program.

 

How to end a program in Python

 

How to end a program in Python by using the sys.exit() function

The sys module in Python provides us with access to variables and functions that are strongly maintained and interact with the interpreter.

To use this function though, we need to import the sys module at the top of our programs.

We also need to import the os module, which allows us to interact with the underlying operating system.  The sys.exit() function accepts an optional argument and is considered to be the standard way of exiting processes in production.

 

 

How to end a program in Python by using the os._exit() function

Alternatively, we can also use the os._exit() function to exit a process. However, unlike the sys.exit() function the os._exit() function is normally used in child processes after the os.fork() system call.

This function accepts one optional argument and can be implemented as shown below.

 

 

Although all the above methods are effective in exiting programs or stopping code execution, the sys.exit() function is the recommended method to be used in production-ready code.

Summary

This is how to end a program in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.