My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

'print' vs 'return' statements in a Python function

Deactivated User's photo
Deactivated User
·May 19, 2020

Hii everyone, Omarrr here. I want to share my joy with you all, the joy of finally understanding the difference between the 'print' and 'return' statements in Python. (tears!!!)

  • Python programming language is famous for it's simple syntax and English like commands.
  • A function is a block of code, enveloped in a unique name which can be called many times in a program to perform its designated task, think of it more like an assistant you regularly call upon to help you keep the oven temperature in check throughout the whole time you spend baking your favorite snacks.
  • 'print' statements, (yeah, you guessed that right), displays the result of your program on the screen while the 'return' statement stores the value of whatever is assigned to it, see it more like your friend who actually keeps your oranges safe for you while 'print' is like the guy who has forgotten where he kept your oranges but keeps showing you a picture of your oranges resting in his fridge (such a scam!).

Let's get our hands dirty, looking at how these guys are different (CODE! CODE!! CODE!!)

blog1.png shows how 'print' behaves in a function, notice that when i call the function 'add_two(num_one, num_two) later in the program, i get NONE. This is because the function's original value is not inherited by 'value''

blog2.png value recognizes the result of my function because i used 'return' which helps store the result of 'answer'

Let's raise the bar.... Say i want to subtract '3' from the result of my function, add_two()

blog3.png 'print' gives me an error because the interpreter reads my program as me trying to subtract a number from a function which has no value mapped to it

blog4.png 'return' to the rescue, trustworthy as ever

Hol' up, there's no need for beef, we all can co-exist peacefully.....

blog5.png now, i can look at the pictures of my oranges in my friend's fridge and be rest assured that they are really safe, phew!

All these talk about oranges is making me hungry...

I hope with these few points of mine, I have been able to show you the use of 'return' and 'print' statements in Python. All codes used here can be found on mygithub

Feel free to have virtual coffee with me via: 'mytwitter /n mylinkedin'