This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Print vs Return in Python


print vs return.jpg

I contemplated on what my first publish shall be, just like the luck of the genie, somebody requested the under query and I jumped on it.

“Please, what is the distinction in utilizing return or print in a perform for python?”

Let me paint a extremely “lengthy in a brief story” right here:

  1. Return – Come again
  2. Print – Shout or do one thing
  3. Function_name – Your errand boy

So, you despatched your man to purchase one thing for you. Allow us to say, Rooster Republic’s burger (my fav ❤) with the Monster drink and gave him your ATM and PIN (hope you belief this pal although – I will not be held accountable. Lol 😁).
You advised him on attending to the situation, he ought to drop a flash in your cell phone quantity.
When he is performed with the acquisition and paid efficiently, he ought to deliver the objects to you.

Now, when he brings the objects, you have got limitless choices of issues to do with them.
You possibly can both:

  • eat it
  • drink it
  • give to a pal
  • hold it
  • throw it away …et al

Utilizing the above, the print in a perform is just like the dropped-flash that you just requested your pal to present to you when he is on the location. If you name a perform, it’s best to do one thing.
Nonetheless, whenever you return a price, similar to whenever you requested your man to return again with the objects, you’re instructing the perform to come again with one thing.
This one thing can be utilized in any approach that you just like. Retailer in a variable, do nothing, utilized in an expression, et al.

Now, let me add the samples:

def say_name():
    title="alvicci"
    print("My title is {:s}".format(title))
Enter fullscreen mode

Exit fullscreen mode

In case you name the perform, it can print the following line:

say_name()
My title is alvicci
Enter fullscreen mode

Exit fullscreen mode

Nonetheless, allow us to use the identical perform, however this time as a substitute of utilizing print, allow us to use the return key phrase.

def say_name():
    title="alvicci"
    return("My title is {:s}".format(title))
Enter fullscreen mode

Exit fullscreen mode

In case you name the above perform, nothing occurs. You did not inform it to do one thing however to return one thing.
What can we do to issues that we obtain? Lord and grasp, over to you, lordship. 😎
Let me use what was returned to do one thing:

print("What's your title? ")
print(say_name())
Enter fullscreen mode

Exit fullscreen mode

We known as the perform within the print perform. So, it can consider the perform, say_name() first and provides the return worth to the print perform.
Now, you have got given the print perform one thing. And the print perform all the time shows to the console what it was given.
The output will appear like this:

What's your title? 
My title is alvicci
Enter fullscreen mode

Exit fullscreen mode

That is my first try ✍ and I hope you discover this beneficial. Nonetheless, I am nonetheless studying and rising!

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?