What is the difference between print and return?
print() shows a value on the screen for a person to read, and the rest of your program gets nothing from it. return hands a value back to the code that called the function, so that code can keep it in a variable or do sums with it.
They look the same until you use the answer
If you try a function on its own, both seem to work, because either way you see the answer. The difference shows up when the program tries to do something with it.
def average(marks):
print(sum(marks) / len(marks))
result = average([60, 70, 80])
print(result)
70.0
None
The 70.0 comes from the print inside the function, and it only goes to the screen. The function then reaches its end without a return, and a function that does that hands back None, Python's word for no value at all. So result holds None, and printing it shows the word.
Try to do arithmetic with it and the program stops.
def average(marks):
print(sum(marks) / len(marks))
total = average([60, 70, 80]) + 5
70.0
Traceback (most recent call last):
File "main.py", line 4, in <module>
total = average([60, 70, 80]) + 5
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
NoneType is the type of None. Python is telling you it was asked to add 5 to nothing. If you ever see an unexpected None on screen, or an error that mentions NoneType, the first thing to check is whether a function printed where it should have returned.
Which one to use
A good habit is to have functions that work something out return the result, and leave the printing to the code that calls them. That way the same function can feed a sum or an if test, and you print only at the point where a person needs to see something.
return also ends the function straight away, so any lines after it inside the function don't run. print doesn't do that. It shows its value and the function carries on.
Try it
These two functions work out the same average. One prints it and the other returns it.
Run it
main.pydef average_printed(marks):
print(sum(marks) / len(marks))
def average_returned(marks):
return sum(marks) / len(marks)
a = average_printed([52, 61, 48])
b = average_returned([52, 61, 48])
print("a is", a)
print("b is", b)
print("b rounded is", round(b, 1))Output
Press Run and what your program prints shows up here.
The first line of output, 53.666666666666664, is average_printed showing its answer as it runs. After that, a is None and b is 53.666666666666664, and only b can be rounded to 53.7. Change the last line to round a instead and run it again. You'll get TypeError: type NoneType doesn't define __round__ method, which is Python's way of saying you can't round nothing.
A small exercise
This program should print Your grade is A on one line. Instead it prints A, and then Your grade is None underneath. Change the function so it works.
def grade(average):
if average >= 70:
print("A")
else:
print("B")
print("Your grade is", grade(75))
Show an answer
def grade(average):
if average >= 70:
return "A"
else:
return "B"
print("Your grade is", grade(75))
With return, the call grade(75) stands for the letter "A", so the last line has something to print after Your grade is. Before, the function printed the letter itself and then handed back None.
You'll write small functions that return averages and grades in the Grade Calculator project. The Handbook pages on return values and None go into more detail.
The project that goes with it
Grade Calculator is a free project in Act 1, for Paul. It runs in your browser with nothing to install, and you don't need an account for it.