In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the meaning of the function in python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the meaning of the function in python"?
Definition of function
Python function is a code block defined by def statement, which is composed of function name, parameter list and function body.
The statement that defines the function begins with def, followed by the function name and parentheses (), and ends with:.
The function's argument list must be enclosed in parentheses, separated by commas.
The first line of the function body can use multiple-line strings to describe the function, the number and type of parameters, and the number and type of values returned (the function description document can be written or not, it is recommended to write).
The function body is implemented by several lines of code, and the function body can be skipped by default or by writing pass.
Return [expression] ends the function and returns the value to the caller. If there is no return statement, it will return None after the code block of the function has finished running.
The following is an example of how a function is defined and the relevant information about the function.
Import sys # Import sys module def sum (XMagi y): # defines the function, which consists of the function name sum, the argument list xMague y, and the colon indicates the end of the def statement result = x + y # function body return result # return value print (sum) # sum is the name of the function, and the value of sum is the memory address of the function The type saved in dictionary format in the global namespace print (type (sum)) # sum is functionprint (id (sum)) # the id of the function is the decimal representation of the memory address print (sys.getsizeof (sum)) # you can see that the short three lines of code contain a lot of other information about the sum function, such as the data type is a function, parameter list information, function body learning, return parameter information, and so on. The conversion of out:1666835439472 # 0x0000018417225F70 to decimal is 1666835439472. The operation of 136 function
When testing in the repl environment, enter the function name and enter to show the type and memory address of the function, followed by () before the function can be executed.
> double = lambda x:x*2 > double > double (3) 6
If you want to run the function returned by the function, add more parentheses. For example:
Def func1 (): print ('function 1') def func2 (): print ('function 2') return func1func2 () () out: function 2 function 1 function is an object
Functions can be assigned to other variables.
Def sum (x, y): result = x + y return resulttemp=sumprint (temp (1)) out:3
Functions can be added to a list or dictionary.
Func_list = [] func_list.append (sum) func_list.append (sub) print (func_list [0] (3,2)) print (func_list [1] (3,2)) func_dict = {} func_dict ['sum'] = sumfunc_dict [' sub'] = subprint (func_dict ['sum'] (3,2) print (func_dict [' sub'] (3,2)) out:5151
A function can be passed to a function as an argument.
Def double (x): return 2 * xdef half (x): return 0.5 * xdef func (x, y): print (x (y)) func (double, 3) func (half, 4) out:62.0
A function can be used as the return value of a function.
Def double (x): return 2 * xdef half (x): return 0.5 * xdef func (): choice=input ('enter 1 to double, enter others to halve:') return double if choice=='1' else halfprint (func () (8)) out:4.0 # not selected # Select 1
At this point, I believe you have a deeper understanding of "what the function in python means". 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.