In this article, we will see some of the exit commands in Python.
exit() and sys.exit() have the almost same functionality. They raise the
SystemExit exception by which the Python interpreter exits and no stack traceback is printed.
for i in range(10): if i == 3: print(quit) quit() print(i)
It works only if the site module is imported. This function should only be used in the interpreter.
0 1 2 Use quit() or Ctrl-Z plus Return to exit
It works only if the site module is imported, so it should be used in the interpreter only.
for i in range(10): if i == 3: print(exit) exit() print(i)
0 1 2 Use exit() or Ctrl-Z plus Return to exit
sys.exit() is considered good to be used in production code for the sys module is always available. The optional argument
arg can be an integer giving the exit or another type of object.
import sys color = "red" if color != "blue": sys.exit("color is not blue") else: print("Blue color")
color is not blue