gogoWebsite

Python func function usage_python functions

Updated to 9 hours ago

Object-oriented programming, everything is an object, mainly class, abstract

In the process, everything is a process, def defines the process

3. Functional programming, encapsulate a certain function, and call the function name directly when using it, def to define the function, also called function/method/process/subprogram

Function definition: A function refers to encapsulating a set of statements through a name (function name). To execute this function, you only need to call its function name --Alex

def hello():'This is a simple function'print("hello world")

hello()

The above defines a function hello() and calls the function. The final effect is to print out a sentence hello world. We can get some basic definitions of functions from the above. The summary is as follows:

The function starts with the def keyword, followed by the function name and parentheses (), and passes parameters in ().

The first line of the function can store strings to illustrate the function's function.

Let's take a look at functions that pass parameters. Everything in Python is an object, so the parameters passing values ​​can be divided into two types. One is an immutable object, such as strings, tuples, numbers, etc.; the other is a mutable object, such as lists, dictionaries, etc.

Passing immutable object types is similar to the value passing of c++. You can only assign the value of the real parameter to the formal parameter, but you cannot transfer the value of the formal parameter back to the real parameter. The changes of the formal parameter will not affect the changes of the real parameter.

def func1(a):

a=3return a

b=2func1(b)

print('b=' +str(b) )

result:

[python@master func]$

b=2

Passing a mutable object, the content of the original object can be modified through functions, similar to the reference passing of C++, and operating the same object.

def func2(a):

([1,2,3])

b=[1,2,3]

func2(b)

print(b)

result:

[python@master func]$

[1, 2, 3, [1, 2, 3]]

Some function parameters have default values, that is, when calling the function, the specified parameters are not passed in, and the default values ​​at the time of definition will be used. In c++, the parameter writing of function with default values ​​follows the order from right to left, so that the value can be passed correctly when the function is called, and this principle is also followed in Python. In addition, you can also use keywords to pass the value of the specified parameter. As shown in the following example:

def func3(name,age=20):

print("name",name,end=";")

print("Age",age)

func3('ysg')

func3(name='zs')

print('''''''''''''''''''''''''''''''''''')

def func3(name,age):

print("name",name,end=";")

print("Age",age)

func3('zhang',20)

func3(age=18,name='wang')

result:

[python@master func]$

Name ysg; age 20 name zs; age 20 name zhang; age 20 name wang; age 18

In addition, if the number of parameters passed in is not clear in advance, you can add * before the parameters to indicate that when passing in parameters, the parameters are separated by commas. As shown in the following example, solve the addition of the input data:

def func4(*number):sum=0

for x innumber:sum=sum+x

print(sum)

func4(1,2,3,4)

result:

[python@master func]$ python34.py10

lambda expression -- create anonymous functions (application scenarios of anonymous functions: applied to one-time scenarios, temporarily used)

The lambda expression is an expression that can complete a specific function. Just like the function of a function, the difference between the two is that the lambda expression code is simple to define and can be defined when used, mainly to complete a specific function once; while the function needs to be defined before use and can be used multiple times. The basic syntax of lambda is as follows:

lambda arg1,arg2,...,argn:expression

The lambda expression parameter can also specify the default value, and also follows the rules when defining the function, and is the same as the function when calling. Here is a simple example:

sum1 = lambda a,b:a+b;

print(sum1(1,2))

sum2= lambda a,b=5:a+b;

print(sum2(1))

result:

[python@master func]$ python35.py3

6

As you can see, the value returned by the lambda expression is the value of the expression. In the function, we can use return to return the calculation result of the function; if return does not take parameters, the function is to exit the program and return None. As shown in the following example:

def func5(a,b):if((type(a)!=int) | (type(b)!=int)):

return;else:

return (a+b);

print(func5('a',2))

print(func5(1,2))

result:

[python@master func]$

None3

How to use built-in functions in combination with anonymous functions

Usage of max, min, sorted, map, reduce, filter

Dictionary operations: minimum value, maximum value, sorting

salaries={'egon':3000,'alex':100000000,'wupeiqi':10000,'yuanhao':2000}

print(max(salaries))

print(max(()))

print(max(salaries,key=lambda name:salaries[name]))

print(min(salaries,key=lambda name:salaries[name]))

print(max(zip((),()))[1])

result:

[python@master func]$

yuanhao100000000alex

yuanhao

alex

map

>>> names=['alex','wupeiqi','yuanhao','yanglei','egon']>>>> res=map(lambda x:x if x == 'egon' else x+'ergouzi',names)>>>> print(res)

>>>print(list(res))

['alex Ergouzi', 'wupeiqi Ergouzi', 'yuanhao Ergouzi', 'yanglei Ergouzi', 'egon']>>>