day.13 function

First, the concept of functions: a function of tissue is good, reusable, single or code segments to implement the associated function. 
Function: function
role: to improve code modularity, improve efficiency, reduce code redundancy, easy to post-maintenance
custom function syntax:
DEF function names (some parameters):
'' 'function documentation' ''
function body
return expression
Description: 
1) the definition of the function key def
2) Function Name, naming rule with identifier y7777
. 3) a number of parameters, if there are a plurality of parameters, the parameters between the parameter and separated by commas;
if no parameters, nor parentheses omitted
4) documentation function is a function of a function, the function name --doc-- be viewed through
5) return, return, the value of the expression is returned to the caller of the function of
a. no return, return None
b .return expression returns the value of the expression to the caller of the function, the variable value received return
data types with a data type of an expression
c.return value 1, value 2 .. . Return multiple values to be stored in the form of a tuple
function returns run return is encountered, the following code is not executed return XP
6) must first define the function, after calling
syntax call: function name (actual parameter), if no arguments can not omit parentheses

Second, the function, depending on whether the parameters, presence or absence of the return value, divided into four types: 
1, no parameters and returns no value
2, no parameters, return value
3, the parameters and returns no value
4, there are parameters, there the return value


function parameters, the following points
1, (a, b, c ) position parameter according to the parameter for parameter passing smell
2, def fun (a, b = 5) b = 5, the default value of the parameter, write behind location parameters, otherwise an error not to the default value of the parameter, the default value of the parameter will be equal to the default value
argument pass argument is that if the value of the parameter passed to the parameter, the default value will be equal to the default value of the parameter passed new entrants
If no the default value of the parameter passed parameters, the default value will be equal to the default value of the parameter
3, def fun (a, b , * args, ** kwargs) *, **, variable length parameters, received extra arguments
* args to tuple receiving the extra position parameter
** kwargs receiving excess dictionary keyword argument
parameters play wrapped
actual parameters of the function, the following points;
. 1, Result = Fun (l, 2,3) l, 2,3, position parameters, parameter passing position parameter according
2.re sult = fun (1,2, c = 3) 1,2 location parameters, c = 3, keyword parameters, the parameters written in the rear position, otherwise an error
keyword parameter passing arguments that line arguments can be defined with the order inconsistent

三,全局变量和局部变量
1.全局变量,定义在文件中,函数为的变量,作用域是整个文件。
如果在函数中想要修改全局变量的值,使用关键字,global 变量名 来进行声明
语法:global 全局变量名
在函数内部,如果不提前声明直接修改全局变量的值,会认为是从新定义了一个局部变量。
局部变量在函数中会被优先使用
2,局部变量,定义在函数中的变量,作用域只在函数中。
 
四,匿名函数:没有名字的函数
语法格式:lambda 若干参数:表达式
只能实现简单的功能,简洁
  匿名函数的作用:
1、lambda函数可以作为函数的实参
2、lambda函数作为内置函数(比如,sort()方法)的参数
 
五,时间函数
1,先引入时间模块 import time
2,时间戳 time.time 从1970年1月1日00:00:00开始到现在时间的秒数
3,格式化时间字符串 time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
4,time.sleep(sec) 休眠几秒钟,sec代表秒数
 
 
 


 

Guess you like

Origin www.cnblogs.com/wz2001/p/12115364.html