Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return vs print list

Very new to programming.
Wondering why does this example print all the items in the list, while the second example prints only the first?

def list_function(x):
    for y in x:
        print(y)

n = [4, 5, 7]
list_function(n)

def list_function(x):
    for y in x:
        return y 

n = [4, 5, 7]
print(list_function(n))
like image 544
Dude Avatar asked Aug 31 '15 13:08

Dude


People also ask

What is the difference between returning and printing?

print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.

When should you use return instead of print?

Use print when you want to show a value to a human. return is a keyword. When a return statement is reached, Python will stop the execution of the current function, sending a value out to where the function was called. Use return when you want to send a value from one point in your code to another.

What is the difference between return and print in Python?

The print() function writes, i.e., "prints", a string or a number on the console. The return statement does not print out the value it returns when the function is called. It however causes the function to exit or terminate immediately, even if it is not the last statement of the function.

Is print a return value?

Printing has no effect on the ongoing execution of a program. It doesn't assign a value to a variable. It doesn't return a value from a function call.

What is return list in Python?

By returning the list using return statement. To send the function's result back to the caller, you can use the Python return statement inside of a function or method. The return keyword is followed by an optional return value in a return statement. Any Python object may be the return value of a Python function.


1 Answers

Your first example iterates through each item in x, printing each item to the screen. Your second example begins iterating through each item in x, but then it returns the first one, which ends the execution of the function at that point.

Let's take a closer look at the first example:

def list_function(x):
    for y in x:
        print(y)  # Prints y to the screen, then continues on

n = [4, 5, 7]
list_function(n)

Inside the function, the for loop will begin iterating over x. First y is set to 4, which is printed. Then it's set to 5 and printed, then 7 and printed.

Now take a look at the second example:

def list_function(x):
    for y in x:
        return y  # Returns y, ending the execution of the function

n = [4, 5, 7]
print(list_function(n))

Inside the function, the for loop will begin iterating over x. First y is set to 4, which is then returned. At this point, execution of the function is halted and the value is returned to the caller. y is never set to 5 or 7. The only reason this code still does print something to the screen is because it's called on the line print list_function(n), so the return value will be printed. If you just called it with list_function(n) as in the first example, nothing would be printed to the screen.

like image 160
Cyphase Avatar answered Sep 28 '22 12:09

Cyphase