Getting Started with Python: Functions

function

What is a function?

         The word function comes from mathematics, but the concept of [function] in programming is very different from functions in mathematics. Functions in programming also have many different names in English. Debt payment subroutine (subprocess or subroutine) in basic, payment procedure (process) and function in Pascal, only function in C, called method in Java

 

definition:

      A function refers to encapsulating a set of statements with a name (function name). To execute this function, you only need to call its function name.

 

characteristic:

  • reduce duplicate code                                                                                       
  • Make the program extensible
  • Is the program easier to maintain       

    

 

Can take parameters:

# The following code
a, b = 5, 8
c = a ** b
print(c)

# Change to function write
def calc(x, y):
    res = x**y
    return res # returns the result of the function execution

c = calc(a, b) # assign the result to the c variable
print(c)

  

function parameter

 

Parameters can make your function more flexible. Not only can you do dead actions, but you can also determine the execution process inside the function according to the different parameters passed when calling

formal parameter

 The memory unit is allocated only when it is called, and the allocated memory unit is released immediately at the end of the call. So the formal parameter is only valid inside the function. After the function call ends and returns to the main calling function, the formal parameter variable can no longer be used.

 

Arguments

It can be common sense, variables, expressions, functions, etc. No matter what type of actual parameters are, they must have definite values ​​when a function call is made in order to pass these values ​​to the formal parameters. Therefore, methods such as assignment and input should be used in advance to obtain a certain value for the parameter.

 

 

 

                                    

 

# default parameters
def stu_register(name, age, country, course):
    print("Registered student information".center(20, '-'))
    print("姓名", name)
    print("age", age)
    print("国籍", country)
    print("course", course)

stu_register("张三", 22, "CN", "python")
stu_register("王五", 23, "CN", "linux")
stu_register("李四", 25, "CN", "Java")

  It is found that the parameter country is basically "CN", which is a default value if it is not filled in. It is very simple to turn country into a default parameter.

def stu_register(name, age, course, country = "CN" ):

  In this way, if this parameter is not specified when calling, the default is CN. If it is specified, the value you specify will be used.

# key parameter
def stu_register(name, age, course='py', country="CN"):
    print("Registered student information".center(20, '-'))
    print("姓名", name)
    print("age", age)
    print("国籍", country)
    print("course", course)

stu_register("张三", course='py', age=22, country="CN")

  Under normal circumstances, the parameters are passed to the function in order. If you don’t want to use the order, you can use the key parameters. The parameter with the parameter name specified is called the key parameter. parameters) after

The call can be like this:

stu_register("张三", course='py', age=22, country="CN")

  但不可以这样:

stu_register("张三", course='py', 22, country="CN")

  

# 非固定参数
不确定用户想传入多少个参数,就可以使用非固定参数
def stu_register(name, age, *args):   # *args 会把多传入的参数变成一个元组形式
    print(name, age, args)
    
stu_register("张三", 22)

  输出:

张三 22 ()   # 后面这个()就是args,只是因为没有传值,所以为空

  

stu_register("张三", 22, "CN", "python")

  输出:

张三 22 ('CN', 'python')

  还可以有一个**kwargs

def stu_register(name, age, *args, **kwargs):   #  *kwargs会把多传入的参数变成一个dict形式
    print(name, age, args, kwargs)

  输出:

stu_register("张三", 22, "CN", "python", sex="M", province="湖南")
# 返回值
函数外部的代码要想获取函数的执行结果,就可以在函数里用return语句把结果返回
def stu_register(name, age, course='py', country="CN"):
    print("注册学生信息".center(20, '-'))
    print("姓名", name)
    print("age", age)
    print("国籍", country)
    print("课程", course)
    if age > 22:
        return False
    else:
        return True

registration_status = stu_register("张三", 22, course="python全栈开发", country="CN")

if registration_status:
    print("注册成功!")
else:
    print("注册信息有误!")

  注意:

  • 函数在执行过程中只要遇到return语句,就会停止执行并返回结果
  • 如果未在函数中指定retrurn,那这个函数的返回值为None

  

# 全局和局部变量
name = "张三"

def change_name(name):
    print("修改前的姓名:", name)
    name = "张三买了一辆特斯拉"
    print("修改后的姓名:", name)

change_name(name)
print("在外面看看name改了吗?", name)

  输出:

修改前的姓名: 张三
修改后的姓名: 张三买了一辆特斯拉
在外面看看name改了吗? 张三

  不用传name值到函数里,也可以在函数里调用外面的变量

name = "张三"

def change_name():
    name = "张三买了一辆特斯拉"
    print("修改后的姓名:", name)

change_name()
print("在外面看看name改了吗?", name)

  但就是不能改

  • 在函数中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量
  • 全局变量作用域是整个程序,局部变量作用域是定义该变量的函数
  • 当全局变量与局部变量同名时,在定义局部变量的函数内,局部变量起作用;在其它地方全局变量起作用

作用域

作用域:通常来说,一段程序代码中所用到的名字并不总是有效/可用的,而限定这个名字的可用性的代码范围就是这个名字的作用域。

 

在函数里修改全局变量

 

name = "张三"

def change_name():
    global name

    name = "张三买了一辆特斯拉"
    print("修改后的姓名:", name)

change_name()
print("在外面看看name改了吗?", name)
global name的作用就是要在函数里声明全局变量name,意味着最上面的name=“张三”,即使不写,程序最后面的print也可以打印name

# 嵌套函数
name = "张三"

def change_name():
    name = "张三丰"

    def change_name2():
        name = "张山峰"
        print("张山峰打印:", name)

    change_name2()    # 调用内层函数
    print("第二层打印:", name)


change_name()
print("最外层打印:", name)

  输出:

张山峰打印: 张山峰
第二层打印: 张三丰
最外层打印: 张三

  匿名函数

 

匿名函数就是不需要显式的指定函数名

 

#这段代码
def calc(x,y):
    return x**y

print(calc(2,5))

 

#换成匿名函数 
calc = lambda x,y:x**y
  print(calc(2,5))

  匿名函数主要是和其它函数搭配使用的

res = map(lambda x:x**2, (1, 5, 7, 4, 8))
for i in res:
    print(i)

  

输出

1
25
49
16
64

  

高阶函数

变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。

def add(x, y, f):
    return f(x) + f(y)

res = add(3, -6, abs)
print(res)

  

只需满足以下任意一个条件,即是高阶函数

  • 接受一个或多个函数作为输入
  • return返回另外一个函数

递归

在函数内部,可以调用其它函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。 

def calc(n):
    print(n)
    if int(n/2) ==0:
        return n
    return calc(int(n/2))

calc(10)

  

输出

10
5
2
1


修改一下代码实现:
def calc(n):
    v = int(n/2)
    print(v)
    if v > 0:
        calc(v)
    print(n)

calc(10)

  

输出

5
2
1
0
1
2
5
10
实现过程:

递归特性:

  • 必须有一个明确的结束条件
  • 每次进入更深一层递归时,问题规模相比上次递归都应有所减少
  • 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减少一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出)

 

 

内置函数

Python的len为什么你可以直接用?肯定是解释器启动时就定义好了

 几个刁钻古怪的内置方法用法提醒
#compile
f = open("函数递归.py")
data =compile(f.read(),'','exec')
exec(data)


#print
msg = "又回到最初的起点"
f = open("tofile","w")
print(msg,"记忆中你青涩的脸",sep="|",end="",file=f)


# #slice
# a = range(20)
# pattern = slice(3,8,2)
# for i in a[pattern]: #等于a[3:8:2]
#     print(i)
#
#


#memoryview
#usage:
#>>> memoryview(b'abcd')
#<memory at 0x104069648>
#在进行切片并赋值数据时,不需要重新copy原列表数据,可以直接映射原数据内存,
import time
for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = data
    while b:
        b = b[1:]
    print('bytes', n, time.time()-start)

for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = memoryview(data)
    while b:
        b = b[1:]
    print('memoryview', n, time.time()-start)

几个内置方法用法提醒
 

  

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325222282&siteId=291194637