In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "python function application example", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn the "python function application example" bar!
How to define a function
Function is also called method. Here is a simple function:
Def function (): print ("I am a function.")
This is a very simple function that does only one thing and prints out "I am a function." This sentence.
From the appearance of this function, there is a def and a function plus () and:.
Def is the keyword that defines the function can not be changed, function is the name of the function, you can write, followed by () and: this is the rule.
The output statement in the function can be called the function body all the function bodies need to be indented, otherwise that will report an error.
How to use
The way to use it is simple.
Function ()
Repeat the function name and add parentheses, you can use it.
What are parameters?
Next I'll write a function:
Def function (aformab): print (ameme b)
In this function, the an and b in parentheses are called parameters (function parameters), also called formal parameters. What are the parameters for? The function of the parameter is to pass something into the objective function.
For example, in this function, you can print the parameters passed in outside:
Function ("aaa", "bbb")
I passed two parameters to this function. The position of the parameter cannot be changed. "aaa" can become the actual parameter, corresponding to the formal parameter a, and "bbb" corresponds to b. The passed-in process is passed as the parameter.
The order of the result output is:
Aaa bbb
You can also pass parameters like this:
Function (a = "aaa", "bbb") function ("aaa", b = "bbb") function (a = "aaa", b = "bbb") function (b = "bbb", a = "aaa")
When there are parameters in the defined function, when you use it, you must pass in the parameters or an error will be reported, such as:
Def function (a): print (a) function ()
Running result:
TypeError Traceback (most recent call last)
In
2 print (a)
three
-> 4 function ()
TypeError: function () missing 1 required positional argument:'a'
Variable length parameter
What if I don't know how many parameters are passed?
It can be implemented with * and *.
Parameters with * are passed in the form of tuple, storing all unnamed variable parameters, such as:
Def function (* args): print (args) function
Running result:
(12, 35, 65)
It is found that the output is a tuple containing all the passed parameters
Parameters with two asterisks * * are passed in the form of a dictionary
Def function (* * kwargs): print (kwargs) function
Note that the parameters passed here are key-value pairs.
When an asterisk and two asterisks appear at the same time, an asterisk must precede the two asterisks, such as:
Def function (* args, * * kwargs): what is print (args) print (kwargs) return?
If return appears in the function, it means that the run of the function ends here, and it will not be executed no matter how much it is. And return returns the value of its subsequent expression, which is equivalent to assigning the latter value to the function, for example:
Def function (): print ("aa") return "aaa" print ("bb") print (function ())
Running result:
Aa
Aaa
It is found that the statement after return exits without executing the function, and the function itself has a value.
Thank you for your reading, the above is the content of the "python function application example", after the study of this article, I believe you have a deeper understanding of the python function application example, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.