White Science Python (17): the basic data type (function) (lower)

Life is short, I chose Python

The foregoing Portal

White learn Python (1): Opening

White Science Python (2): basic data type (on)

White Science Python (3): fundamental data types (lower)

White Science Python (4): Variable Basic Operation

White Science Python (5): base operator (on)

White Science Python (6): base operator (lower)

White Science Python (7): based flow control (on)

White Science Python (8): the basis of flow control (lower)

White Science Python (9): basic data structure (list) (a)

White Science Python (10): basic data structure (list) (lower)

White Science Python (11): basic data structure (tuples)

White Science Python (12): basic data structure (dictionary) (a)

White Science Python (13): basic data structure (dictionary) (lower)

White Science Python (14): basic data structure (set) (a)

White Science Python (15): basic data structure (set) (lower)

White Science Python (16): the basic data type (function) (a)

Variable Scope

It refers to the scope of the variable named in the program range variable thought generated.

I did not understand? It does not matter, we write slowly example product.

a = 0

def print_1():
    a = 1
    print('a =', a)

    def print_2():
        a = 2
        print('a =', a)

print_a()

The results show the following:

a = 1

In the above example, a we conducted three assignments, but note that this is not covered by the assignment, but each time the assignment of different scopes.

A first assignment was out most, this is called a global variable, meaning that its scope is global, we use a globally anywhere, can get this value.

The second assignment, a is in print_1()this function, we call print_1()this function, so the assignment will take effect here.

The third assignment is in print_2()this function, we do not call this function, so a assignment here does not take effect.

For example, so we can print:

a = 0

def print_1():
    a = 1
    print('a =', a)

    def print_2():
        a = 2
        print('a =', a)


print('a =', a)

Print results are as follows:

a = 0

Then we will print out a global variable.

Second and third in a method to become local variables, which are only a two to take effect in the current function, the function will not enter into force.

If we assign a local variable commented out, then the function whether to take the values ​​of global variables?

The answer of course is yes, we try:

a = 0

def print_1():
    # a = 1
    print('a =', a)

    def print_2():
        a = 2
        print('a =', a)

print_a()

Print results are as follows:

a = 0

We can also do a little change in the function print_1()call function print_2(), as follows:

a = 0

def print_1():
    # a = 1
    print('a1 =', a)

    def print_2():
        a = 2
        print('a2 =', a)

    print_2()

print('a3 =', a)
print_1()

Print results are as follows:

a3 = 0
a1 = 0
a2 = 2

This example adds a1, a2, a3 only after printing for easy identification, which is not the three variables.

Note: variable scope only in the inner layer of the inner function function, and does not cover the outer function.

Anonymous function

Yes, you read that right, the function can not be named.

When we need to use an anonymous function, you can use lambdakeyword to declare an anonymous function.

  • Just a lambda expression, function body is much simpler than def.
  • Lambda expression is a body, instead of a code block. We can only package a limited logic into the lambda expression.
  • lambda function has its own namespace, and can not be accessed outside its own parameter list or the global namespace parameters.

grammar:

函数对象名 = lambda 形参:表达式

We write a simple anonymous function that we understand it:

add = lambda x,y: x + y

print(add(1, 2))

Print results are as follows:

3

This is achieved our previous article the most simple addition, if you write a little more complicated, for example, plus logic judgment:

max_num = lambda x,y: x if x >= y else y

print(max_num(5, 9))

Print results are as follows:

9

Recommendation: When the function is more complex when the anonymous function is not recommended because it can only be a line, to write not write at the same time is not a good read, structural obscure, if complex business logic, allows anonymous function written by people suspected of life, read people living death. Love life, please stay away from anonymous function.

recursive function

A function can call another function, if this function is called in their own words, then this is called a recursive function.

Now if we ask n factorial:

Do not tell me you do not know what factorial, you might need to go back and ask your junior high school math teacher.

n! = 1 * 2 * 3 * ... * n

Read the wow, think about how to write code.

I could not think or see it written:

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n - 1)

print('10的阶乘为:', factorial(10))

Print Results:

10的阶乘为: 3628800

I hope that the students have to knock yourself codes.

Sample Code

This series of small series all the code will be placed on code management repository Github and Gitee, to facilitate access.

Sample Code -Github

Sample Code -Gitee

Guess you like

Origin www.cnblogs.com/babycomeon/p/11817982.html