创建第一个 Python 应用

| 选择喜欢的代码风格  
print('Hello World, Python!')

传递到 print 的参数是字符串,这是 Python 中用于存储和管理文本的基础数据类型之一。 默认情况下,print 在行末尾输出新一行的字符,以便后续输出(例如对 print 的其他调用)会在下一行开始。

试用第一个 Python 语句


Python 支持交互式控制台体验,你可以键入命令并立即查看结果。 这有时称为 “读取–求值–输出-循环” 或 REPL。

1.键入 python 以在交互模式中启用 Python 解释器。

python

2.应当会看到与以下内容类似的输出:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

3.>>> 是等待你键入 Python 命令的解释器。 将以下语句键入到解释器中。

print('CommandNotFound')

4.它应该将文本直接回显给你,然后再发出一个等待下一条命令的提示。

CommandNotFound
>>>

获取帮助


REPL 有一个可用于查找关键字和函数的内置帮助函数。 此函数的通用语法是:

help([object])

其中 [object] 是你想获取有关其帮助的特定函数或关键字。 我们来尝试一些示例。

1.键入以下命令以获取有关 print 函数的帮助。

help(print)

2.这将显示以下输出:

Help on built-in function print in module builtins:

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

    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.
    flush: whether to forcibly flush the stream.

3.键入 q 退出帮助屏幕并返回到解释器提示。

交互式帮助控制台


如果不将参数传递到帮助函数,则会启动交互式帮助功能。

1.键入 help() 以输入交互式帮助控制台。 这将列出一些关于如何使用帮助系统的基本说明。

2.在这里,只需键入你感兴趣的元素。 键入 string 获取关于 string 数据类型的帮助。 它将显示与以下内容类似的内容:

>>> help()

Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/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, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> string
Help on module string:

NAME
    string - A collection of string constants.

DESCRIPTION
    Public module variables:

    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template

    class Formatter(builtins.object)
     |  Methods defined here:
     |
     |  check_unused_args(self, used_args, args, kwargs)
     |
     |  convert_field(self, value, conversion)
     |
     |  format(*args, **kwargs)
     |
     |  format_field(self, value, format_spec)
     |
     |  get_field(self, field_name, args, kwargs)
     |      # given a field_name, find the object it references.
     |      #  field_name:   the field being looked up, e.g. "0.name"
     |      #                 or "lookup[3]"
     |      #  used_args:    a set of which args have been used
     |      #  args, kwargs: as passed in to vformat
     |
     |  get_value(self, key, args, kwargs)
     |
     |  parse(self, format_string)
     |      # returns an iterable that contains tuples of the form:
     |      # (literal_text, field_name, format_spec, conversion)
     |      # literal_text can be zero length
     |      # field_name can be None, in which case there's no
     |      #  object to format and output
     |      # if field_name is not None, it is looked up, formatted
     |      #  with format_spec and conversion and then used
     |
     |  vformat(self, format_string, args, kwargs)
     |
     |  ----------------------------------------------------------------------

3.如果有多个页面的输出,可以按 Enter 以逐行显示,或者按空格键以逐页显示。

4.点击帮助屏幕末尾后,键入 q 退出页面。 这将返回到交互式帮助提示。

5.可在此处随意浏览更多内容 - 例如,可通过键入 keywords 获取 Python 关键字列表。

6.完成后,键入 quit 以返回到交互式提示。



发表评论