{"id":7566,"date":"2020-07-30T16:02:16","date_gmt":"2020-07-30T16:02:16","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7566"},"modified":"2023-04-07T15:03:53","modified_gmt":"2023-04-07T15:03:53","slug":"exit-a-python-program","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/exit-a-python-program","title":{"rendered":"Exit a Python Program in 3 Easy Ways!"},"content":{"rendered":"\n<p>Sometimes a program is written to be built and run forever but needs to be stopped when certain specific things happen. There are many methods in Python that help to stop or exit a program. <\/p>\n\n\n\n<p>This tutorial will cover the different functions to exit a Python program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the quit() function<\/h2>\n\n\n\n<p>The easiest way to quit a Python program is by using the built-in function quit(). The quit() function is provided by Python and can be used to exit a Python program quickly.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nquit()\n<\/pre><\/div>\n\n\n<p>As soon as the system encounters the quit() function, it terminates the execution of the program completely.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor x in range(1,10):\n    print(x*10)\n    quit()\n<\/pre><\/div>\n\n\n<p>In the above code, we have used the quit() function inside a <a href=\"https:\/\/www.askpython.com\/python\/python-for-loop\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/python-for-loop\" target=\"_blank\" rel=\"noreferrer noopener\">for loop<\/a> to see if it terminates.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<p>Here we can see that the first iteration completes and then the quit() function executes immediately which ends the program, hence the statement print only once.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python sys.exit() function<\/h2>\n\n\n\n<p>Another useful function to exit a Python program is <code>sys.exit()<\/code> provided by the sys module which can be imported directly into Python programs. <\/p>\n\n\n\n<p>An additional feature of this function is that it can take a string as an argument and print it after it is executed. The sys.exit() function can be used at any point in time without worrying about corruption in the code.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsys.exit(argument)\n<\/pre><\/div>\n\n\n<p>Let us have a look at the below example to understand <code>sys.exit()<\/code> function.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport sys \n\nx = 50\n\nif x != 100: \n\tsys.exit(&quot;Values do not match&quot;)\t \nelse: \n\tprint(&quot;Validation of values completed!!&quot;) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nValues do not match\n<\/pre><\/div>\n\n\n<p>There are many more functions in the sys module that you might find useful in learning,<a href=\"https:\/\/www.askpython.com\/python-modules\/python-sys-module\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python-modules\/python-sys-module\" rel=\"noreferrer noopener\"> click here<\/a> to see the full guide on this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using exit() function<\/h2>\n\n\n\n<p>Apart from the above-mentioned techniques, we can use another in-built <code>exit()function<\/code> to quit and come out of the execution loop of a program in Python. This function is similar to the function quit().<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nexit()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor x in range(1,10):\n    print(x*10)\n    exit()\n<\/pre><\/div>\n\n\n<p>As already mentioned, the exit() function can be considered an alternative to the quit() function, which enables us to terminate the execution of a Python program.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<p>After the first iteration completes and the exit() function executes immediately which ends the program, hence the statement print only once.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have learned three methods to exit a Python program, among them sys.exit() is more beneficial as it can print a final message by taking it as an argument, but it is complex as you need to import the sys module in order to use it whereas the quit() and exit() functions can be called directly. You can choose one according to your need.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/19782075\/how-to-stop-terminate-a-python-script-from-running\/\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/19782075\/how-to-stop-terminate-a-python-script-from-running\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes a program is written to be built and run forever but needs to be stopped when certain specific things happen. There are many methods in Python that help to stop or exit a program. This tutorial will cover the different functions to exit a Python program. Using the quit() function The easiest way to [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":48542,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-7566","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7566"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7566\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/48542"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}