+ 3
what is the difference between print and return both is doing the same job
6 Answers
+ 7
both are not doing the same job... when a return is executed that part of the code will end and next function or statement.. will be executed.. but when a print is used so ethig will appear on the screen and the code will run until the whole process is finished
example:
def func():
if 5<7:
return "ok"
else:
return "no"
return "last step"
now when if statement is run, it will check if 5 is less than 7.. and yes it is... so it will return "ok" and it will not continue to "last step...
but if we replace the return "ok" with print "ok" - in if condition 5<7 ... then it will print "ok" and then "last step"
+ 3
First of all it is not a good practice to give many exit window for a function. The good practice is to use only one return.
Difference between print and return:
print will help the user to debug, it print the given arguments to standard output.
In python if you does not have a return also it will return None.
Lets say if printed something inside your function and not returned, there are many scenarios you may need the output of function to proceed further in the program. So how print helps in those scenarios.
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.
+ 1
thank ks dude
+ 1
print is used only to print whereas return is output of code
0
print just print what you have to return but return gives the result of the function
0
a anb b are numbers - could you explain the output of print(print(a,b), print(a,b)) - why it is three lines and none none at the third and not "ab+ab"?