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/02 Report--
This article mainly explains "how to customize the function in python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to customize the function in python".
Custom functions are supported in python
Begins with def, followed by the identifier name and parentheses (). Parentheses contain parameters inside the parentheses
Parentheses are followed by a colon and the next line is indented
End with return [expression]
# Custom function
Def printme (str):
Print (str)
Return
# call function 1
Printme ("I call myself")
Printme as a custom function, str as a parameter, the print (str) inside the custom function performs the operation of printing the parameters, and ends with return.
Parameters in def can be divided into immutable types and variable types.
Strings\ tuples\ numbers is immutable
List\ dict and so on are mutable types
# immutable type
Def ChangeInt (a):
A = 10
B = 2
ChangeInt (b)
Print (b) # the result is 2
You can see that b, as a numbers, does not change after the passed-in parameters of b.
# variable Type 1
Def changec (c):
C [2] = 1
Return
Cc = [3,3,3]
Changec (cc)
Print (cc)
You can see that cc, as a list, has changed after the introduction of c, and the result is 3meme3re1. Here is an experiment when the internal function c [2] = 1 is changed to c = [1, 1, 1], the function does not change, friends are interested and can explain why.
# variable Type 2
Def changeme (mylist):
"modify incoming list"
Mylist.append ([1,2,3,4])
Mylist [2] = 55
Print ("value within function:", mylist)
Return
# call changeme function
Mylist = [10,20,30]
Changeme (mylist)
Print ("function extrapolation:", mylist)
The function of append here is to add [1, 2, 3, 4] matrices to the right of the matrix, and the same is true here, and the final output is
Value in function: [10, 20, 55, [1, 2, 3, 4]]
Function external value: [10, 20, 55, [1, 2, 3, 4]]
Parameters in def can be matched automatically
# Parameter names match parameter values
Def printme (str):
"print any incoming strings"
Print (str)
Return
# call printme function
Printme (str= "My string")
Here is the principle that the parameter name matches the parameter value. After setting the parameter to str, you can print str= "XXX" and python will automatically identify it.
# the order of parameters does not matter which hospital has a good http://mobile.sptdnk.com/ in Zhengzhou
Def printinfo (name, age):
"print any incoming strings"
Print ("Name:", name)
Print ("Age", age)
Return
# call printinfo function
Printinfo (age=25, name= "wly")
The order of the parameters is not important for the transmission. Here, the reverse order is automatically corrected by python.
Parameters in def can be assigned initial values
# default parameters can be defaulted when there is an initial amplitude
Def printinfo1 (name, age=25):
"print any incoming strings"
Print ("Name:", name)
Print ("Age", age)
Return
# call printinfo function
Printinfo1 (name= "wqr")
When there is an initial value assignment, the default parameters can be defaulted. If there is an initial value assignment, null can be passed.
Indefinite length parameters in def
# variable length parameter
Def printinfo (arg1, * vartuple):
"print any incoming parameters"
Print ("output:")
Print (arg1)
For var in vartuple:
Print (var)
Return
# call printinfo function
Printinfo (10)
Printinfo (70, 60, 50, 40)
The variable length parameter is represented by * XXX. When calling the function, it can be greater than any number of parameters. The function of for is to print circularly within the number of elements of the vartuple, resulting in
Output:
ten
Output:
seventy
sixty
fifty
forty
# Anonymous functions can contain only one statement lambda [arg1 [, arg2,.argn]]: expression
Sum = lambda arg1, arg2: arg1 + arg2
# call sum function
Print ("the added value is:", sum (10,20))
Print ("the added value is:", sum (20,20))
Lambda is an anonymous function defined as follows
Lambda [arg1 [, arg2,.argn]]: expression
Thank you for reading, the above is the content of "how to customize the function in python", after the study of this article, I believe you have a deeper understanding of how to customize the function in python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.