Difference Between print() and return :-
Python में print() का उपयोग किसी value, message या result को स्क्रीन पर प्रदर्शित (Display) करने के लिए किया जाता है।
Python में return statement का उपयोग किसी function से value वापस (Return) भेजने के लिए किया जाता है। Returned value को variable में store करके आगे भी उपयोग कर सकते हैं।
in English-
In Python, the print() function is used to display a value, message, or result on the screen.
In Python, the return statement is used to send a value back from a function to the place where the function was called. The returned value can be stored in a variable and used later.
Example:
Output:
ऐसा क्यों हुआ?
add_print() केवल Result Display करता है। इसमें return नहीं है, इसलिए Function None Return करता है।
add_return() Result को वापस भेजता है, इसलिए y में 30 Store होता है।
Important Rules of return
return Function से Value वापस भेजता है।
return Execute होने पर वर्तमान Function का Execution समाप्त हो जाता है।
यदि Function में कोई return Statement Execute नहीं होता, तो Python उस Function से None Return करता है।
एक Function एक से अधिक Values को Comma से Separate करके Return कर सकता है। Python उन्हें एक Tuple के रूप में Return करता है।
Example:
Output:
यहाँ तीन Values एक Tuple के रूप में Return होती हैं और फिर तीन Variables में Unpack हो जाती हैं।