Here is three years old, to nag you custom functions, this one magical thing, take you vernacular play with custom functions
Custom functions, the essence of programming inside!
def
Necessary functions for custom functions: def
Usage:
def function name(parameters1,parameters2,parameters…): function body (math.)(statement block (computing)) return [return value]
caveat
- Naming rules for function names: The basic rules for identifiers are the same, basically consisting of lowercase letters and underscores.
- def is a keyword and cannot be modified (short for define)
- Functions must be followed by parentheses (in English) Whether or not to add parameters inside the parentheses depends on the situation.
- The colon after the parentheses cannot be forgotten
- The function body must be indented (4 spaces recommended)
Case Study
def hello(): # Customize the hello() function. print('hello world !') hello() # Calling functions hello world ! # output result
This defines a function that outputs 'hello world ! function, just call hello() to realize the call to the function body.
Let's try something a little more complicated to get a feel for Mulan.
def add(x, y): # new add() return x+y add(3, 7) # Call add() (of a computer) runing exportsnone
Ah tired! Ah Tired! Ahh Tired!
Did I write a bug or did I write a loneliness?
Ohhhhhhh! I built a house without leaving the door open (tears)
How can there be results without output?
print(add(3, 7)) print(add('hello ', 'world')) #Running results 10 hello world
death-defying challenge
Remember that every call to the function adds () so what's the thing without the parentheses?
Is it the legendary writing about loneliness or something?
Let's die together.
print(hello) # Calling hello print(add) # Call add in the end: <function hello at 0x0000020213737048> <function add at 0x0000020213737678>
Wow! He really could have
After querying he shows the memory address of the function, not the result of the function or anything, the function is not called
- hint
- Always call a function with parentheses after the function name.
function parameter
Arguments to custom functions, which fall into several categories:
- positional passing parameter
- Keyword Passing Parameters
- mandatory parameter
- default parameter
- Variable parameters
- ……
formal participant and real participant (math.)
Vernacular time: (personal understanding of the basis of the expression, if there is a difference, please enlighten the proposed)
Shape Sen:
is the name of a parameter, a variable name that is not assigned a value and does not actually participate in the function's operations, but rather acts as a placeholder.
Real reference:
The values that actually participate in the function operation have been assigned to the corresponding variable names.
ps: the above is based on personal understanding, this is not an accurate expression, in the vernacular expression based on personal understanding
position parameter
A positional parameter is an expression and correspondence according to the position of the parameter
Parameters written in the position determines the value it corresponds to, this writing method if there are more than one parameter must pay attention to, write the wrong location of the entire function may be reported as an error, the entire program may run down!
Keyword parameters
Although the keyword parameter is more troublesome to write, but it is a good solution to multiple parameters because of the location of the wrong function caused by the error and program instability issues
Problems can be solved efficiently by determining the value of the corresponding parameter from the defined parameter name
Example: print() has an end parameter in addition to the frequently used output content.
The end parameter defaults to '\n', which can be changed if redefined via the keyword parameter
for i in range(10): print(i)
Results:
0
1
2
3
4
5
6
7
8
9
for i in range(10): print(i,end= ' ')
Results.
0 1 2 3 4 5 6 7 8 9
default parameter
In the function involved in the process of many parameters are not necessarily everyone must be used, you can set a default value, this way the need for people can DIY, there is no need to adopt the default parameters
For example, end in print() is the default parameter with a default value of '\n'.
def add(x=0, y=0): return x+y print(add()) in the end: 0
mandatory parameter
Mandatory parameters are parameters that must be written, this is the opposite of the default parameters, either the default parameters or mandatory parameters, if the mandatory parameters are not imported will trigger an error, resulting in a system function crash!
def add(x, y): # new add() return x+y print(add()) Traceback (most recent call last): print(add()) TypeError: add() missing 2 required positional arguments: 'x' and 'y'
This one is an exception caused by missing x, y parameters.
All functions and customizations should be used with mandatory parameters in mind
Variable parameters
Variable Parameters has a rather lofty name, but it's actually quite simple
What if you want to write a function that adds multiple numbers, for example?
Write directly?
def add(x, y): # new add() return x+y
So what if it's 3 values?
should report an error for an extra parameter
So change it to three parameters?
def add(x, y, z): # new add() return x+y+z
The problem comes if it's 2 values then it will report an error missing a parameter, if it's 4 or more it will report an error with more parameters
Ahhhhhhhhhhhh!
Just what is it, difficult python's whole life ruined here?
No, no, no!
Something magical has happened.
def function name(parameters1,parameters2,parameters…,*可变parameters): function body (math.)(statement block (computing)) return value
emmm, strange knowledge grows
def add(x, *y): for num in y: x += num return x print(add(2,3,4)) in the end 9
ps: what *y produces is a tuple
We need to iterate through them one by one before we can get the result of each one.
Variable keyword parameters
What if there are multiple keyword parameters or dictionary imports that cannot be satisfied using * parameter names?
Is this how python's reputation was planted?
No, no, no.
♪ If one ♪ ♪ doesn't work, then two ♪
def personinfo(name, age, **message): print(f'name:{name},age:{age},{message}') personinfo('Zhang San', 23, message = {'city':'Beijing', 'heigh': 180}) in the end: name:John Doe,age:23,{'message': {'city': 'Beijing', 'heigh': 180}}
The result you get in there is a dictionary, and looking at the example you can see that you get a tessellated dictionary
Parameter questions answer the writing requirements for the parameters:
① Mandatory parameters are written at the top
② Default parameters are written after mandatory parameters
③ Default parameters followed by keyword parameters
④ keyword followed by * function name (variable parameters)
⑤ followed by variable keyword arguments (**function name)
ps: go in that order, what's not there can be left out
def a(x , y=0, *z, **i): print(f'x:{x};y:{y};z:{z};i')
The above are just examples, the naming convention is not in line with the rules, do not learn from it
All sorts of strange things can happen if you don't do the above
to this article on the application of python custom function def details of the article is introduced to this, more related python custom function def content please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!