- Python basis:
-
- input Output:
- 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)
- 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:
- Integer
- Float
- 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: '' '...' ''
- 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:
- 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
- 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
- list:
- Analyzing conditions: cycle:
- if...elif...else
- for x in ...和while...
- Expansion of functions:
- range (5) represents a sequence generated 0-4
- list () sequence into the generated list
- dict dictionary and set collections:
- 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
- set:
-
集合类型与数学中的集合概念一致,是多个元素的无序组合(每个元素唯一,不存在相同元素)
-
集合用大括号{}表示,元素间用逗号分隔
-
建立集合类型用{}或set() ,建立空集合,必须使用set()
-
- 函数:
- 调用:和C语言C++中类似
- 定义:
- 使用def定义,且参数不需要定义
- 空函数:Python不允许出现空语句,使用pass表示什么都不做的语句
- 返回多个值:return x,y
- 参数:
- 类型检查:isinstance()
- 位置参数:类似于C++中默认参数,默认参数只能出现在函数参数的后面
- 可变参数:允许传入0个或者任意个参数,定义的时候,直接在参数前加一个*,调用时,直接传入所有的参数即可,会自动生成一个tuple
- 关键字参数:定义的时候,在参数名前加两个*。调用传入参数时,内部会组成一个dict,传入参数时,必须使用a=b的形式
- 命名关键字参数:参数组合:参数定义的顺序:必选,默认,可变,命名关键字,关键字
- 需要一个特殊分隔符
*
,*
后面的参数被视为命名关键字参数 - 如果函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符
*
了 - 必须传入参数名,这和位置参数不同。如果没有传入参数名,调用将报错
- 可以有缺省值,从而简化调用
- 需要一个特殊分隔符
- 递归函数
- 高级特性:
- 切片:取出list或者tuple中部分元素
- 装饰器:
- 用途:有时候需要打印函数的日志但又不需要修改函数的内容
- 是一个返回函数的高阶函数
- 偏函数:
- functools.partial会帮助我们创建一个偏函数
- 作用:吧一个函数的某些参数固定住,返回一个新的函数,调用这个函数会更加简单
- 使用模块
- import A:引入A库
- from A import b:导入A库中的b模块
- import A as B:导入A库并命名为B