In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "Python learning how to understand the function def", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "Python Learning how to understand the function def"!
I. Overview of functions
To put it simply, a function is a self-defined piece of Mini Program that is convenient for you to call and use.
The keyword used by def to define the function is the name of the function.
When the function runs to return, it will return the value and empty all the parameters in the function. The variable will no longer occupy the memory library space to exit the function.
The format of the function is as follows: # x is the incoming parameter, or you don't have to enter a value, because all the values in the function are written to death.
Def CeShi (x):''to test the value of the calculated number multiplied by 3 minus 2: param x: enter a number: return: return the result of a calculation''y = Xerox 3-2 return ya = CeShi (5) print (a) # 13
To call a function is to add () to the function name.
Function to simplify recurring code
The code can be called repeatedly and is easy to maintain and modify
Second, the call of function
Calling the function is very simple and you can use the function name plus () as follows
Def CeShi (x):''is used to test the calculated value multiplied by 3 minus 2: param x: enter a number: return: return the result of a calculation''y = xtriple 3-2 return y a = CeShi (5) # call function CeShi and input value is 5print (a) # 13 III. Rules for the use of function parameters
The parameters are divided into formal parameters and actual parameters.
The parameter allocates the memory unit only when it is called, and the lock allocated memory is released after the call ends.
Arguments are variables that currently have values.
1. Position parameters
The position parameter standard call value and keyword correspond one to one, more or less in order.
Def CeShi: print (x) print (y) print (z) CeShi (1meme 2je 3jue 4)
For example, the number of these four parameters here must be the same as the number of parameters needed in the function, and they assign values to the wrecoery x _ memory _ z ~ y in the function in order of position, so their positions are also very important and cannot be confused.
2. Keyword parameter assignment
Not in order, but the quantity must be equal to the number of values required by the function, always in the form of keywords such as x = 1, b = 3.
Def CeShi: # y will include the input 2 through the last element in it. If there is only one value, it doesn't matter. Print (w) print (x) print (y) print (z) CeShi, position parameter and keyword parameter are mixed.
The position parameter must be to the left of the keyword parameter
Keywords cannot be assigned twice in a single call and cannot overwrite the previous value.
Def CeShi: # y will include the input 2 through the last element. If there is only one value, it doesn't matter. Print (w) # 1 print (x) # 2 print (y) # 3 print (z) # 4CeShi (1m 2m z = 3le y = 4)
PS:
If the parameter value is already defined in the function, the existing parameter value of the function will be used by default when it is called.
When you customize the parameter values of the function, the original parameter values of the function will be overwritten.
4. Parameter group * * dictionary * list
* args accepts location parameters
* * kwargs accepts keyword parameters
* args, * * kwargs must be expressed in this way because the position parameter must be on the left side of the keyword parameter.
5. * list
No matter how many elements y are after the first element, the meta-ancestor will be packaged as a whole, and if the meta-element has no element, it will be regarded as no element will return an empty meta-ancestor.
Def CeShi (xmemeny): # y will include the input 2 through the last element. It doesn't matter if there is only one value. Print (x) # 1 print (y) # (2) CeShi (1)
If the value passed to y is multiple, for example, if the list meta-ancestor dictionary is preceded by a *, it will be passed into y in turn and will not be treated as a whole element assigned to y, but if the dictionary can retain keys.
Def CeShi (xmemery): # y will include the input 2 through the last element. If there is only one value, it doesn't matter print (x) # 1 print (y) # ((2) CeShi (1, (2) print (x) # 1 print (y) #) CeShi (1) 6]) # you can enter the list of meta-ancestor dictionaries here by def CeShi (x): # print (x) # 1 print (y) # ('axie,' baked,'c') CeShi (1) * {'axiang: 2) * * input dictionary 6, * *
The return value will be presented in the form of a dictionary. Only keyword parameters can be placed here.
Def CeShi: # y encapsulates a = 1, b = 2, c = 3 in the form of dictionary print (x) # 1 print (kwargs) # {'axie: 1,' baked: 2, 'cantilever: 3} CeShi (1Magi a = 1, b = 2, c = 3)
If you give
* * if the kwargs assignment itself is a dictionary, you need to add * * before the dictionary.
Def CeShi: # y encapsulates a = 1, b = 2, c = 3 in the form of a dictionary print (x) # 1 print (kwargs) # {'axiao: 1,' baked: 2, 'cased: 3} CeShi (1) * {' axiang: 1, 'baked: 2,' cased: 3})
You can also write books in the following form to facilitate the acceptance of any type of data.
Def CeShi: # print (x) # 1 print (args) # (2, 3, 4, 5) print (kwargs) # {'axie: 1,' baked: 2, 'cynical: 3} CeShi (1)
PS:
A procedure is a function that does not return a value
But in python, even if there is no defined return value, python will default to a return value that is None.
Usually return a value, but if multiple values are returned, it will be returned in the form of meta-ancestor, that is, the element will be packaged and returned.
At this point, I believe you have a deeper understanding of "Python learning how to understand the function def". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.