python shell使用_使用 Python Shell

Python Shell 是您探索 Python 语法,通过命令获取交互式帮助以及调试段程序的地方。图形化 Python Shell (名为 IDLE)还包括了一个不错的文本编辑器,它支持 Python 语法着色并与 Python Shell 进行了整和。如果还没有喜欢的文本编辑器,不妨试用下 IDLE 。

重中之重。Python Shell 本身是一款了不起的互动环境。在本书中,您将看到下面这样的例子:

>>>1+1

2

这三个尖括号, >>>,表示 Python Shell 提示符。不要输入该部分。它只是让您知道该例要在 Python Shell 中运行。

1+1 是您输入的部分。您可在 Python Shell 中输入任何有效的 Python 表达式和命令。别怕羞,它不会咬你!最糟糕的事情也不过看到一条错误信息。命令将立即得到执行(一旦您按下 ENTER[回车键]);表达式的值将立即得到计算,而 Python Shell 将输出结果。

2 是该表达式的计算结果。事实上,1+1 是一个有效的 Python 不等式。结果,当然,是 2 。

让我们尝试下另一个例子.

>>>print('Hello world!')

Hello world!

很简单,不是吗?但你在 Python shell 中可完成的工作要多得多。如果您被困住了——无法想起某个命令,或者无法想起如何正确给某个函数传递参数——您可寻求 Python Shell 的交互式帮助。只需输入 help 并按下 回车键 。

>>>help

Type help() for interactive help, or help(object) for help about object.

有两种帮助模式。您可以获得某个对象的帮助,这样将只打印出文档并返回 Python Shell 提示符。您也可以输入 help mode,系统将不会计算 Python 表达式,您只需输入关键字或命令名称,系统将会输出关于该命令它所知道的内容。

1-13101Q05125360.jpg

要进入交互帮助模式,仅需输入 help() 并按下 回车键。

>>>help()

Welcome to Python 3.0!This is the online help utility.

If this is your first time using Python, you should definitely check out

the tutorial on the Internet at http://docs.python.org/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing

Python programs and using Python modules. To quit this help utility and

return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",

"keywords", or "topics". Each module also comes with a one-line summary

of what it does; to list the modules whose summaries contain a given word

such as "spam", type "modules spam".

help>

请注意提示符是如何从 >>> 改变为 help> 的。该提示符提醒您目前正处于交互式帮助模式。现在您可以输入任何关键字、命令、模块名称、函数名称 — 几乎任何 Python 能够理解的一切 — 然后阅读其文档。

help>print ①

Help on built-in function print in module builtins:

print(...)

print(value, ..., sep=' ', end='\n', file=sys.stdout)

Prints the values to a stream, or to sys.stdout by default.

Optional keyword arguments:

file: a file-like object (stream); defaults to the current sys.stdout.

sep: string inserted between values, default a space.

end: string appended after the last value, default a newline.

help>PapayaWhip ②

no Python documentation found for 'PapayaWhip'

help>quit ③

You are now leaving help and returning to the Python interpreter.

If you want to ask for help on a particular object directly from the

interpreter, you can type "help(object)". Executing "help('string')"

has the same effect as typing a particular string at the help> prompt.

>>> ④

①要获取 print() 函数的文档,仅需输入 print 然后按下 回车键 。该交互式帮助模式将会显示类似 man 页面的内容:函数名称、简要内容、函数的参数及缺省值等等。如果文档看起来很难懂,千万别慌。您将在后面不远的章节中学到关于这些概念的更多内容。

②当然,交互式帮助模式并不知道一切。如果您所输入的不是 Python 的命令、模块、函数或者其它内建关键字,交互式帮助模式将只能耸耸虚拟的肩膀。

③要退出交互帮助模式,仅需输入 quit() 并按下 回车键。

④提示符将变回 >>> 以提示您已经离开交互帮助模式,并返回到了 Python Shell 。

图形化的 Python Shell —— IDLE,同样带有一个 Python 相关的文本编辑器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值