What is the difference between return and print in python?


#1

Sometimes the tutorial teaches to use ‘return’ after a function is created. I am not sure what it does.


#2

I think it’s time someone introduced you to https://www.google.com

:^)

And also, if you think about what functions do, that should let you make a pretty good guess about what return does.


#3
def two_plus_two()
  print(2+2)
This one will print to your screen 4

def two_plus_two()
  2+2
This one does 2+2 and just throws away the 4

def two_plus_two()
  return 2 + 2
This one hands the value 4 back to the caller
I can use that 4 in other parts of my program now

i.e. 4 + two_plus_two() = 8
or if (two_plus_two > 3)

#4

After looking a bit using google: Print vs Return. Hope it helped you.


#5

You don’t always want to print everything on the screen.

def plus_two(integer):
   x = integer + 2
  return x

After you use this function:

plus_two(5)

The value of x will be saved in the memory and you can use it for later operations.