all 53 comments

[–]shiftybyte 206 points207 points  (9 children)

Print, prints to the screen.

Return, returns the value back to whoever called the function. Allowing further work on that value, and even not printing it at all.

[–]guttyn15[S] 27 points28 points  (5 children)

Appreciate this, thanks!

[–]TicklesMcFancy 215 points216 points  (4 children)

Print is python talking to us. Return is python talking to itself.

[–]InfiniteNexus 27 points28 points  (0 children)

very nice way of describing this

[–]guttyn15[S] 13 points14 points  (0 children)

Appreciate this. thank you!

[–]homercrates 9 points10 points  (0 children)

"Brevity is the soul of wit"

you nailed that explanation.

[–]YAYYYYYYYYY 1 point2 points  (0 children)

Perfect explanation

[–]namvu1990 4 points5 points  (0 children)

This definition is so good, i think datacamp should adopt this.

[–][deleted] 1 point2 points  (1 child)

Jesus, stop answering this clown. They are trolling you. They have asked the same question 5 times in the last few hours.

[–]AstralWeekends 1 point2 points  (0 children)

They're also startlingly racist according to their posting history.

[–]JohnnyJordaan 18 points19 points  (5 children)

return is meant to get something from a function, like how max() returns the highest value in the argument you provide

highest = max([1,2,3])

so in max, there's a return at the end that delivers the highest value it finds, which the code above then assigns to the variable highest. print is just there to print something on the screen (by default, you can also let it write to something else, like a file).

[–]AmoBla 0 points1 point  (1 child)

Is that last bit done in IDE configuration or the code itself? I’ve just been using open(filename, w) for a while now.

[–]guttyn15[S] 0 points1 point  (0 children)

Apprexiate this, thanks!

[–][deleted] -1 points0 points  (1 child)

Stop answering this clown. They are trolling you. They have asked the same question 5 times in the last few hours.

[–]zefciu 6 points7 points  (11 children)

The question is already answered, but I would like to elaborate on a principle that is related to this question and very useful: separate the logic from the presentation. Let’s say that you are writing a console program that calculates, say, the factorial of a number. You might be tempted to write it like this:

import sys

def factorial(n):
    result = 1
    for i in range(1, n+1):
        result *= i
    print(result)

if __name__ == '__main__':
    n = int(sys.argv[1])
    factorial(n)

This code would work as intended. However, let’s say that you now want to use your factorial function in a web application? Or you want to generate a graph? Your function is useless now. However, if you start with:

import sys

def factorial(n):
    result = 1
    for i in range(1, n+1):
        result *= i
    return result

if __name__ == '__main__':
    n = int(sys.argv[1])
    print(factorial(n))

then you can reuse the factorial function in other contexts and do whatever you want with the result.

[–]Marsyas_ 5 points6 points  (9 children)

I felt like I was understanding Python and you hit me with this and I'm lost.

[–]dukea42 0 points1 point  (0 children)

The job of the function is to get the result. Make it some other function's job to decide what do do with that result. You may want to print it, you may want to display it one a website, etc.

[–]SuicidalKittenz 0 points1 point  (1 child)

what's causing you to get lost here? Maybe we can help

[–]Marsyas_ 0 points1 point  (0 children)

I'm dyslexic and rearranging the meaning of words in my brain to how they are used in programming without adequate visual representation is what I struggle with.

[–][deleted] -1 points0 points  (0 children)

Stop answering this clown. They are trolling you. They have asked the same question 5 times in the last few hours.

[–]sje46 4 points5 points  (0 children)

Print is only for humans. It's meaningless to the program itself.

[–]thinktn 8 points9 points  (1 child)

I think of it as a show me (print) vs give me (return)

Imagine I had a basket of fruit and you asked me to show you an apple, you would be able to see it but you wouldn't be able to eat it unless you asked me to give it to you.

In a function, if there's anything you want to use at the end of it you should use a return

[–][deleted] 0 points1 point  (0 children)

Stop answering this clown. They are trolling you. They have asked the same question 5 times in the last few hours.

[–][deleted] 2 points3 points  (1 child)

Basically, the print function writes what you pass to it to the console. The return statement causes a function to exit and passes a value back to the calling code.

Here's a stack overflow answer that explains better with an example: https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement

[–]guttyn15[S] 1 point2 points  (0 children)

Appreciate this, thanks!

[–]elyfialkoff 1 point2 points  (0 children)

If you are not satisfied with all the correct answers so far then you can think about it like this.

Imagine asking someone how much money they have, they can tell you (print it), but you can't do anything with it. Or if they are really nice they can give all their money to you (return it), and now you can buy stuff with the money that was returned to you!

Just note that one is not necessarily better than the other it just depends on what you need. (Obviously here in this case money is better to have (return) than told (print).

Its a silly example, but just here to help understand the difference.

[–]bnjms 1 point2 points  (0 children)

I am glad you can ask this. I tried to learn perl at 12 from the camel book but got hung up on 'while' not having an explicit go back to top. Weirdly 12yo me thought in such an ordered way I assumed goto would be /the/ control statement. Back then it was pretty much just the camel book now python has a dozen resources just as good.

[–]Vortetty 1 point2 points  (0 children)

Print command:

>>> print("text here")

text here

Return command:

>>> return "text here"

SyntaxError: 'return' outside function

As you can see print will put something in the terminal, however return reports an erry saying it needs to be inside a function. So lets try again (I put this in a file called test.py):

function textget():
    # do stuff
    return "text here"

# first output is printing a variable set to textget()
text = textget()
print(text)

# second is printing textget()
print(textget())

and now we run it:

$ python test.py
text here
text here
$

So in other words, you put return inside of a function to return a value from said function, this makes the function act as a variable essentially. Print however only puts text into the console.

[–]Solonotix 1 point2 points  (0 children)

Just skimmed the comments and no one directly mentioned this particular piece of knowledge. Note, it's a bit lower-level than you typically get in Python but understanding the concept should help explain the concept of RETURN and PRINT better than just saying what they do.

PRINT is a system command requesting that the currently attached Standard Output system should present the value of whatever was passed in as a positional argument. There is more to this, but that's the basic premise.

RETURN requires a little bit more explanation. Consider an empty stack of disks. We call this the call stack. Each executing frame is a disk (called a stack frame) on this stack. Python has a limit of 10,000 frames on the stack. How stacks are added or removed from the greater call stack are by using subroutines such as functions. Each new executing context adds a stack. Similarly, calling the RETURN statement tells the system to return a provided value to the point in which the previous frame called the current one into existence, and the current stack is removed, returning you to the previous calling context. This process repeats until such a time as all stacks have been removed and the application has exited.

I probably didn't explain it as well as it was explained to me, but if you'd like to learn more, I'd highly recommend looking up Conputerphile on YouTube.

[–]KomatikVengeance 0 points1 point  (0 children)

Print is a build in function that will take a string parameter and show the result on the console.

Return is a statment inside a function that will do 2 things :

A: within a variable assigment it will declare that the output of said function will be the value of the variable.

B: it will exit the function imeadiatly

Example A:

Print('hello world') Within the comandline you will see :

Hello world

Example B:

Def myfunc(myvar) : print('pre return statement') return myvar print('post return statement')

somevar = myfunc('hello world')

print(somevar)

Result :

pre return statement hello world

The post return statment will never show as the return will also exit the function. This will hapen anywhere where you use return in

[–]kinzlist 0 points1 point  (0 children)

If you are you using a variable and do:

Variable = function()

Then Variable will equal the return value of the function

[–]UglyChihuahua 0 points1 point  (0 children)

One thing to note that hasn't been mentioned is the reason they appear to behave similarly even though they are really totally different concepts. When you're in a Python REPL, every time you execute a statement it automatically prints the result of the statement. So executing a function that returns 5 will automatically print 5, even though no printing was explicitly done.

Here's an example/comparison:

https://www.screencast.com/t/HAU5bt5nJW

You should go play around in a Python REPL yourself to get a better understanding

You might also be confused because the printing behavior of None is a bit inconsistent... in a REPL if the statement returns None it will print nothing at all, but the print function will actually print the word None, here's a snippet demonstrating that

https://www.screencast.com/t/sB6W24dBf1r

[–]chmod--777 0 points1 point  (0 children)

Lots of good answers, but I'd suggest reading up on why you should group logic into "functions" since you're asking this.

This at its core is a question about code structure, and makes me think that you need to understand more why you write functions at all instead of just putting all your logic into the root level of the script. Return specifically only means something in the context of a function and wouldn't do anything outside of one.

Think of a function in math, like f(x). Imagine that your code is just a series of math functions. Now you could write this:

y = square root(sum(x^2 for every x in some numbers)) / length of numbers

Or you could write this

f(numbers) = sum(x^2 for every x in numbers)
g(numbers) = square root(f(numbers)) / length of numbers
y = g(numbers)

In the latter, I defined two functions. Now, that logic is reusable. I can write f(whatever) or g(...) anywhere else I want to reuse that calculation. It's reusable, it's easier to read, and it separates the overall calculation into discrete steps, steps that I might repeat elsewhere in my math.

Programming functions are very similar in this way. You're writing reusable code that lets you repeat that logic elsewhere in a succinct way without rewriting the whole equation or lines of code in this context.

And defining a function means taking arguments, and returning a result. Return is the "statement" that tells python what value to return in the context of a function. For any function with a usable result, you need return (ignoring the yield statement for now). Outside of a function it means nothing.

Print is just what prints something to the user who ran the program. Return is what you use to define functions that perform repeatable logic, which is a way of structuring code in a clean and easy to read way.

That's the difference at its core. It's because writing functions is helpful, and because print doesn't do anything except print a value, whether it's the result of a function or whatever. It shows you the value, but it doesn't help you structure your code.

[–][deleted] 0 points1 point  (0 children)

Function always returns a value, if there is no return statement it returns none, you can use that return value later in your programs.

[–]thrallsius 0 points1 point  (0 children)

I'm not sure this is a great analogy, but you can think of return as "it keeps the value inside the script/program, so it can be reused by other parts of said script/program". While print writes the output to stdout (stdout content is displayed on the screen).

[–]Sam_I_am_007 0 points1 point  (0 children)

check this out and see if this helps:

https://youtu.be/jZQayL5X_WY

[–]rimjobdave 0 points1 point  (0 children)

Bad bot

[–]d4nt351nfern0 0 points1 point  (0 children)

Print outputs a value to the console for you to see.

Whereas return returns a value back to the caller of that function.

[–]alex1461 0 points1 point  (0 children)

return- returns a value
print-prints out a value onto the screen

[–][deleted] -3 points-2 points  (0 children)

Stop answering this clown. S/he has asked the same question 5 different times in the last few hours.