Python basis (seven)

1 function module

1.1 Module Functions

Refers to a function module function module, the module has three functions:

  • Built-in modules: also known as the standard library.
  • The third party open source module: the package can be installed by a management tool.
  • Custom modules.

1.2 Import

There are three main import method:

1.2.1 import moudle

import math
math.e

Here Insert Picture Description

1.2.2 import module as alias

alias represents the alias.
Here Insert Picture Description

1.2.3 from module import function

Import function from the module.
Here Insert Picture Description

1.2.4 from module import function as alias

And on a similar, plus an alias.
Here Insert Picture Description

2 custom function

2.1 Defined Functions

Use the keyword def definition:

def func():
    print(1)

2.2 Setting docstring

Note docstring is described functions, a single or three using three double quotes may be) acquired by the function docstring help (.

def func():
    '''
    func docstring
    '''
    print(1)
help(func)

Here Insert Picture Description

2.3 function calls

Function name can be used directly, if required, to add parameters.

func()
func(22)

2.4 Return value

The return value is specified in the return, the return multiple values:

def f():
    return 1,2,3,4

Equivalent returns a tuple. If the function does not return, None is returned.

2.5 parameter

2.5.1 parameter classification

Parameter when the parameter list is defined inside the function parameters can be divided into:

  • Optional parameter: the parameter is given a default value.
  • Required parameter: the default parameter is not given.
def f(x1,x2,x3=3)

Wherein x1, x2 mandatory parameter, x3 optional parameter.

2.5.2 * and **

* The parameter with a parameter that indicates the type of tuples, with two * indicates that the parameter of the parameter type as a dictionary.

2.5.3 named keyword arguments

* Appeared in the parameters.

def f(x1,*x2,x3,x4)

x3 and x4 is named keyword arguments, you must explicitly use the parameter name when calling.

2.6 argument

python argument can be divided into solid position to participate in the keyword arguments.

2.6.1 positional parameters

Location parameter is the name of the parameter is not specified arguments, each argument to a sequence based on the associated parameter.

2.6.2 keyword arguments

Keyword argument that specifies the name of the parameter argument to specify the form of key-value pairs can not be called in sequence.

def f(x1,x2,x3):
    print(x1,x2,x3)
f(1,2,3)
f(x3=999,x1=888,x2=999999)

Here Insert Picture Description

2.7 Variable Visibility

python visibility of three variables: global variables, local variables, the non-local variables.

2.7.1 Local Variables

Function is variable in a local variable, therefore, the output code 9:

x1 = 8
def f():
    x1 = 9
    print(x1)
f()

If the output x1 before it?
Here Insert Picture Description
Direct prompted not defined x1, x1 global variable is not visible in the function, but I did not give up, be sure to run it.
Here Insert Picture Description
It says a local variable x1 has not been assigned, note, is a local variable, therefore, this also proves that global variables are not visible within the function.

2.7.2 Global Variables

To make visible global variables, use the global within the function:

x1 = 8
def f():
    global x1
    print(x1)
    x1 = 9
    print(x1)
f()

Here Insert Picture Description
global x1 x1 represents is a global variable, pay attention to global x1 require a separate line.

2.7.3 Non-local variables

A nested function using nonlocal statement, this variable indicates the inner function is a function of the outer layer.

x1 = 8
def f():
    x1 = 9
    print(x1)
    def f1():
        nonlocal x1
        print(x1)
        x1 = 10
        print(x1)
    return f1
f()
f()()

Here Insert Picture Description
The first and second F 9 are print () () in the output, third 9 is f1 () in the print () output.

2.8 parameter passing rules

When the argument to the parameter, there are two routing rules, one value is passed, but the address transfer.

2.8.1 value is passed

When arguments when immutable object, the value transfer rules, common types include immutable objects int, float, str, bool, tuple.

def f(x):
    x = 1
    print(x)
x = 3
print('-------int------')
f(x)
print(x)
print()

x = 'string'
print('-------str------')
f(x)
print(x)
print()

x = (3,4,5)
print('-------tuple------')
f(x)
print(x)
print()

x = 5.799
print('-------float------')
f(x)
print(x)
print()

Here Insert Picture Description

2.8.2 delivery address

When the variable parameter is the object address using the routing rules, the modified parameter value while changing the value of the argument. Common types include variable object list, set, dict.

def f(x):
    if type(x) == list:
        x = x.reverse()
    elif isinstance(x,set):
        x.add(8)
    else:
        x['0'] = 3
x = [1,2,3]
print('-------list------')
print(x)
f(x)
print(x)
print()

x = {4,5,6}
print('-------set------')
print(x)
f(x)
print(x)
print()

x = {'a':1,'b':2}
print('-------dict------')
print(x)
f(x)
print(x)
print()

Here Insert Picture Description

2.9 lambda function

lambda is a special custom function, using the keyword lambda definition, is an anonymous function, the function body is generally very short.

2.9.1 definitions

Begins with the keyword lambda, followed by a list of parameters, a colon after the end of the parameter list, followed by the return value.

y = lambda x:x*3
z = lambda x,y:x*y

2.9.2 calls

Call through the "function" calls, or use with other functions.
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/Blueeeeeeee/p/12121183.html