In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "example analysis of Python function parameters", with detailed contents, clear steps and proper handling of details. I hope that this "Python function parameter example analysis" article can help you solve your doubts.
1. Function parameter # 1. Position parameters: when calling a function The passed values need to be passed in the order of position # instance: find the n power of x def xPowN (XMagne n): # pass the values passed in the order of x while n: s = 1 while n > 0: n = n-1 s = s * x return sprint ("the value of 2 to the fifth power is:", xPowN (2p5)) print ("the value of 5 to the power of 2 is:" XPowN (5)) # result output: the value of the fifth power of # 2 is: the value of the second power of 3 times 5 is: 2 times 2. Default parameter # example: find the n-th power of x def xPowN (x while n = 2): s = 1 while n > 0: n = n-1 s = s * x return sprint ("the value of 2 to the 5th power is:", xPowN (2)) # when a new value is passed in, it is calculated using the new value, if not Then the default value of print ("5 to the second power is:", xPowN (5)) # n defaults to 2print ("- -") print ("- -") # Note for setting default parameters: # 1. The required parameters are first, and the default parameters are after; # 2. When the function has multiple parameters, put the parameters with large changes in front and the parameters with small changes after; # parameters with small changes can be used as default parameters; # 3. Using default parameters can reduce the difficulty of calling the function; # 4. When defining a default parameter, the default parameter must point to an immutable object, and if it is a mutable object, there is a problem For example: list# example: student information registration def studentInfo (name,gender,age,city = "Shenzhen"): print ("student's name is:", name) print ("student's gender is:", gender) print ("student's age is:", age) print ("the city where the student comes from:" City) print ("classmate 1's information is as follows:") print ("-") studentInfo ("Willard", "male") 18) # replace Shenzhen with the default parameter Shenzhenprint ("* *") print ("classmate 2 information is as follows:") print ("-") studentInfo ("ChenJD", "female", 18, "Maoming") # pass in the new parameter Maoming
# result output:
The value of the fifth power of 2 is: 32
The value of 5 to the power of 2 is: 25
-
-
The information of classmate 1 is as follows:
-
The student's name is: Willard
The gender of the student is: male
The age of the students is: 18
The city where the students come from is: Shenzhen
* *
The information of classmate 2 is as follows:
-
The student's name is: ChenJD
The gender of the student is: female
The age of the students is: 18
The city where the students come from is: Maoming
# 3. Variable parameters: the number of parameters passed in is variable, which can be 0, 1, 2 or any # when parameters are passed in the traditional way You can import # instances (traditional way) as list or tuple: def sumOfN (intNumbers): sum = 0 for n in intNumbers: sum = sum + n return sumprint ("the first 100 items add up to:", sumOfN (range (101) print ("the first 50 items add up to:", sumOfN (range (51)) print ("any items add up to:", sumOfN ([1pm2pr 5pr 6pr 7pr 7pr 7pr 9je 10]) print ("any items add up to:" SumOfN ((1, 2, 3, 4, 5, 7, 8, 9) (12) print ("- -") # variable parameters: add the * sign def sumOfN (* intNumbers): sum = 0 for n in intNumbers: sum = sum + n return sumprint before the parameter ("the sum of any items is:", sumOfN (1, 2, 3, 4, 6, 7) 8) print ("the sum of any items is:", sumOfN (1) (2)) print ("the sum of any items is:", sumOfN (3), 4, 5, 6 7) print ("- -") # pass list or tuple as a variable parameter to def sumOfN (* intNumbers): sum = 0 for n in intNumbers: sum = sum + n return sum# already has list * nums means to pass all the elements of nums as variable parameters to nums = [1 list 2, 3 5] print ("pass in a list:", sumOfN (* nums))
# result output:
The sum of the first 100 items is: 5050
The sum of the first 50 items is: 1275
The sum of any items is: 48
The sum of any items is: 51
The sum of any items is: 40
The sum of any items is: 7
The sum of any items is: 25
Pass in a list: 11
# 4. Keyword parameter # variable parameter allows any parameter to be passed in. These variable parameters are automatically assembled into a tuple;# keyword parameter when the function is called. Any parameter with a parameter name is allowed to be passed, and these keyword parameters are automatically assembled into a dict inside the function. Def personInfo (name,age,**kw): print ("name:", name, "age:", age, "other:", kw) print ("Willard personal Information:") personInfo ("Willard" 18) print ("-") print ("Willard more detailed personal information:") personInfo ("Willard", 18pm gender = "male") Job = "Enginner") print ("-") print ("ChenJD personal Information:") personInfo ("ChenJD", 18 Gender = "female") print ("-") # keyword parameter: function that can be extended # Eg: user registration function User name and age are required. Other information is optional. # you can assemble dict first. Then the dict is converted to the keyword parameter extraInfo = {"gender": "Man", "city": "Shenzhen", "job": "Engineer"} print ("hackerLuo personal information is as follows:") personInfo ("hackerLuo", 18 gender = extraInfo ["gender"] Job = extraInfo ["job"]) print ("-") # use the simplified writing method print ("simplified writing method is passed into hackerLuo personal information:") personInfo ("hackerLuo", 18memorialInfo)
# result output:
Willard personal Information:
Name: Willard age: 18 other: {}
-
Willard's more detailed personal information:
Name: Willard age: 18 other: {'gender':' male', 'job':' Enginner'}
-
ChenJD personal Information:
Name: ChenJD age: 18 other: {'gender':' female'}
-
HackerLuo personal information is as follows:
Name: hackerLuo age: 18 other: {'gender':' Man', 'job':' Engineer'}
-
Simplify the method of writing and introduce hackerLuo personal information:
Name: hackerLuo age: 18 other: {'gender':' Man', 'city':' Shenzhen', 'job':' Engineer'}
# 5. Named keyword parameters # function callers can pass any unrestricted keyword parameters; # which parameters are passed in need to pass the kw check in the function Def personInfo (name,age,**kw): if "city" in kw: pass if "job" in kw: pass print ("name:", name, "age:", age, "other:", kw) # if you want to restrict the names of keyword parameters, you can use named keyword parameters # receive gender,job only as keyword parameters # naming keyword parameters need * as delimiters * the following parameter is regarded as named keyword parameter # named keyword parameter must pass the parameter name def personInfo (name,age,*,gender,job): print (name,age,gender,job) # call method: print ("named keyword parameter call!") PersonInfo ("Willard", 18 gender = "male", job = "Engineer") print ("-") # if there is already a variable parameter in the function definition Then the named keyword parameter does not need the * delimiter def personInfo (name,age,*args,gender,job): print (name,age,args,gender,job) # the default value of the named keyword parameter def personInfo (name,age,*,gender,job = "Engineer"): print (name,age,gender,job) print ("named keyword parameter has a default value!") PersonInfo ("Willard", 18female gender = "male")
# result output:
Name keyword parameter call!
Willard 18 male Engineer
-
The named keyword parameter has a default value!
Willard 18 male Engineer
# 6. Parameter combination # Common parameters: required parameters, default parameters, variable parameters, keyword parameters, named keyword parameters # Parameter definition order: required parameters, default parameters, variable parameters, named keyword parameters, keyword parameters def egFunc1 (name,age,gender = "male", * args,**kw): print ("name:", name, "age:", age, "gender:", gender, "args:", args "kw:", kw) def egFunc2 (name,gender = "male", *, job,**kw): print ("name:", name, "gender:", gender, "job:", job, "kw:", kw) # call print ("egFunc1 passes name and age parameters:") egFunc1 ("Willard") 18) print ("-") print ("egFunc1 is passed into name" Age Gender parameter: ") egFunc1 (" ChenJD ", 18," female ") print ("-") print (" egFunc1 input name,age,gender,args parameter: ") egFunc1 (" hackerLuo ", 18," male " ) print ("-") print ("egFunc1 input name,age,gender,args,kw parameter:") egFunc1 ("Willard", 18, "male", 170) 60 Job = "Engineer") print ("-") print ("egFunc2 input name,gender,job,kw parameter:") egFunc2 ("Willard", job = "Engineer" Ext = None) print ("-") print ("egFunc1 passes parameters through tuple and dict:") args = ("Willard", 18, "male", 170J 60) kw = {"job": "Engineer"} egFunc1 (* args,**kw) # Tips:# 1. The default parameter must be an immutable object. If it is a mutable object, a logic error occurs when the program runs; # 2.*args is a variable parameter, args receives a tuple;# 3.**kw keyword parameter, and kw receives a dict;# 4. Variable parameters can be passed in directly: func (1 args 2), or you can assemble list or tuple,# first and pass in: func (* (1 meme 2 Mage3 4)); # 5. Keyword parameters can be passed directly: func (a = 1), or you can assemble dict,# first and pass it through * * kw: func (* * {"a": 1, "b": 2}); # 6. The named keyword parameter needs to write the delimiter * without variable parameters.
# result output:
EgFunc1 passes name and age parameters:
Name: Willard age: 18 gender: male args: () kw: {}
-
EgFunc1 input name,age,gender parameters:
Name: ChenJD age: 18 gender: female args: () kw: {}
-
EgFunc1 input name,age,gender,args parameters:
Name: hackerLuo age: 18 gender: male args: (170,60) kw: {}
-
EgFunc1 input name,age,gender,args,kw parameters:
Name: Willard age: 18 gender: male args: (170,60) kw: {'job':' Engineer'}
-
EgFunc2 input name,gender,job,kw parameters:
Name: Willard gender: male job: Engineer kw: {'ext': None}
-
EgFunc1 passes parameters through tuple and dict:
Name: Willard age: 18 gender: male args: (170,60) kw: {'job':' Engineer'}
two。 Recursive function # inside the function, you can call other functions or the function itself Then calculate the factorial for the recursive function # factorial: return 1 × 2 × 3... × ndef fact (n): if n = = 1: return 1 return n * fact (n-1) print ("5 factorial is:", fact (5)) print ("factorial is:", fact (50)) # print ("factorial is:", fact (10000)) # print ("10000 factorial is:", fact (10000)) # stack overflow # in the computer The function call is realized through the stack data structure. Each time a function call is entered, the # stack will add a layer of stack frames. When the function returns, the stack will be reduced by one layer of stack frames, and too many recursive calls will lead to stack overflow. # readers try fact (1000), fact (10000), maybe fact (1000) has stack overflow, # author tests fact (1000) can still output normally, fact (10000) stack overflows
# result output:
The factorial of 5 is: 120
The factorial of 100 is 30414093201713378043612608166064768844377641568960512000000000000
After reading this, the article "instance Analysis of Python function parameters" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, 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.