In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the python function parameters and return value is what the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe you read this python function parameters and return value is what the article will have a harvest, let's take a look at it.
The return value of the function
Multiple return values can be returned after a function is executed
Def measure (): print ('measurement begins.') Temp=39 wetness=50 print ("end of measurement") # tuples-can contain multiple data, so you can use tuples to return multiple values return (temp,wetness) result=measure () print (result) at a time
Running result:
The measurement begins.
End of measurement
(39, 50)
Def measure (): print ('measurement begins.') Temp=39 wetness=50 print ("end of measurement") # tuples-can contain multiple data, so you can use tuples to return multiple values return (temp,wetness) # if the type returned by the function is a tuple, and you want the element # in a separate processing tuple to use multiple variables, receive the result of the function at once # when using multiple variables to receive the result The number of variables should be the same as the number of elements in the tuple gl_temp,gl_wetness=measure () print (gl_temp) print (gl_wetness)
Running result:
The measurement begins.
End of measurement
(39, 50)
Exchange the values of two variables
A=6b=100# uses other variables c=aa=bb=c# does not use other variables a=a+bb=a-ba=a-b#python proprietary arecrint ("a's value is d" a) print ("b's value is d" b) the parameters of the function are immutable and variable
Within the function, using the assignment statement for the parameter does not change the value of the argument passed when the function is called
No matter whether the passed parameter is mutable or immutable, as long as the assignment statement is used for the parameter, the reference of the local variable will be modified inside the function, and the reference of the external variable will not be affected.
Def demo (num,num_list): print ("inside the function") # inside the function, using assignment statements for parameters changes the reference of formal parameters But the reference of the argument has not changed. Num=200 num_list= [1Magne 2jue 3] print (num) print (num_list) print ("function internal code completion") gl_num=99gl_list= [2Jing 3Jing 4] demo (gl_num,gl_list) print (gl_num) print (gl_list)
Running result:
Inside the function
two hundred
[1, 2, 3]
The internal code of the function is completed
ninety-nine
[2, 3, 4]
If the passed parameter is a mutable type, inside the function, the content of the data is modified using the method, which will also affect the external data.
Def demo (num_list): print ("inside the function") num_list.append ([1Magne2Magne3]) print (num_list) print ("function internal code completion") gl_list= [2mai 3dag4] demo (gl_list) print (gl_list)
Running result:
Inside the function
[2, 3, 4, [1, 2, 3]]
The internal code of the function is completed
[2, 3, 4, [1, 2, 3]]
+ =
In python, the list variable call + = essentially executes the extend method of the list variable and does not modify the reference to the variable
Def demo (num,num_lst): print ("function start") num+=num num_lst+=num_lst print (num) print (num_lst) print ("function end") gl_num=9gl_list= [1 gl_num,gl_list 2 Magi 3] demo (gl_num,gl_list) print (gl_num) print (gl_list)
Running result:
Function start
eighteen
[1, 2, 3, 1, 2, 3]
Function end
nine
[1, 2, 3, 1, 2, 3]
The default parameter of the function
When defining a function, you can assign a default value to a parameter, and a parameter with a default value is called a default parameter
If no default parameter value is passed in when the function is called, the parameter default value specified when the function is defined is used inside the function
(1) specify the default parameters of the function
Using an assignment statement after a parameter, you can specify the default value of the parameter
Def print_info (name,gender=True): gender_text= "boys" if not gender: gender_text= "girls" print ("% s is% s"% (name,gender_text))
(2) prompt
Default parameter, you need to use the most common value as the default value
If the value of a parameter cannot be determined, the default value should not be set, and the specific data is passed by the outside world when the function is called.
(3) matters needing attention of default parameters
1. The definition location of the default parameter
You must ensure that the default parameter with the default value is at the end of the parameter list
two。 Call functions with multiple default parameters
When calling a function, if there are multiple default parameters, you need to specify the parameter name so that the interpreter can know the corresponding relationship of the parameters.
Multi-valued parameter
Sometimes you may need an uncertain number of parameters that a function can handle, so you can use multi-valued parameters.
There are two kinds of multivalued parameters in python:
Add a * before the parameter name to receive tuples
Add two * before the parameter name to accept the dictionary
In general, when naming multi-valued parameters, it is customary to use the following two names:
* args---- stores tuple parameters
* * kwargs- stores dictionary parameters
Def demo (* args,**kwargs): print (args) print (kwargs) demo (1 age=14)
Running result:
(1, 2, 3, 4)
{'name':' test1', 'age': 14}
You can see that 1Jing 2Jing 3Jing 4 is formed into a tuple.
Name= "test1", age=14, is made up of a dictionary
# calculate the sum of any number of def sum_args (* args): sum=0 for n in args: sum+=n return sumresult=sum_args (1 result) unpack the print (result) tuple and dictionary
When calling a function with multi-valued arguments, if you want to:
Pass a tuple variable directly to args
Pass a dictionary variable directly to kwargs
You can use unpacking to simplify the transmission of parameters. The way to unpack is:
Add a * before the tuple variable
Add two * * before the dictionary variable
# if the package is not unpacked, the gl_args,gl_kwargs will be grouped into a tuple def demo (* args,**kwargs): print (args) print (kwargs) gl_args= (1 name 2, 3 age 4) gl_kwargs= {"name": "test1", "age": 12} demo (gl_args,gl_kwargs)
Running result:
((1,2,3,4), {'name':' test1', 'age': 12}) {}
If you do not unpack the package, the gl_args,gl_kwargs will be grouped into a tuple, and the correct way to pass it on should be: demo (1, age 2, 3, 4, name = "test", "age" = 12)
But arguments are tuples and dictionaries, so you need to take the elements apart one by one, so you need to unpack them.
The correct way to use:
Def demo (* args,**kwargs): print (args) print (kwargs) gl_args= gl_kwargs= {"name": "test1", "age": 12} demo (* gl_args,**gl_kwargs)
Running result:
(1, 2, 3, 4)
{'name':' test1', 'age': 12}
This is the end of the article on "what are the function parameters and return values of python". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what is the function parameter and return value of python". If you want to learn more knowledge, you are welcome to follow the industry information channel.
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.