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 :
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 calledIf 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>
f1(): ()<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():
()
()
f1()
案例3,结果为123、111,因为有return把值给了r,print(r)打印出来111 案例执行结果为:123、111 案例4:当函数无return的时候,自动默认返回值为None;返回结果为123、None,因为没有return,r接受到的值为None 案例执行结果为:123、none
案例5:python传递的是引用,不是复制,如下的li经过函数体执行后,已经被引用了 执行结果: [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> <br> 案例2:理解参数的调用 普通参数,x1、x2,在f1里面按顺序传递 默认参数,如果设置,则该形式参数必须放后面,如x3,调用时不用再次传递 指定参数,指定参数可以改变顺序指定 案例3:动态函数(函数名前加*,或者**):一个形式参数,可以接受多个实际参数。 当形式参数带*时,默认将传递的参数放置在群组中 当实际参数为普通参数传递时,即使列表,也会被作为一个元素传递 当实际参数有*时,list所有的元素将相应的作为元祖的每一个元素 执行结果: ('55', 66, 'll') ([11, 22, 33, 'hhhh'],) (11, 22, 33, 'hhhh') <br> 当为**时,默认传递的参数放置在字典中,实际参数必须为指定参数或字典 案例4:如果形式参数为**,传递实际参数的时候也传递**,则会把整个字典传进去 执行结果:<br> {'n1': 'hh', 'n2': 'kk'} {'k1': 'n1', 'k2': 'n2'} <br> 案例5:万能参数:f1(*args,**args),必须*在前,**在后 执行结果 (11, 22, 33) {'k1': 'n1', 'k2': 'n2'} <br> 关于万能参数的应用,就是str.format 案例6:用占位符传递,这样是*arg的应用 执行结果: i am hh,age2 i am hh,age2 <br> 案例7:当形式参数为字符变量时,必须后面指定参数传递,为**arg的应用 执行结果: i am hh,age2 i am nn,age4 <br> 5)全局变量:作用在全局,用大写表示,如果要修改且作用于全局,则需要加global 案例1:全局变量:作用域在全局,用大写表示。<br> 案例2:修改全局变量:如果要修改且对全局有用,则可以用global 执行结果: 案例3:修改全局变量:不加global,则仅作用在函数内部 执行结果: <br> 6)三元\三目运算:即if..else的简称。 格式为:"为真时的结果 if 判定条件 else 为假时的结果" , <br> 案例1:如果1==1条件成立,就等于前面的值,否则为后面的值hhh 6)lambda函数:目的就是简化用户定义使用函数的过程 案例1:lambda函数,简写函数,一个参数 #案例可以简写 案例2:lambda函数,简写俩参数 案例3:应用lambda函数
案例4:循环用法案例
7)python有很多内置函数,可以直接使用 可参考:http://www.cnblogs.com/vamei/archive/2012/11/09/2762224.htmlf1():
()
r=f1()
(r)
f1():
()
r=f1()
(r)
f1(a1):
a1.append()
li=[,,,]
(li)
f1(li)
(li)
f1(xxx):
()
xxx+r=f1()
(r)
<br>
f1(x1,x2):
x1+x2
r=f1(,)
f1(x1,x2,x3=):
x1+x2+x3
r=f1(,)
<br>
f1(x1,x2,x3=):
x1+x2+x3
f1(x2=,x1=)
(r)
(x,(x))
f1(,,)
li=[,,,]
f1(li)
f1(*li)
f1(**x):
(x,(x))
f1(=,=)
dic={:,:}
f1(**dic)
f1(*a,**x):
(a,(a))
(x,(x))
f1(,,,**{:,:})
s1=.format(,)
s2=.format(*[,])
(s1)
(s2)
=.format(=,=)
dic={:,:}
s2=.format(**dic)
()
(s2)
=f1():
age=(age,)
f2():
age = (age, )
f1()
f2()
NAME=f1():
age=NAME
NAME = (age,NAME)
f2():
age = (age, NAME)
f1()
f2()
=f1():
age= = (age,)
f2():
age = (age, )
f1()
f2()
“condition ? true_part : false_part”
==:
name=:
name=name2===
f1(a1):
a1+ret=f1()
(ret)
=a1:a1+r1=()
(r1)
=a1,a2:a1*a2+r1=(,)
(r1)
=[,,,,,]
key=w:[w]
r=key()
(r)
=n=alphabet=s3=[[:i]+c+[i+:] i () c alphabet]
(s3)
执行结果为:
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!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools