I am a front-end nobody, for my own better opportunities, I am trying to learn Python by myself, I hope there are like-minded leaders can communicate

Learning the road, long, step by step step out of their own road

1.1 Definition and invocation of functions

# function, can achieve a specific function, is the integration of multiple lines of code
Use the keyword def
# def function names () : # function names follow the rules for identifiers
# function definition, the code in a function is not executed, only executed when the function is called
# Function benefits: Repetitive code needs to be written multiple times, reducing code redundancy
print('Before function definition')  Before the function is defined

The definition of a function does not execute the code evaluated in the function
def func() :
    print('Good good study, day day up')
    print('good good study, day day up')
    print('Don't let your mind wander -')

print('After the function is defined')
The code in the function is executed when the function is called.
print('Before function call')
func()   The code jumps to where the function is defined
print('After function call')
func()
Copy the code

1.2 Documentation of functions

The documentation of a function is essentially a comment, telling others how the function is used and what it does
# this comment, however, has a specific location requirement, to be written below the function name

def func() :
    """ Prints a Hello World, """
    # aaa
    print('hello world')
func()

To view the documentation of the function, use help.
# help (print)

help(func)
Copy the code

1.3 Write functions with parameters

Define a function that implements the sum of two numbers
def add(a, b) :  # a and b are called formal parameters, or parameters for short
    c = a + b
    print(F prime is the sum of f prime{c}')

If the function is defined as a parameter, then the parameter value must be passed when the function is called
This parameter is called an implementation parameter, or argument for short
When the function is called, the value of the argument is passed to the parameter
add(120.100)   # 220
add(4.6)   # 10
Copy the code

1.4 Local Variables

The scope of a local variable (scope) : the interior of the current function
Local variables are created at the time of a function call and destroyed (deleted) after the function call.
Local variables can only be used inside the current function, not outside it.

A local variable is a variable defined inside a function
Local variables can only be used inside functions, not outside functions and other functions
def func() :
    # define local variables
    num = 100
    print(num)

def func1() :
    num = 200  # num is not related to num in func
    print(num)


# Function call
func()
func1()

Copy the code

1.5 Global Variables

g_num = 100


# 1. Can global variables be accessed inside functions? ====> You can access the value of a global variable directly
def func1() :
    print(g_num)


# 2. Can I change the value of a global variable inside a function? ===> The value of a global variable cannot be modified directly
def func2() :
    # g_num = 200 # we are not modifying the value of the global variable, we are defining a local variable with the same name as the global variable
    To change the value of a global variable inside a function, declare it as a global variable using the global keyword
    global g_num
    g_num = 300


func1()
func2()
func1()
Copy the code

1.6 the return value

Return the value of a local variable defined in a function, or computed from a local variable, that you want to access and use outside the function
# the function wants to return a data value to the place where it is called, using the keyword return
# return return (); The program code terminates execution when it encounters a return
The return keyword must be written in the function
def add(a, d) :
    c = a + d
    If you want to return the summation result c, you will not print the summation result inside the function
    return c
    print(F prime is the sum of f prime{c}') The function ends when it encounters a return, and does not execute the code after the return

result = add(100.200)
print(I get the sum outside of f prime{result}')

print(add(10.30))
Copy the code

Def func(): XXX return # returns None, terminating the function

  1. The function may not write return, but returns None by default

def func(): xxx pass`

1.7 Function nesting

def func1() :
    print('func1 start ... ')
    print('Other code for the function')
    print('func1 end ... ')


def func2() :
    print('func2 start .... ')
    func1()
    print('func2 end ... ')


# call func1 ()
# func1()
# call func2 ()
func2()

Copy the code

1.8 Application of functions

# 1. Define a function that prints a line
def print_line() :
    print(The '-' * 30)


# 2. Define a function that prints a line of any number
def print_lines(n) :
    for i in range(n):
        print_line()


# print_line()
# print_lines(4)
print_lines(5)


Copy the code

Today’s study is to learn here, there are big guy can guide