• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Terminate a Program in Python

Terminate a Program in Python

Author: Aditya Raj
Last Updated: February 18, 2022

While writing a program in python, you might need to end a program on several occasions after a condition is met. In this article, we will discuss different ways to terminate a program in python.

Terminate a Program Using the quit() Function

The quit() function is an inbuilt function that you can use to terminate a program in python. When the quit() function is executed, it raises the SystemExit exception. This results in the termination of the program. You can observe this in the following example.

number = 10
if number >= 10:
    print("The number is:", number)
    quit()
    print("This line will not be printed")

Output:

The number is: 10

The quit() function is defined in the site module and it works only if the module is installed. Therefore,  I would recommend you not to use this function in real-world applications. However, you can use the quit() function while writing smaller programs.

Terminate a Program Using the exit() Function

The exit() function is also defined in the site module. It works in a similar way to the quit() function and terminates the program when executed as shown in the following example.

number = 10
if number >= 10:
    print("The number is:", number)
    exit()
    print("This line will not be printed")

Output:

The number is: 10

Again, I would advise you to not use this function in the production environment due to its module dependency.

Terminate a Program Using the sys.exit() Function

The sys module is included in the core python installation. You can import the sys module as follows.

import sys

To terminate a program using the sys module, we will use the sys.exit() function. The sys.exit() function when executed, takes a string message as the optional input argument and prints the message before terminating the program as shown below.

import sys
number = 10
if number >= 10:
    print("The number is:", number)
    sys.exit()
    print("This line will not be printed")

Output:

The number is: 10

However, passing any input argument other than 0 means that the program has an abnormal termination.

Using the SystemExit exception

All the above functions raise the SystemExit exception to terminate the program. So, we can also raise the SystemExit program explicitly to terminate a program in python as follows.

import sys
number = 10
if number >= 10:
    print("The number is:", number)
    raise SystemExit
    print("This line will not be printed")

Output:

The number is: 10

Conclusion

In this article, we have seen different ways to terminate a program in python. The best way to terminate a program is to use the sys.exit() function as discussed by many people on StackOverflow. So, I would suggest you use this function to terminate a program. To learn more about python programming, you can read this article on list comprehension. You might also like this article on string concatenation in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us