zoukankan      html  css  js  c++  java
  • Python基础语法

    Python标识符

    在Python中,标识符由字母、下划线、数字组成。

    在Python中,所有的标识符可以包括英文, 数字以及下划线(­_),但是不能以数字开头。

    Python中的标识符是区分大小写的。

    以下划线开头的标识符是由特殊意义的。

    l  以单下划线开头的 _foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入

    l  以双下划线开头的 __foo 代表类的私有成员

    l  以双下划线开头和结尾的 __foo__代表python里特殊方法专用的标识,如 __init__()代表类的构造函数。

    Python可以同一行显示多条语句,方法是用分号 ; 分开

    Python保留字符

    保留字不能作为常量或变量,或其他任何标识符名称,所有Python的关键字只包含小写字母。

    and

    exec

    not

    assert

    finally

    or

    break

    for

    pass

    class

    from

    print

    continue

    global

    raise

    def

    if

    return

    del

    import

    try

    elif

    in

    while

    else

    is

    with

    except

    lambda

    yield

    行和缩进

    python的代码块不使用大括号开控制类、函数以及其他逻辑判断。python最具有特色的就是用缩进来写模块

    缩进的空白数量是可变得,但所有的代码块必须包含相同的缩进空白数量,这个必须严格执行。

    建议在每个缩进层次使用单个制表符或两个空格或四个空格,切记不能混用。

    多行语句

    Python语句中一般以新行作为语句的结束符。

    但是我们可以使用斜杠()将一行的语句分为多行显示

    语句中包含[],{}或()就不需要使用多行连接符

    python引号

    Python可以使用引号、双引号、或三引号来表示字符串,引号的开始和结束必须是相同类型。

    其中三引号可以由多行组成,编写多行文本的快捷语法,常用于文档字符串,在文件的特定地点,被当做注释。

    Python注释

    python中单行注释采用#开头

    注释可以在语句或表达式行末

    python中多行注释使用三个引号或三个双引号

    Python空行

    函数之间或类的方法之间用多行空行隔开,表示一段新的代码的开始。类和函数入口之间也用一行空行分隔,以突出函数入口的开始

    空行和代码缩进不同,空行并不是Python语法的与部分,书写时不插入空行,Python解释器运行也不会出错。但是空行的作用在于分隔两段不同的功能或含义的代码,以便日后代码的维护或重构。

    空行也是程序代码的一部分。

    等待用户输入:

    下面的程序执行后就会等待用户输入,按回车键就会退出:

    raw_input("enter is exit , and else is print : ")

     

    import sys; x = 'runoob'; sys.stdout.write(x+'
    ')

    同一行显示多条语句:

    Python可以在同一行中使用多条语句,语句之间使用分号分隔:

    运行结果:

    exbot@ubuntu:~/wangqinghe/python/20190819$ python mul.py

    runoob

    print输出:

    print默认输出是换行的,如果要实现不换行需要在变量末尾加上逗号,

    /***
    print
    ***/
    x = "a"
    y = "b"
    
    print x
    print y
    
    print "----------------"
    
    print x,
    print y,
    
    print x,y

    运行结果:

    exbot@ubuntu:~/wangqinghe/python/20190819$ python print.py

    a

    b

    ----------------

    a b a b

    多个语句构成代码组

    缩进相同的一组语句构成一个代码块,我们称之为代码组。

    像if、while、def、和class这样的复合语句,首先以关键字开始,以冒号结束,该行之后的一行或多行代码构成代码组。

    我们将首行以及后面的代码组称之为一个自居(clause)

    命令行参数:

    Python可以使用-h参数来查看各参数帮助信息:

    exbot@ubuntu:~/wangqinghe/python/20190819$ python -h

    usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...

    Options and arguments (and corresponding environment variables):

    -B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x

    -c cmd : program passed in as string (terminates option list)

    -d     : debug output from parser; also PYTHONDEBUG=x

    -E     : ignore PYTHON* environment variables (such as PYTHONPATH)

    -h     : print this help message and exit (also --help)

    -i     : inspect interactively after running script; forces a prompt even

             if stdin does not appear to be a terminal; also PYTHONINSPECT=x

    -m mod : run library module as a script (terminates option list)

    -O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x

    -OO    : remove doc-strings in addition to the -O optimizations

    -R     : use a pseudo-random salt to make hash() values of various types be

             unpredictable between separate invocations of the interpreter, as

             a defense against denial-of-service attacks

    -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew

    -s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE

    -S     : don't imply 'import site' on initialization

    -t     : issue warnings about inconsistent tab usage (-tt: issue errors)

    -u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x

             see man page for details on internal buffering relating to '-u'

    -v     : verbose (trace import statements); also PYTHONVERBOSE=x

             can be supplied multiple times to increase verbosity

    -V     : print the Python version number and exit (also --version)

    -W arg : warning control; arg is action:message:category:module:lineno

             also PYTHONWARNINGS=arg

    -x     : skip first line of source, allowing use of non-Unix forms of #!cmd

    -3     : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix

    file   : program read from script file

    -      : program read from stdin (default; interactive mode if a tty)

    arg ...: arguments passed to program in sys.argv[1:]

    Other environment variables:

    PYTHONSTARTUP: file executed on interactive startup (no default)

    PYTHONPATH   : ':'-separated list of directories prefixed to the

                   default module search path.  The result is sys.path.

    PYTHONHOME   : alternate <prefix> directory (or <prefix>:<exec_prefix>).

                   The default module search path uses <prefix>/pythonX.X.

    PYTHONCASEOK : ignore case in 'import' statements (Windows).

    PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.

    PYTHONHASHSEED: if this variable is set to 'random', the effect is the same

       as specifying the -R option: a random value is used to seed the hashes of

       str, bytes and datetime objects.  It can also be set to an integer

       in the range [0,4294967295] to get hash values with a predictable seed.

    我们在使用脚本形式执行Python时,可以接受命令行输入的参数。

    执行脚本传入参数,可以使用sys模块

    /***
    argv.py
    ***/
    import sys
    
    print sys.argv

    运行结果:

    exbot@ubuntu:~/wangqinghe/python/20190819$ python argv.py hello

    ['argv.py', 'hello']

    python基本语法图:

  • 相关阅读:
    AutoFac (控制反转IOC 与依赖注入DI)
    【干货】利用MVC5+EF6搭建博客系统(一)EF Code frist、实现泛型数据仓储以及业务逻辑
    Log4.Net 在Winfrom、MVC、ashx程序里的使用,ashx程序里使用异步
    触发器
    游标的使用
    sql数据库快照与恢复 规则绑定
    磁盘恢复
    修改sql数据库名称
    MVC学习笔记
    NuGet包和功能
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11376320.html
Copyright © 2011-2022 走看看