Exiting a Python program might seem like a straightforward task, but there are several ways to do it depending on the context and requirements of your script. Whether you want to gracefully shut down your application or terminate it immediately, knowing the right approach can help you manage your code more effectively. In this article, we will explore various methods to exit a Python program in the terminal.
Running a Python Program in Terminal
Before diving deep into exiting a Python program in the terminal, you should know how to run a program in the terminal. First, make sure that you have Python installed on your system. You can check this, by writing the following command in the command prompt. If Python is already installed, it will display the current Python version on your system.
python --version
Now let us see step by step how we can run a Python program in the terminal.
Open Terminal
First, open the terminal/command prompt in your system by typing terminal or cmd in the search box and press enter.
Initialize Python Interpreter
Next, you need to initialize the Python interpreter in the terminal so that you can easily execute Python commands.

Write and Execute Code
Once the Python interpreter is initialized in the terminal window, we can start typing the Python code and press enter to execute the systems.

Exiting Python Program in the Terminal
Exiting a Python program involves terminating the execution of the script and optionally returning a status code to the operating system. The most common methods include using exit(), quit(), sys.exit(), and raising exceptions like SystemExit. Each method has its specific use cases and implications.
Let us discuss each of these methods in detail with proper example.
Using sys.exit()
The sys.exit() function is a part of the Python sys module and is widely use to exit a python program. This function is used to terminate the program in the terminal. When this function is used, its exits the program while ignoring the rest of the code present after it, i.e., the system doesn't execute the code that are write after the sys.exit() function.
Syntax:
import sys
# code written here
sys.exit()
Below is an image of a Python program using sys.exit() in a terminal.

Using exit() and quit() functions
The exit() and quit() functions are built-in functions used in Python. We don't have to import any module for this function. It is the shortcut for sys.exit(). It is also use to terminate a program similar to sys.exit(), but it more user friendly and preferred more among coders.
Syntax for exit():
#code
exit()
Below is an image of a Python program using exit() in a terminal.

Syntax for quit():
#code
quit()
Below is an image of a Python program using quit() in a terminal.
.png)
Using Keyboard Shortcuts
While writing code in a Python terminal if you find a need to terminate the program you can use keyboard shortcuts as well to exit the program.
- In Windows, click CTRL + Z and then press ENTER, this will terminate the program from the terminal.
- in MacOs, click CTRL + D , this will terminate the program from the terminal.

Conclusion
Exiting a Python program can be done in various ways depending on the context and requirements. While exit() and quit() are suitable for interactive use, sys.exit() is the preferred method for scripts. Understanding these methods and their appropriate use cases can help you manage your Python programs more effectively.