built-in function
Call functions directly in python, such as:
abs(-100)
data type conversion;
Python's built-in common functions also include data type conversion functions, such as the int() function, which can convert other data types to integers;
int('123') //123
int(12.34) //12
bool('') //False
The function name is actually a reference to a function object. It is completely possible to assign the function name to a variable, which is equivalent to giving the function an alias;
a = abs
a(-1)
define function
In python, the def statement is used to define a function;
def my_abs(x):
if x >= 0:
return x
else:
return -x
Empty function:
If you want to define an empty function that does nothing, you can use the pass statement:
def nop():
pass
Parameters of the function:
Positional parameters:
def power(x,n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
Default parameters:
We can set the default value of the parameter when defining the method. If there is a parameter passed in, use the passed parameter, otherwise use the default value;
def pow(x,n = 2):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
Note: 1: The required parameters are in the front, and the default parameters are in the back, otherwise an error will be reported;
Two: how to set the default parameters;
One thing to keep in mind when defining default parameters: default parameters must point to immutable objects~
Otherwise, the function will always remember this parameter and add one each time~
def add_end(L=[])
L.append('END')
return L
When you call normally, the result seems to be fine;
add_end() //['END']
add_end() //Second call,['END','END']
add_end() //Three calls,['END','END','END']
To modify the above example, we can use the None immutable object to achieve;
def add_end(L = None):
if L is None:
L = []
L.append('END')
return L
Variable parameters: We can define variable parameters so that the number of incoming parameters is variable. We can set up a list or tuple to pass in first, and change the parameters of the list or tuple to achieve variable parameters. The effect; however, we can also define a *num to not limit the parameters you pass in, you can use a for n in num in the function to iterate your data;
def calc(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum
Keyword parameters: The variable parameters we talked about earlier allow you to pass in 0 or more parameters, which are automatically assembled into a tuple when passed in, and keyword parameters allow you to pass in 0 or more key- Parameters of value type, these keyword parameters will be automatically assembled into a dict after being passed in, which is a dictionary data type;
def person(name,age,**kw):
print('name',name,'age',age,kw)
Similar to variable parameters, you can also assemble a dict first, and then convert the dict into keyword parameters and pass them in;
Named keyword arguments: If we want to limit the names of keyword arguments, we can use named keyword arguments, and only accept city and job as keyword arguments;
def person(name,age,*,city,job):
print(name,age,city,job)
In this way, when you pass in the keyword parameters, you must indicate city='Beijing' and job = 'Engineer'; this way the function will accept it, otherwise it will not be accepted;
person ('Jack', 24, city ='Beijing', job ='Engineer') // Correct answer;
person('Jack',24,'Beijing','Engineer') //Misunderstanding;