How to end a Python Program Explained

Posted in Python on January 17, 2023 by Jessica Rose ‐ 1 min read

How to end a Python Program Explained

10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

To end a Python program, press one of the following keys depending on the type of operating system :

For Windows :

CTRL + C

For Mac Os

Control + C

How to End the Interactive Python Command-Line Session

To leave the interactive Python command-line session, press: For Windows :

CTRL + Z 

For Mac OS :

Control + Z

Or run the exit() function in your session.

>>> exit()

Python-exit

How to Terminate a Python Script

To terminate a Python program from within the code, utilize the sys.exit() function.

  1. Always import the sys library
  2. Use sys.exit()

For example:

import sys
sys.exit()

10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co