Python basic knowledge integration - novice

  • Python basis: 
    • input Output:
      1. Input: input ( "message"), the raw_input ( "message") can, to read the content in the form of a string (if necessary with int () to convert the string to an integer)
      2. Output: print ( "content output"), quotes, it is output by the string; otherwise it is before outputting; If you want to format the output, separated by%
    • Data types and variables:
      1. Integer
      2. Float
      3. String:
        • C and different language, contained in a single or double quotes are strings
        • If the string contains a single and double quotes, this time should be escaped
        • r '' represents a string not escape the inside of the
        • Wrap deal with the problem: '' '...' ''
      4. Boolean: null: None (can not understand is 0, there is a special Python null value)
        • True/False
        • May be used and / or / not calculates
    • list list and tuple tuple:
      1. list:
        • All the elements using [] enclosed
        • You can use the standard (0 to len-1) to traverse, but the difference is that the C language, list [-n] (n is a positive number) represents the inverse of the n-th list element
        • Related operations: append () element is added to the end of list; insert () element inserted into the specified position; POP () Removes the specified elements, etc.
        • Features: find and insert elements with the increase would be slower, but take up less memory
      2.  tuple
        • With all of the elements () enclosed, can not be changed once the initialization
        • Defined tuple tuple = (); if you want to define only one tuple element (1) must be a comma disambiguate
    • Analyzing conditions: cycle:
      1. if...elif...else
      1. for x in ...和while... 
      2. Expansion of functions:
        • range (5) represents a sequence generated 0-4
        • list () sequence into the generated list
    • dict dictionary and set collections:
      1. dict:
        • To form key-value store, it has a fast search speed, but will take up more memory (that is, a method of time in exchange for space)
        • {} Used to enclose all the elements, directly through the index key value to the value
      2. set:
        • 集合类型与数学中的集合概念一致,是多个元素的无序组合(每个元素唯一,不存在相同元素)

        • 集合用大括号{}表示,元素间用逗号分隔

        • 建立集合类型用{}或set() ,建立空集合,必须使用set()

  • 函数:
    • 调用:和C语言C++中类似
    • 定义:
      1. 使用def定义,且参数不需要定义
      2. 空函数:Python不允许出现空语句,使用pass表示什么都不做的语句
      3. 返回多个值:return x,y
    • 参数:
      1. 类型检查:isinstance()
      2. 位置参数:类似于C++中默认参数,默认参数只能出现在函数参数的后面
      3. 可变参数:允许传入0个或者任意个参数,定义的时候,直接在参数前加一个*,调用时,直接传入所有的参数即可,会自动生成一个tuple
      4. 关键字参数:定义的时候,在参数名前加两个*。调用传入参数时,内部会组成一个dict,传入参数时,必须使用a=b的形式
      5. 命名关键字参数:参数组合:参数定义的顺序:必选,默认,可变,命名关键字,关键字
        • 需要一个特殊分隔符**后面的参数被视为命名关键字参数
        • 如果函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符*
        • 必须传入参数名,这和位置参数不同。如果没有传入参数名,调用将报错
        • 可以有缺省值,从而简化调用
    • 递归函数
  • 高级特性:
    • 切片:取出list或者tuple中部分元素
    • 装饰器:
      1. 用途:有时候需要打印函数的日志但又不需要修改函数的内容
      2. 是一个返回函数的高阶函数
    • 偏函数:
      1. functools.partial会帮助我们创建一个偏函数
      2. 作用:吧一个函数的某些参数固定住,返回一个新的函数,调用这个函数会更加简单
    • 使用模块
      1. import A:引入A库
      2. from A import b:导入A库中的b模块
      3. import A as B:导入A库并命名为B

Guess you like

Origin www.cnblogs.com/bilbylu/p/11138714.html