Print vs Return in Python

I pondered on what my first post will be, like the luck of the genie, someone asked the below question and I jumped on it.

“Please, what’s the difference in using return or print in a function for python?”

Let me paint a really “long in a short story” here:

  1. Return – Come back
  2. Print – Shout or do something
  3. Function_name – Your errand boy

So, you sent your guy to buy something for you. Let us say, Chicken Republic’s burger (my fav ) with the Monster drink and gave him your ATM card and PIN (hope you trust this friend though – I won’t be held accountable. Lol ).
You told him on getting to the location, he should drop a flash on your mobile phone number.
When he’s done with the purchase and paid successfully, he should bring the items to you.

Now, when he brings the items, you have unlimited options of things to do with them.
You can either:

  • eat it
  • drink it
  • give to a friend
  • keep it
  • throw it away …et al

Using the above, the print in a function is like the dropped-flash that you asked your friend to give to you when he’s at the location. When you call a function, you should do something.
However, when you return a value, just like when you asked your guy to come back with the items, you are instructing the function to come back with something.
This something can be used in any way that you like. Store in a variable, do nothing, used in an expression, et al.

Now, let me add the samples:

def say_name():
name = 'alvicci'
print("My name is {:s}".format(name))
def say_name():
    name = 'alvicci'
    print("My name is {:s}".format(name))
def say_name(): name = 'alvicci' print("My name is {:s}".format(name))

Enter fullscreen mode Exit fullscreen mode

If you call the function, it will print the next line:

say_name()
My name is alvicci
say_name()
My name is alvicci
say_name() My name is alvicci

Enter fullscreen mode Exit fullscreen mode

However, let us use the same function, but this time instead of using print, let us use the return keyword.

def say_name():
name = 'alvicci'
return("My name is {:s}".format(name))
def say_name():
    name = 'alvicci'
    return("My name is {:s}".format(name))
def say_name(): name = 'alvicci' return("My name is {:s}".format(name))

Enter fullscreen mode Exit fullscreen mode

If you call the above function, nothing happens. You didn’t tell it to do something but to return something.
What do we do to things that we receive? Lord and master, over to you, lordship.
Let me use what was returned to do something:

print("What is your name? ")
print(say_name())
print("What is your name? ")
print(say_name())
print("What is your name? ") print(say_name())

Enter fullscreen mode Exit fullscreen mode

We called the function in the print function. So, it will evaluate the function, say_name() first and give the return value to the print function.
Now, you have given the print function something. And the print function always displays to the console what it was given.
The output will look like this:

What is your name?
My name is alvicci
What is your name? 
My name is alvicci
What is your name? My name is alvicci

Enter fullscreen mode Exit fullscreen mode

This is my first attempt and I hope you find this helpful. However, I’m still learning and growing!

原文链接:Print vs Return in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
I only wish to face the sea, with spring flowers blossoming.
我只愿面朝大海,春暖花开
评论 抢沙发

请登录后发表评论

    暂无评论内容