
在本文中,你将了解如何使用以下方法在终端中退出 Python 程序:
- Windows 和 macOS(以及其他基于 Unix 的系统——我们将使用 macOS 来表示它们)中的 and 函数。
exit()
quit()
- Windows 中的命令。
Ctrl + Z
- macOS 中的命令。
Ctrl + D
如何在终端中运行 Python 程序
要在终端中运行 Python,您必须打开终端并运行命令。python
请注意,仅当您的计算机上安装了 Python 时,该命令才在您的终端中起作用。Python
运行命令后,终端中应该有如下内容:
C:\Users\USER>python
Python 3.10.8 (main, Nov 6 2022, 23:27:16) [GCC 12.2.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

我正在使用适用于 Windows 的命令提示符,但如果您使用的是 macOS 或 Linux,这应该以相同的方式工作。
现在,您可以在终端中运行 Python 代码:
>>> print("Welcome to Py in the terminal!")
Welcome to Py in the terminal!
如何使用 和 函数在终端中退出 Python 程序exit()
quit()
您可以使用 and 函数在 Windows 和 macOS 中退出 Python 程序。exit()
quit()
>>> print("Welcome to Py in the terminal!")
Welcome to Py in the terminal!
>>> exit()
C:\Users\USER>

在上面的例子中,我们在使用该函数退出终端之前打印了“欢迎来到终端中的 Py!exit()
执行函数后,您将能够以常规方式使用终端(使用 Python 环境)。
该函数的过程是相同的:quit()
>>> print("Welcome to Py in the terminal!")
Welcome to Py in the terminal!
>>> quit()
C:\Users\USER>

如何使用命令在终端中退出 Python 程序Ctrl +
您可以使用以下命令退出在 Windows 终端中运行的 Python 程序:Ctrl + Z
>>> print("Welcome to Py in the terminal!")
Welcome to Py in the terminal!
>>> ^Z
C:\Users\USER>

同样,您可以在 macOS 中使用该命令。Ctrl + D
总结
在本文中,我们讨论了在终端中运行 Python 程序。
我们看到了如何使用该命令在终端中运行 Python。Python
我们还看到了如何使用几种不同的方法在终端中退出 Python 程序。
和 函数可以在 Windows 和 macOS 的终端中退出 Python 程序。exit()
quit()
或者,您可以使用该命令在 Windows 和 macOS 的终端中退出 Python 程序。Ctrl + Z
Ctrl + D
祝你编码愉快