Python End Program – 如何在终端中退出 Python 程序
Python End Program – 如何在终端中退出 Python 程序
可以在终端中执行 Python 代码,就像在 IDE(如 VS Code、Atom 等)中执行一样。您可以在 Windows 和 Unix 操作系统(如 Linux 和 macOS)中执行此操作。

在本文中,你将了解如何使用以下方法在终端中退出 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.
py-in-terminal
终端中的 Python 命令

我正在使用适用于 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-in-terminal-exit
Python 终端中的 exit() 命令

在上面的例子中,我们在使用该函数退出终端之前打印了“欢迎来到终端中的 Py!exit()

执行函数后,您将能够以常规方式使用终端(使用 Python 环境)。

该函数的过程是相同的:quit()

>>> print("Welcome to Py in the terminal!")
Welcome to Py in the terminal!
>>> quit()

C:\Users\USER>
py-in-terminal-退出
Python 终端中的 exit() 命令

如何使用命令在终端中退出 Python 程序Ctrl +

您可以使用以下命令退出在 Windows 终端中运行的 Python 程序:Ctrl + Z

>>> print("Welcome to Py in the terminal!")
Welcome to Py in the terminal!
>>> ^Z


C:\Users\USER>
py-in-terminal-ctrl-z
ctrl + z 命令退出 Windows 中的 Python 终端

同样,您可以在 macOS 中使用该命令。Ctrl + D

总结

在本文中,我们讨论了在终端中运行 Python 程序。

我们看到了如何使用该命令在终端中运行 Python。Python

我们还看到了如何使用几种不同的方法在终端中退出 Python 程序。

和 函数可以在 Windows 和 macOS 的终端中退出 Python 程序。exit()quit()

或者,您可以使用该命令在 Windows 和 macOS 的终端中退出 Python 程序。Ctrl + ZCtrl + D

祝你编码愉快

© 版权声明
文章全是未果本果写的,尽量别直接复制粘贴
暂无评论

发送评论 编辑评论

上一篇
下一篇