进击 of python
Function basis (a): Parameter
== it clear that Austria, if you have not read the article front, then I suggest you do not look directly at the ==
== looked as a waste of time, does not make sense ==
Well, then Python is part of the soul - function
In the course of history, we encounter complex problems, people always want to be able to simplify handling of the issue
Thus, the module ah, ah, what functions, including various algorithms on the heels of the
That function was originally how to come out of it?
In the original ah, people encountered such a problem
# 报警装置,出现问题,发送问题到邮件
It is quite normal demand
How to write it (pseudo-code)
A = 故障
if A == "警报故障"
打开QQ邮箱
编辑警报故障邮件
发送邮件
关闭QQ邮箱
if A == "排水故障"
打开QQ邮箱
编辑排水故障邮件
发送邮件
关闭QQ邮箱
if A == "储水故障"
打开QQ邮箱
编辑储水故障邮件
发送邮件
关闭QQ邮箱
... ...
So people started to write, but soon it was discovered the problem
This code there are a lot of repeat statements
While it is possible when "CV Engineer", but humans still have to progress
So people think
These repeated, repeated use of the code statement on the "box", when used directly to box out
Thus, the function appeared!
This code can become so (do not ask why, look at the effect!):
def send_mail(x):
打开QQ邮箱
编辑x邮件
发送邮件
关闭QQ邮箱
A = 故障
send_mail(A)
Is not simply too much!
The reality finished, then start learning function journey!
grammar
The syntax is a very important aspect to note the following:
There must be a space behind def
Function names follow the variable naming rules
There are brackets behind the function name ()
Brackets are behind a colon:
def send_mail(): # 定义一个名为 send_mail 的函数
Then the first line indent, write your code and write code as normal
def send_mail(): print("这是一个函数")
Well, part of the function code is finished
You can not perform is carried out, and why? Because you did not call
How to call? Good question!
In fact, we wrote this in front
print()
This is the Python comes with a function called print of
That our custom function named send_mail how to call it?
send_mail()
I can understand that it (would not understand it again !!!)
parameter
In fact, you should just have a question
I have just introduced when the function definition, () it was empty, but inside is my example (x)
What this does is x?
It touches your knowledge to the blind
Then talk about two concepts
Arguments parameter
Arguments:
You see people from the name, arguments
What is the argument? Real parameters, or
The real parameters
for example:
print("App")
This App is my data to process, because I want to print this right
That he is the parameter
I know the contents of the parameter App Well it is not
So this App is the real argument is the argument!
Understand applause!
This means that you listen to, as well as untrue parameters chant?
Katachisan:
Formal parameters, that is not true parameter
Well, talking examples
def send_mail(x):
Come on, I was not trying to deal with this by x function?
That this is not the x parameter?
Then you tell me, what x is?
1? 2? Cat? dog?
I do not know it!
That's parameter!
The default parameters
Do you think you two kinds of parameters? ? Ausferrite want more juice!
To really so simple, how can you call it soul?
What is the default? Is that you do not change the default output something he
For example, you select a city some App when it is not the default is Beijing?
The default is that the default parameters
For example
def city(x="北京"): print(x) city() city("上海")
Why is this?
When you do not give this city function variables when the city function to use the default "Beijing" parameter passed to x; and when you give it a "Shanghai" parameter when you use your arguments to x
understand? Minato brother!
Positional parameters
Positional parameters! Is a carrot a pit!
def func(x, y, z): print("x:", x) print("y:", y) print("z:", z) func(1, 2, 3)
Order one by one, nothing to speak of this.
key parameter
Look, it came out a parameter!
The key parameters, in my opinion, should be called keyword arguments!Like a key-value dictionary mode
def func(x, y, z): print("x:", x) print("y:", y) print("z:", z) func(y=1, z=2, x=3)
Simple is not? understand? It is not quite like key-value dictionary modes!
Non-fixed parameters
The last Austrian, Shajiao non-fixed parameters?
We still look at this function:
def func(x, y, z): print("x:", x) print("y:", y) print("z:", z)
I want to use this function, but I want to pass four parameters how to do? Direct write?
func(1,2,3,4)
Some will complain! 4 Because you are not the parameter variables Next, you will complain ah!
Ever since, the clever "lazy" who would come up with this stuff
*args
Do not ask, would be finished by!
He is a container you extra positional arguments, in the manner of tuples stored up
def func(x, y, z,*args):
print("x:", x)
print("y:", y)
print("z:", z)
print("其他",*args)
func(1,2,3,4,5)
ok not? I do not understand?
Then again a stimulus!
** kwargs
What the hell? ? ? ? ? what is this? ? ? ? ? ?
This is a key parameter overflow storage container
def func(x, y, z, *args, **kwargs):
print("x:", x)
print("y:", y)
print("z:", z)
print("其他", *args)
print("**kwargs:", kwargs)
func(1, 2, 3, 4, 5, key="hello")
It is stored in the form of the dictionary, ha ha ha ha ha ha ha ha ha ha! ! ! ! ! ! ! ! !