What Is the Formal Difference Between "Print" and "Return"

What is the formal difference between print and return?

Dramatically different things. Imagine if I have this python program:

#!/usr/bin/env python

def printAndReturnNothing():
x = "hello"
print(x)

def printAndReturn():
x = "hello"
print(x)
return x

def main():
ret = printAndReturn()
other = printAndReturnNothing()

print("ret is: %s" % ret)
print("other is: %s" % other)

if __name__ == "__main__":
main()

What do you expect to be the output?

hello
hello
ret is : hello
other is: None

Why?

Why? Because print takes its arguments/expressions and dumps them to standard output, so in the functions I made up, print will output the value of x, which is hello.

  • printAndReturn will return x to the caller of the method, so:

    ret = printAndReturn()

ret will have the same value as x, i.e. "hello"

  • printAndReturnNothing doesn't return anything, so:

    other = printAndReturnNothing()

other actually becomes None because that is the default return from a python function. Python functions always return something, but if no return is declared, the function will return None.


Resources

Going through the python tutorial will introduce you to these concepts: http://docs.python.org/tutorial

Here's something about functions form python's tutorial: http://docs.python.org/tutorial/controlflow.html#defining-functions

This example, as usual, demonstrates some new Python features:

The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.

What is the formal difference between print and return?

Dramatically different things. Imagine if I have this python program:

#!/usr/bin/env python

def printAndReturnNothing():
x = "hello"
print(x)

def printAndReturn():
x = "hello"
print(x)
return x

def main():
ret = printAndReturn()
other = printAndReturnNothing()

print("ret is: %s" % ret)
print("other is: %s" % other)

if __name__ == "__main__":
main()

What do you expect to be the output?

hello
hello
ret is : hello
other is: None

Why?

Why? Because print takes its arguments/expressions and dumps them to standard output, so in the functions I made up, print will output the value of x, which is hello.

  • printAndReturn will return x to the caller of the method, so:

    ret = printAndReturn()

ret will have the same value as x, i.e. "hello"

  • printAndReturnNothing doesn't return anything, so:

    other = printAndReturnNothing()

other actually becomes None because that is the default return from a python function. Python functions always return something, but if no return is declared, the function will return None.


Resources

Going through the python tutorial will introduce you to these concepts: http://docs.python.org/tutorial

Here's something about functions form python's tutorial: http://docs.python.org/tutorial/controlflow.html#defining-functions

This example, as usual, demonstrates some new Python features:

The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.

What should I use, print or return in Python?

Print function output the value (whatever it gets either directly or through a function) to the standard output.

In the first screenshot, you are running the function and using print in it, which gives 15 in the output. But as that function isn't returning anything, it's none (or nothing) in the output.

In the second screenshot, function is returning some value which print method gets and output it to the screen.

How is returning the output of a function different from printing it?

print simply prints out the structure to your output device (normally the console). Nothing more. To return it from your function, you would do:

def autoparts():
parts_dict = {}
list_of_parts = open('list_of_parts.txt', 'r')
for line in list_of_parts:
k, v = line.split()
parts_dict[k] = v
return parts_dict

Why return? Well if you don't, that dictionary dies (gets garbage collected) and is no longer accessible as soon as this function call ends. If you return the value, you can do other stuff with it. Such as:

my_auto_parts = autoparts() 
print(my_auto_parts['engine'])

See what happened? autoparts() was called and it returned the parts_dict and we stored it into the my_auto_parts variable. Now we can use this variable to access the dictionary object and it continues to live even though the function call is over. We then printed out the object in the dictionary with the key 'engine'.

For a good tutorial, check out dive into python. It's free and very easy to follow.

Why is this function returning no output and only working with print?

its just RETURNING the value its up to u what u do with the value
and the the brackets r also not required if u r returning the value

def get_formatted_text(first, last):
full_name = f'{first} {last}'.title()
return full_name # only works with print. Why?


x = get_formatted_text('Dick', 'Long')
print(x)

But if u r returning a value u can even manuplitate like

def get_formatted_text(first, last):
full_name = f'{first} {last}'.title()
return full_name # only works with print. Why?


x = get_formatted_text('Dick', 'Long')
print(x.lower())


Related Topics



Leave a reply



Submit