Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Notice Board

Per partecipare al corso di Fondamenti di programmazione 2023-24 loggatevi e attivatelo nella vostra pagina dei corsi preferiti. A quel punto il corso appare nel menù personale cliccando sul proprio avatar. Per i materiali degli anni precedenti seguite lo stesso metodo.

To join the Programming/Lab 2023-24 course, log-on and select it on the my courses page. It will appear on the personal menu of your avatar. For earlier years use the same method.

Understanding the difference between "Print" and "Return" in Python

E
Emre Savluk (560 points)
2 2 7
in News by (560 points)
edited by
Hello, what is the difference between return and print in python?
771 views

18 Answers

W
WatPac2002 (530 points)
1 4 8
by (530 points)
While print is to display the final output like for example a ‘string’, return is how a function gives back a value and all functions return a value. I hope I was clear
aryanahamed (7920 points)
6 12 67
by (7.9k points)

print() is for displaying data. 

return is used in a function to output/produce a value which can be used for other operations later. Think of return like you are passing a value in the background without displaying it. 

n
nerimatt (2320 points)
0 0 20
by (2.3k points)
when you use the print function, you pass a value to it and it simply displays the data on the console.

return on the other hand does not display the data on the console, but instead you use it in functions to pass the data calculated in the function to another component of your program, like variables.

ex:

print "hello" -> displays the word hello, no further calculations can be done with it

function something:

   ...

  return 23

variable var = something() -> 23 is stored in var, it is not displayed on the console
P
PeressiniA (1390 points)
0 0 12
by (1.4k points)
Return can only be used inside a function and gives back the result which can be boolean, float, int or string.

While print can be used anywhere and usually for strings but you can also print the other types as well.
A
Andrea Catino (4720 points)
1 2 13
by (4.7k points)
The return statement is used to exit a function and return a value. The print statement is used to display an output to the console or terminal. Basically print() it's often used to give a feedback to the programmer, so he can make sure that everything is working correctly. The computer can't make use of the printed value. Meanwhile, the value that is returned by a function can then be further used as an argument passed to another function, stored as a variable, or printed.
Nago (1690 points)
7 14 20
by (1.7k points)

Print is a function you use to make your program output a text or value for you to see. You use this when you want to see the value of a variable or text.

Return is a keyword. When a function of a program runs and it reaches the return keyword it stops the execution of the current function that the return is in and it sends the value to where it was called during the operation of the program. In short, the keyword return is usually being used when we want to send a value from point A to point B in our code.

nishantmete (460 points)
0 0 3
by (460 points)

Print is a pre-set function in Python. The thing is, in the early age of computers, there weren't any displays for computers to display the output, so they would print (on paper) the output from the computer. Hence the name print (Prof. Mancini also told this in the class). You would use print function to get any output from the computer. 

Whereas, return is statement/command that you give in a function to return some value (you write the value after the return, multiple values can be separated by a comma) to the function. This way, accessing the function will give you the value/thing you returned. It's like a variable, you assign the function what you return (sort of). After the return statement none of the things you write get executed/processed in that function -- basically indented, by writing return you're in a way telling Python to end this function here, execute all the statements before return and give values after the return to function after executing. 

                                           

p
prasantaks (900 points)
0 0 7
by (900 points)
print() is to show the output value to the screen, on the other hand return is used to send the function's result back to the caller and reuse to other func if it's necessary.

1. def add(x,y):

return x + y

result = add(1 +2)

print(result)

2. def add(x,y):

print( x + y)

add(1 +2)

in this case you can not use the result to any other function. Question to yourself, how you are going to use if you don't save the result some variable to like I did in function 1.
E
Egnald Çela (2320 points)
7 12 29
by (2.3k points)
Hey,

Summarized:

PRINT(): It shows the result, it displays the result on the screen

return: it just makes an operation but does not print it

ANALOGY:

You did your maths homework. You 'returned the value' of the maths homework, so you performed a set of actions.

If someone has to see your homework, you have to show it to them, so you have to print them.

They cannot see your homework, even though you have done it (pretty much what return means) if you do not print it.

I hope that made sense :)
C
Chrinzo (850 points)
4 8 14
by (850 points)
So, in python we can use print() to display something on our console, like numbers, words, any kind of information. For example if you write print("Impero Romano") in your console the words "Impero Romano" will appear.

We use return most of the time in function ( def function() ... return -result-) to specify the result that our function should produce. Example:

def addition(x,y):
    return x+y --> this is the result that we want about our function

hope I've been helpful :)