+ 4
[Print] vs [Return]
Correct me if I’m wrong, but I think I came up with an applicable explication. Of course, the people who have more experience can maybe help me expand my understanding. Since there is a character limit, I am going to post my thoughts as a comment below. Feel free to criticize or point out things I missed or misinterpreted.
13 ответов
+ 12
Eric
print will always show an output of the function.
return allows you to generate a result without showing it. You can also assign that result to a variable and use it later in your code
+ 5
return is just not used to return value, return is also used to stop further execution of function. So return can be used in different ways but print function is just used to print value, it could be returned value or non returned value means directly print inside function. So print is a function and return is a statement. Return cannot be use outside the function but print function can be use. So I don't think both are similar.
+ 4
A͢J
Great example
+ 3
Eric
See this example and tell me how both do the same thing?
def example(arg):
print ('abc')
if arg == 4:
return
print ('xyz')
example(4)
+ 2
[Return]vs[Print]
For anyone that doesnt understand:
* at first glance [return] seems very similar to [print]. They both have an output.
* [print] outputs whatever you put in the arguments and only that.
* it can be part of a code block[nest] or a standalone function outside of the [nest]
* however, [return] must be included inside a [nest]
* the key difference with [return] is that it can have variable output (meaning that with one function, the output can change on repeated uses)
* you can make [return] output one or many values.
* the [return] value can be any python object like: integer, string, floats, list, etc.
* unlike [print], if you leave out the [return] value, it will output “None”
#notes
* so what can i do that’s different?
* if you’re working with an “interactive” session[code], you want to use [return]. e.g., an interactive sign-up process like Github’s
* if you’re working with a script that only outputs a result, you would use [print]. e.g., What’s 2+2?, batch converting jpg to png, etc.
+ 2
Eric
In which lesson you got "return as an alternative print"?
return never print any value, it just return value back to the function. Then after assigning returned value to a variable, you can print it
+ 2
Return and print are completely different things.
It is like comparing cat and dog.
Return is used to return something on function call. For example you have a function which will add two numbers and return sum of those numbers. You can store returned value in variable or send that returned variable to other functions to perform something.
You can also print that returned value.
+ 2
I see it like this:
print is a function and return is a keyword that is used to access and return the value from logic performed in the function
we need the return keyword because there are different scopes in python. Anything inside the local scope (for example, function) will stay in function scope and cannot be accessed unless its value is given to the global scope, we use the return keyword for giving whatever value we want from the function to the program.
print is a function, which is used to see output in the console, it does I think by writing the output in a file name stdout (not sure how that works). I also read in the sololearn lesson that print can be used to write to a file instead of file.write
+ 1
A͢J your explination confuses me a bit. To a beginner learning beginner code, [return] and [print] look like they do almost the same thing. Can you explain further?
+ 1
In Python 3.x:
`print()` is a function which is used to output text on terminal
`return` is a statement that indicates the result a certain function gives back after executed
def sample1(a):
print(a)
def sample2(a):
print(a)
return
def sample3(a):
print(a)
return a
def sample4(a):
return a
# will print, but a = None
a = sample1(2)
# will print, but b = None
b = sample2(2)
# will print and c = 2
c = sample3(2)
# won't print but d = 2
d = sample4(2)
# printing a,b,c,d:
### None None 2 2
print(a,b,c,d)
+ 1
Slick very detailed. I need to read it a bit more but thank you. It gives me a better idea of the differences.
- 1
A͢J Thanks for your reply. My comment as in reference to the content as it is presented in the Python beginners course. The examples provided made it confusing since they used return as an alternative print. Thats why I made this thread so I can learn more about it and possibly see what else it can do.