Summary of functions of Python basic knowledge

零到壹度
Release: 2018-04-02 09:46:34
Original
1550 people have browsed it

This article shares with you a summary of functions of Python basic knowledge. The content is quite good. I hope it can help friends in need.

Functions: including custom functions and built-in functions <br>

1) Custom functions Structure: Contains five parts:

  • ##def Keywords: Identifies the function used to create

  • Function name: For example, f1

  • ():( ) There are parameters in it

  • Function body:Specific function to be achieved by this function

  • return: Return value, if none, return none

as shown below :

Summary of functions of Python basic knowledge

2) Function call: Use function name + ()

The form is: function name (parameter 1, parameter 2), such as f1 (5,8)

##3) Function execution order:

from top to bottom Down.

And the function body is only executed when it is called

If you want to get the return value of the function, you need to assign a value.

The statement after return in the function body will no longer be executed.

<br>

Summary of functions of Python basic knowledge

Execution result:

Summary of functions of Python basic knowledge

Case 1 : Custom function, because the function is not called, the function body is not executed

f1():
()
Copy after login
<br>

<br>

Case 2, call the function and execute the function body. Once return is executed in the function body, it terminates immediately, so the subsequent print (456) will never be executed.

f1():
()
()
Copy after login
f1()
Copy after login

The case execution result is: 123

案例3,结果为123、111,因为有return把值给了r,print(r)打印出来111

f1():
()
r=f1()
(r)
Copy after login
Copy after login

案例执行结果为:123、111

案例4:当函数无return的时候,自动默认返回值为None;返回结果为123、None,因为没有return,r接受到的值为None

f1():
()
r=f1()
(r)
Copy after login
Copy after login

案例执行结果为:123、none

案例5:python传递的是引用,不是复制,如下的li经过函数体执行后,已经被引用了

f1(a1):
    a1.append()
li=[,,,]
(li)
f1(li)
(li)
Copy after login

执行结果:

[11, 22, 33, 44]

[11, 22, 33, 44, 999]

<br>

4)函数的参数:<br>

比如f(x1,x2,x3=1),x1,x2,x3则为参数

包含的参数类型有:

            普通参数:形式参数和实际参数<br>

            默认参数:提前给定值,比如x3<br>

            指定参数:实际参数调用时,可以改变顺序指定

            动态参数:<br>

                *args<br>

                **kargs<br>

                万能参数*args,**kagrs

<br>

案例1:区分形式参数和实际参数:

案例中的xxx为形式参数,调用函数时传递的为实际参数<br>

f1(xxx):
()
xxx+r=f1()
(r)
Copy after login

<br>

案例2:理解参数的调用

普通参数,x1、x2,在f1里面按顺序传递

<br>
Copy after login
Copy after login
f1(x1,x2):
x1+x2
r=f1(,)
Copy after login

默认参数,如果设置,则该形式参数必须放后面,如x3,调用时不用再次传递

f1(x1,x2,x3=):
x1+x2+x3
r=f1(,)
Copy after login

指定参数,指定参数可以改变顺序指定

<br>
Copy after login
Copy after login
f1(x1,x2,x3=):
x1+x2+x3
f1(x2=,x1=)
(r)
Copy after login

案例3:动态函数(函数名前加*,或者**):一个形式参数,可以接受多个实际参数。

当形式参数带*时,默认将传递的参数放置在群组中

  • 当实际参数为普通参数传递时,即使列表,也会被作为一个元素传递

  • 当实际参数有*时,list所有的元素将相应的作为元祖的每一个元素

(x,(x))
f1(,,)
li=[,,,]
f1(li)
f1(*li)
Copy after login

执行结果:

('55', 66, 'll')

([11, 22, 33, 'hhhh'],)

(11, 22, 33, 'hhhh')

<br>

当为**时,默认传递的参数放置在字典中,实际参数必须为指定参数或字典

案例4:如果形式参数为**,传递实际参数的时候也传递**,则会把整个字典传进去

f1(**x):
(x,(x))
f1(=,=)
dic={:,:}
f1(**dic)
Copy after login

执行结果:<br>

{'n1': 'hh', 'n2': 'kk'}

{'k1': 'n1', 'k2': 'n2'}

<br>

案例5:万能参数:f1(*args,**args),必须*在前,**在后

f1(*a,**x):
(a,(a))
(x,(x))
f1(,,,**{:,:})
Copy after login

执行结果

(11, 22, 33)

{'k1': 'n1', 'k2': 'n2'}

<br>

关于万能参数的应用,就是str.format

案例6:用占位符传递,这样是*arg的应用

s1=.format(,)
s2=.format(*[,])
(s1)
(s2)
Copy after login

执行结果:

i am hh,age2

i am hh,age2

<br>

案例7:当形式参数为字符变量时,必须后面指定参数传递,为**arg的应用

=.format(=,=)
dic={:,:}
s2=.format(**dic)
()
(s2)
Copy after login

执行结果:

i am hh,age2

i am nn,age4

<br>

5)全局变量:作用在全局,用大写表示,如果要修改且作用于全局,则需要加global

案例1:全局变量:作用域在全局,用大写表示。<br>

=f1():
    age=(age,)
f2():
    age = (age, )
f1()
f2()
Copy after login

Summary of functions of Python basic knowledge

案例2:修改全局变量:如果要修改且对全局有用,则可以用global

NAME=f1():
    age=NAME
    NAME = (age,NAME)
f2():
    age = (age, NAME)
f1()
f2()
Copy after login

执行结果:

64Summary of functions of Python basic knowledge

案例3:修改全局变量:不加global,则仅作用在函数内部

=f1():
    age= = (age,)
f2():
    age = (age, )
f1()
f2()
Copy after login

执行结果:

Summary of functions of Python basic knowledge

<br>

6)三元\三目运算:即if..else的简称。

格式为:"为真时的结果   if 判定条件    else 为假时的结果" ,

“condition ? true_part : false_part”
Copy after login


案例1:如果1==1条件成立,就等于前面的值,否则为后面的值hhh

==:
    name=:
    name=name2===
Copy after login

6)lambda函数:目的就是简化用户定义使用函数的过程

案例1:lambda函数,简写函数,一个参数

f1(a1):
a1+ret=f1()
(ret)
Copy after login

#案例可以简写

=a1:a1+r1=()
(r1)
Copy after login

案例2:lambda函数,简写俩参数

=a1,a2:a1*a2+r1=(,)
(r1)
Copy after login

案例3:应用lambda函数

=[,,,,,]
key=w:[w]
r=key()
(r)
Copy after login

案例4:循环用法案例

=n=alphabet=s3=[[:i]+c+[i+:] i () c alphabet]
(s3)
Copy after login

执行结果为:
Copy after login

Summary of functions of Python basic knowledge

7)python有很多内置函数,可以直接使用

可参考:http://www.cnblogs.com/vamei/archive/2012/11/09/2762224.html

Summary of functions of Python basic knowledge

The above is the detailed content of Summary of functions of Python basic knowledge. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!