{"id":23833,"date":"2021-10-05T20:28:38","date_gmt":"2021-10-05T14:58:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=23833"},"modified":"2021-11-22T18:33:25","modified_gmt":"2021-11-22T13:03:25","slug":"python-program-for-how-to-end-program","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/","title":{"rendered":"Python Program for How To End Program"},"content":{"rendered":"

In Python, we can use one of the following methods to exit the program.<\/p>\n

    \n
  1. sys.exit()<\/li>\n
  2. quit()<\/li>\n
  3. exit()<\/li>\n<\/ol>\n

    All of these functions have nearly identical functionality because they raise the SystemExit exception, which is the reason the Python interpreter exists, and no stack traceback is printed.<\/p>\n

    When we run a program in Python, we execute every line of code in the file, from top to bottom. Scripts normally terminate when the interpreter reaches the end of the file, but the execution can also be terminated programmatically.<\/p>\n

    Program for How To End Program in Python<\/h2>\n

    1. sys.exit() Method:<\/strong><\/p>\n

    In Python, use the sys. exit() function to exit the program. To exit the program, use the sys module’s built-in function sys. exit(). The sys. exit() function can be called at any time without fear of causing code corruption.<\/p>\n

    Syntax:<\/strong><\/p>\n

    sys.exit<\/span>([arg])<\/pre>\n

    The sys. exit() function accepts an optional argument arg, which can be an integer representing the exit or another type of object.<\/p>\n

    Examples:<\/strong><\/p>\n

    Example1:<\/strong><\/p>\n

    import sys\r\ngvn_numb = 10\r\nif gvn_numb != 15:\r\n    sys.exit(\"Number mismatch.Exiting from program\")\r\nelse:\r\n    print(\"The given numbers are matched\")\r\n<\/pre>\n

    Output:<\/strong><\/p>\n

    Number mismatch.Exiting from program<\/pre>\n

    Example2:<\/strong><\/p>\n

    Input:<\/strong><\/p>\n

    import sys\r\ngvn_numb = 15\r\nif gvn_numb != 15:\r\n    sys.exit(\"Number mismatch.Exiting from program\")\r\nelse:\r\n    print(\"The given numbers are matched\")\r\n<\/pre>\n

    Output:<\/strong><\/p>\n

    The given numbers are matched<\/pre>\n

    2.quit() Method:<\/strong><\/p>\n

    Use the quit() function to exit the Python program. quit() is a built-in Python function that raises the SystemExit exception and displays a message.<\/p>\n

    Syntax:<\/strong><\/p>\n

    quit()<\/pre>\n

    Examples:<\/strong><\/p>\n

    Example1:<\/strong><\/p>\n

    for itr in range(2, 6):\r\n    print(itr*5)\r\n    quit()<\/pre>\n

    Output:<\/strong><\/p>\n

    10<\/pre>\n

    Example2:<\/strong><\/p>\n

    Input:<\/strong><\/p>\n

    for itr in range(1, 12):\r\n    if(itr % 2 == 0):\r\n        print(itr, end=\" \")\r\n        quit()\r\n<\/pre>\n

    Output:<\/strong><\/p>\n

    2<\/pre>\n

    3.exit() Method:<\/strong><\/p>\n

    In Python, use the exit() function to exit the program. The exit() function in Python is a built-in function that exits the program’s execution loop.<\/p>\n

    Syntax:<\/strong><\/p>\n

    exit()<\/pre>\n

    The exit() function is an alternative to the quit() function, which terminates the program’s execution.<\/p>\n

    That is for terminating a Python program.<\/p>\n

    Example1:<\/strong><\/p>\n

    for itor in range(10):\r\n    if itor == 5:\r\n        exit()\r\n    print(itor)\r\n<\/pre>\n

    Output:<\/strong><\/p>\n

    0\r\n1\r\n2\r\n3\r\n4<\/pre>\n

    Example2:<\/strong><\/p>\n

    for itor in range(20):\r\n    if itor % 2 != 0 and itor > 8:\r\n        exit()\r\n    print(itor)\r\n<\/pre>\n

    Output:<\/strong><\/p>\n

    0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8<\/pre>\n","protected":false},"excerpt":{"rendered":"

    In Python, we can use one of the following methods to exit the program. sys.exit() quit() exit() All of these functions have nearly identical functionality because they raise the SystemExit exception, which is the reason the Python interpreter exists, and no stack traceback is printed. When we run a program in Python, we execute every …<\/p>\n

    Python Program for How To End Program<\/span> Read More »<\/a><\/p>\n","protected":false},"author":7,"featured_media":23974,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Program for How To End Program - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program for How To End Program - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In Python, we can use one of the following methods to exit the program. sys.exit() quit() exit() All of these functions have nearly identical functionality because they raise the SystemExit exception, which is the reason the Python interpreter exists, and no stack traceback is printed. When we run a program in Python, we execute every … Python Program for How To End Program Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-05T14:58:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:03:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vikram Chiluka\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#primaryimage\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png\",\"width\":1280,\"height\":720,\"caption\":\"Program for How To End Program\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/\",\"name\":\"Python Program for How To End Program - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#primaryimage\"},\"datePublished\":\"2021-10-05T14:58:38+00:00\",\"dateModified\":\"2021-11-22T13:03:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Program for How To End Program\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\"},\"headline\":\"Python Program for How To End Program\",\"datePublished\":\"2021-10-05T14:58:38+00:00\",\"dateModified\":\"2021-11-22T13:03:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#webpage\"},\"wordCount\":259,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\",\"name\":\"Vikram Chiluka\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"caption\":\"Vikram Chiluka\"},\"url\":\"https:\/\/python-programs.com\/author\/vikram\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program for How To End Program - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/","og_locale":"en_US","og_type":"article","og_title":"Python Program for How To End Program - Python Programs","og_description":"In Python, we can use one of the following methods to exit the program. sys.exit() quit() exit() All of these functions have nearly identical functionality because they raise the SystemExit exception, which is the reason the Python interpreter exists, and no stack traceback is printed. When we run a program in Python, we execute every … Python Program for How To End Program Read More »","og_url":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-10-05T14:58:38+00:00","article_modified_time":"2021-11-22T13:03:25+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Vikram Chiluka","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#primaryimage","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png","width":1280,"height":720,"caption":"Program for How To End Program"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#webpage","url":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/","name":"Python Program for How To End Program - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#primaryimage"},"datePublished":"2021-10-05T14:58:38+00:00","dateModified":"2021-11-22T13:03:25+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-program-for-how-to-end-program\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Program for How To End Program"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602"},"headline":"Python Program for How To End Program","datePublished":"2021-10-05T14:58:38+00:00","dateModified":"2021-11-22T13:03:25+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#webpage"},"wordCount":259,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"image":{"@id":"https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#primaryimage"},"thumbnailUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-program-for-how-to-end-program\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602","name":"Vikram Chiluka","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","caption":"Vikram Chiluka"},"url":"https:\/\/python-programs.com\/author\/vikram\/"}]}},"jetpack_featured_media_url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-for-How-To-End-Program.png","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/23833"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=23833"}],"version-history":[{"count":6,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/23833\/revisions"}],"predecessor-version":[{"id":23979,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/23833\/revisions\/23979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media\/23974"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=23833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=23833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=23833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}