美文网首页
Python3 基础语法

Python3 基础语法

作者: 双门 | 来源:发表于2018-09-11 17:14 被阅读23次
    Mac:~ mac$ which python3
    /usr/local/bin/python3
    
    Mac:~ mac$ /usr/local/bin/python3
    Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> #!/usr/bin/python3
    ...  
    ... print("Hello, World!");
    Hello, World!
    
    或者:
    Mac:~ mac$ which python3.7
    /usr/local/bin/python3.7
    Mac:~ mac$ /usr/local/bin/python3.7
    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
    [Clang 6.0 (clang-600.0.57)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import keyword
    >>> keyword.kwlist
    ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
    >>> 
    

    标识符
    第一个字符必须是字母表中 字母下划线 '_'
    标识符的其他的部分有字母、数字 和 下划线 组成。
    标识符对大小写敏感,区分大小写。
    在Python 3中,非-ASCII 标识符 也是允许的了。


    python保留字

    保留字即关键字,我们不能把它们用作任何标识符名称。Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字:

    >>> import keyword
    >>> keyword.kwlist
    ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
    
    整理排序为: 
    
    and  as  assert
    break
    class   continue
    def  del
    elif else except
    finally  False for from
    global
    if import   is in
    lambda
    None   nonlocal  not
    or
    pass
    raise return
    True  try
    while with
    yield
    
    

    Python中单行注释以 # 开头,实例如下:

    >>> # 第一个注释
    ... print ("Hello, Python!") # 第二个注释
    Hello, Python!
    >>> 
    

    你可以将以上代码保存在 hello.py 文件中并使用 python 命令执行该脚本文件。

    $ python3.7 hello.py

    以上命令输出结果为:
    Hello, World!

    相关文章

      网友评论

          本文标题:Python3 基础语法

          本文链接:https://www.haomeiwen.com/subject/ptpioxtx.html