Important Notice:

Difference Between print() and return

Difference Between print() and return

33 views 1 min read

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:

def add_print(a, b):
    print(a + b)

def add_return(a, b):
    return a + b

x = add_print(10, 20)
y = add_return(10, 20)

print("x =", x)
print("y =", y)

Output:

30
30
x = None
y = 30

ऐसा क्यों हुआ?

  • add_print() केवल Result Display करता है। इसमें return नहीं है, इसलिए Function None Return करता है।

  • add_return() Result को वापस भेजता है, इसलिए y में 30 Store होता है।

Important Rules of return

  1. return Function से Value वापस भेजता है।

  2. return Execute होने पर वर्तमान Function का Execution समाप्त हो जाता है।

  3. यदि Function में कोई return Statement Execute नहीं होता, तो Python उस Function से None Return करता है।

  4. एक Function एक से अधिक Values को Comma से Separate करके Return कर सकता है। Python उन्हें एक Tuple के रूप में Return करता है।

Example:

 

def get_marks():
    return 80, 70, 90

hindi, english, computer = get_marks()

print(hindi)
print(english)
print(computer)

 

Output:

80
70
90

 

यहाँ तीन Values एक Tuple के रूप में Return होती हैं और फिर तीन Variables में Unpack हो जाती हैं।

Related Notes