0 and 1 are the exit codes.

exit(0) means a clean exit without any errors / problems

exit(1) means there was some issue / error / problem and that is why the program is exiting.

This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.

This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.

Answer from manojlds on Stack Overflow
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ exiting-a-python-program
Exiting a Python program - Python Morsels
February 21, 2022 - To exit a Python program early, call sys.exit with 0 for success, 1 for error, or a string (to print an error message while exiting).
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ sys.html
sys โ€” System-specific parameters and functions
If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ builtin-exceptions โ€บ systemexit
SystemExit | Pythonโ€™s Built-in Exceptions โ€“ Real Python
Calling sys.exit(0) raises a SystemExit exception and terminates the interpreter without printing a traceback.
๐ŸŒ
Adam Johnson
adamj.eu โ€บ tech โ€บ 2021 โ€บ 10 โ€บ 10 โ€บ the-many-ways-to-exit-in-python
The Many Ways to Exit in Python - Adam Johnson
October 10, 2021 - More obscurely, we can pass SystemExit any object, in which case Python will print the str() of that object to stderr and return an exit code of 1:
๐ŸŒ
Piccolo-orm
piccolo-orm.com โ€บ blog โ€บ understanding-sys-exit
Understanding sys.exit - Piccolo Blog
April 21, 2021 - The number passed to sys.exit is the exit code. In Unix, an exit code of 1 means something went wrong. An exit code of 0 means it was successful. On the command line, you can see the exit code of the last command using echo $?. >>> python successful_script.py >>> echo $? 0 >>> python ...
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ python-exit-commands-quit-exit-sys-exit-os-exit-and-keyboard-shortcuts
Python Exit Commands: quit(), exit(), sys.exit(), os._exit() and Keyboard Shortcuts | Codecademy
This function prints numbers from 1 to 20 but exits gracefully when it reaches 10, returning a status code of 0 (indicating successful completion). While sys.exit() ensures proper handling, os._exit() is a lower-level function that forces immediate termination without executing cleanups. Letโ€™s examine it next. When a Python program needs to terminate instantly, bypassing all cleanup routines and exception handling, os._exit() is the go-to command.
๐ŸŒ
Databricks
community.databricks.com โ€บ t5 โ€บ get-started-discussions โ€บ what-is-the-alternative-for-sys-exit-0-in-databricks โ€บ td-p โ€บ 38301
Solved: What is the alternative for sys.exit(0) in Databri... - Databricks Community - 38301
July 27, 2023 - 2. Job Terminate with sys.exit(1) - when the job criteria meet some condition, then we do terminate the job with sys.exit(1), which basically terminates the job by marking the job as failed. the above criteria work as expected in any Python environment but not in Databricks.
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
> sys.exit(1) # Python exits with error code 1 on EPIPE Should be 141 instead of... | Hacker News
March 22, 2020 - Should be 141 instead of 1. Convention is that when a program dies because of a signal, the exit code should be 128 + the signal number, in this case 13. So, 128 + 13 = 141 ยท Here are some examples:
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-38683.html
python difference between sys.exit and exit()
November 11, 2022 - Hi Team, In my project I used try except block , if exception occured I used sys.exit() I saw exit() in someones code , when to use sys.exit and exit() if not Path.exists(source_path): print(f'Source path: {source_path} does not exists'...
๐ŸŒ
McNeel Forum
discourse.mcneel.com โ€บ rhino developer
Sys.exit() shows popup window instead of just exiting - Rhino Developer - McNeel Forum
August 9, 2023 - We are successfully launching Rhino 7 with command line parameters to run a script. For testing, the script just waits a few seconds and then exits with the sys.exit() command. Except that it always displays a popup window so that a user has to press a button.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2088897 โ€บ why-do-i-need-sysexit
Why do I need sys.exit()? | Sololearn: Learn to code for FREE!
A slightly more technical example: You are from a Shell and want to run a Python script, but you need to know if everything went well. One way to know if everything was executed correctly is to see some value that your script returns. Not only can you go out with sys.exit (), you can also go out with a custom value like sys.exit (1) or sys.exit (999) and use that value returned by your program to do one or another action from your shell.
๐ŸŒ
Geeksta
geeksta.net โ€บ geeklog โ€บ how-to-exit-a-python-program-gracefully
How to Exit a Python Program Gracefully | Geeksta
May 22, 2025 - The sys.exit() function stops the entire program. If provided, the argument (e.g., "Goodbye!") is displayed to the console, or passed to the parent process when used in automation pipelines.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-exit-status-code-of-a-Python-script
What is the exit status code of a Python script? - Quora
In Python, you can check the exit code of a script using the `sys.exit()` function provided by the `sys` module. By default, if your script runs without any issues, it will have an exit code of 0 (zero), which indicates success .
๐ŸŒ
Peerlist
peerlist.io โ€บ blog โ€บ engineering โ€บ python-exit-function
How to Use an Exit Function in Python to Stop a Program
September 22, 2024 - import sys import argparse def main(): parser = argparse.ArgumentParser(description="Sample CLI program") parser.add_argument("--name", help="Your name") args = parser.parse_args() if args.name: print(f"Hello, {args.name}!") else: print("Error: Please provide your name using --name") sys.exit(1) sys.exit(0) if __name__ == "__main__": main() ... import sys import signal def signal_handler(sig, frame): print("\nCtrl+C pressed. Exiting gracefully...") sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print("Running... Press Ctrl+C to exit.") while True: pass # Simulate long-running process ยท Whether you're developing scripts, command-line tools, or complex applications, mastering the exit() function will enhance your Python programming skills and improve the overall quality of your software.
๐ŸŒ
Medium
pavolkutaj.medium.com โ€บ explaining-the-difference-between-exiting-functions-with-exit-and-raise-in-python-ceda89ef2846
Explaining the Difference between Exiting Functions with exit() and raise in Python | by Pavol Z. Kutaj | Medium
June 30, 2023 - exit() is intended for use in the interactive shell. ... import pytest def test_confirm(): intention = "no" with pytest.raises(SystemExit): confirm(intention) intention = "" with pytest.raises(SystemExit): confirm(intention) intention = "yes I am serious" confirm(intention)