+ 1
Return vs Print
What is the difference between print function and return keyword in Python language? Please explain me it elaborately with example. Please help.
3 Answers
+ 3
in the context of functions.
you either return the value or print it.
ie:
def sum_fun(x, y):
return x + y
if you want to print the result you have to use print outside of the function.
like :
print(sum_fun(4, 6))
#2nd scenario
def sum_fun(x, y):
print( x + y)
now you can call the function without printing because it is included in the function.
like :
sum_fun(4 + 6)
so it depends on how you want to output the result. either using the function itself or by using print .
+ 2
Bahhađ§
Yeah I understand.thank u.
But why need return keyword?
+ 2
because sometimes you just need the result without the need to print it.
not every function has to print its result.