0
What is difference between Print () and return ??
Explain with example if possible.
2 Respostas
+ 1
"print" always output its unamed arguments to the standard output (console/terminal -- unless you explicitly set specific behavior through named specific parameter ^^) and as a function, have None return value (in python prior to 3 "print" was a statement).
"return" is a keyword statement used to return a value from a function:
def spam():
return "egg"
var r = spam()
print(r) # output: egg
What should be confusing to you, is the behavior of the python command line interpreter (when you doesn't run a script file directly, but run python itself, and get the ">>>" prompt for typing python instructions wich will be executed as soon as you validate your entry:
>>> print("egg")
egg
>>> def spam():
. . . return "egg"
. . .
>>> spam()
egg
Why that seems doing the same thing?
Because in these context, an implicit print() is done with the result of the instruction entered:
>>> "e"+"g"+"g"
egg
>>> s = "egg" # no output as an assignation return None (as a function without return statement)
>>> s
egg
0
return is used inside a function
so whenever you call that function it returns the value written with "return" in function.
whereas Print can be used inside or outside a function.(Some languages like python ,Jscript. etc doesn't need a function to run simple codes. But C,C# etc does )