A picture is worth a thousand miles

Print is used to print data to the controller, that is, print it on the interface where you can see it

  • The function of print is relatively easy to understand
print (1)
print('asdfghj') Output1
asdfghj
Copy the code

Above is the output data to the control end

One of the functions of return is to return the calculated value

  • No return statement
x = 1
y = 2
def add(x, y) :
    z = x + y
print(add(x,y)) output the resultNone
Copy the code

There is no return statement, so there is no assignment to add(), which prints out as null (None).

  • A return statement
x = 1
y = 2
def add(x, y) :
    z = x + y
    return z
print(add(x,y)) output the result3
Copy the code

Note: The return value will only be displayed if it is printed by print, but it is not required in interactive mode

def func1() :
    for i in range(1.5) :return (i)

print(func1())
print("...") func1() outputs the result1.Copy the code

As above, a direct call to func1() produces no output.

Do a complex print and return combination

x = 1
y = 2
def add(x, y) :
    z = x + y
    print(z)
print(add(x,y)) output the result3
None
Copy the code

When you print add(x,y), add(x,y) executes the print (z) statement to get 3, but add(x,y) returns None, so the print output should be 3,None

Print and return program execution aspects

def func1() :
    for i in range(1.5) :print (i)

def func2() :
    for i in range(1.5) :return (i)

func1()

print("...")
print(func2()) outputs the result1
2
3
4.1
Copy the code

The program reads the return() statement, and the statement after it will not be executed, so it prints func2() and returns the result with **”1″. Unlike the print() statement, the statement after it is still executed, so when func1() is called, the values “1”, “2”, “3”, and “4”** are printed.