How to exit in python
In this article, we will see some of the exit commands in Python.
The functions quit()
, 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.
quit()
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.
Output:
0 1 2 Use quit() or Ctrl-Z plus Return to exit
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)
Output:
0 1 2 Use exit() or Ctrl-Z plus Return to exit
sys.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")
Output:
color is not blue
Subscribe
Login
Please login to comment
0 Discussion