For example, now I have one of the simplest addition (JIAFA) functions:
def jiafa (x, y):
z = x + y
Return Z
Print (JIAFA)
This is very simple, you can see that the output equals 3.
The next question is, how do I calculate if I want to add a number that is not a fixed number?
At this point, the use of args and *kwarg can help us solve this problem.
*args: Can be understood as only one column of the table, the length is not fixed.
**kwargs: Can be understood as a dictionary, the length is not fixed.
First,args and kwarg do not have to appear in pairs, nor must they be called, or X and **y. Written in this way, it is only a kind of conventions, such as telling people stories: once there was a Zhang San and John Doe ... Everyone knows what you are going to say, but you can't say there was a horse seven dog eight, although everyone can understand, but always not very good understanding.
First say the role of args, or the beginning of the case, we want to calculate the length of the addition, you can use the args to define, of course, can also be called x,y.
def Jiafa (*args):
sum = 0
For i in args:
sum = sum + I
Print (sum)
Jiafa (1, 3, 5)
Jiafa (2, 4, 6, 8,)
Output results, 9 and 20. This case is simple and can be introduced with the *args definition, which is equivalent to defining a function of indefinite length, which can then be used more than once in a program.
What about **kwargs's dictionary? First look at the ordinary dictionary, with a pair of curly braces {} Can create a dictionary, such as the following 3 lines of the program, you can compile a dictionary program:
Dict = {"System": "Systems", "China": "Chinese", "link": "Join"}
x = input ("Please enter an English word:")
Print (Dict.get (x, "not found in this dictionary!"))
If the input is correct, you will get an answer, otherwise it will not be found.
In this program, Dict = {"System": "Systems", "China": "Chinese", "link": "Join"} created three pairs of "keys and Values" (Key and value), such as "system" is key, "systems" is the value corresponding to the key, also called the key value.
You can also write a small software that tests words.
Dict = {"System": "Systems", "China": "Chinese", "link": "Join"}
To find the statement of value by key:
y = input ("Please enter China's Chinese meaning:")
If dict[' china '] = = y:
Print ("Congratulations, you got it!" ")
A statement that uses value to find key:
z = input ("Enter the system" in the English word: ")
If List (Dict.keys ()) [List (Dict.values ()). Index ("system")] = = Z:
Print ("Congratulations, you got it!" ")
Now the problem, if you do not know what is in the dictionary, need to be in the program to know what to do? You can use Kwargs to define it at this time . We first use Kwargs to define a function, the function content is not written first, and then look at the following small program:
def dict (**kwargs):
Return Kwargs
Mydict = Dict (system= "System", china= "China", link= "join")
x = input ("Please enter the word:")
If x in Mydict.keys ():
Print ("Chinese meaning:", mydict[x])
Else
Print ("Sorry, not found.") ")
A dictionary can also be used to achieve such a function, using In......keys (), is to determine whether the key exists, if present, return its value. Similarly, the ability to pass data by using **kwargs can also be designed as a user-logged program:
def dict (**kwargs):
Return Kwargs
UserDict = Dict (user1= "1234", user2= "5678")
x = input ("Please enter user name:")
If x in Userdict.keys ():
y = input ("Please enter password:")
If userdict[x] = = y:
Print ("absolutely correct")
Else
Print ("Bad password!") ")
Else
Print ("User does not exist! ")
The above functions, are able to express the function of the args and *kwarg the most refined code, you will be more helpful to understand the exercise. Hope you learn the preliminary functions, common progress, refueling!
At the same time, also to see the master of this article to ask a question, why directly define a dictionary, can not be used dict = {system= "system", china= "China", link= "join"}, not to use Dict = {"System": "Systems", "China": "Chinese", "Link": "Join"}? What difference does it have?
The Almighty Old J
Links: https://www.jianshu.com/p/0d5dfd40d518
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.
Summary of usage of args and Kargs in Python