Table of Contents
Return vs Print in Python
In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is where confusion occurs regarding the print and return statement in Python.
Here is the quick description about return and print in Python.
print()is used to print something on console.returnis used to return value such as string, integer from function and exit the function,
We will discuss the return vs print in Python.
Let us begin with print. The print keyword has evolved in Python. Before Python 3, it was a statement. Afterward, it was changed to a function and made more convenient. We use the print() function to display some output in the standard output. The print() function can be used anywhere in the program.
For example,
|
1 2 3 |
print("Sample Message") |
Output:
The above example is a Python 3 code where we print a string. We display this string to the user.
The print() function when encountered within a function does not affect it. We only use it to display some output to the user.
The return statement on the other hand is used to return some value from the given function. This means that the function sends a value back to the caller. This value is specified with the return statement and can be any Python object like a string, number, list, and more.
Every function returns some value. A function that is missing the return statement returns a None value. The return statement affects the function because it specifies the type of value which will be returned by the function. A return statement is only used in a function definition.
Let us see an example of the return statement.
|
1 2 3 4 5 |
def fun(): return 5 x = fun() |
In the above example, the function fun returns a value of 5 which is stored in the variable x.
A function can have both return and print statements. They both do not affect each other in any way. Return vs Print in Python can be understood more clearly by taking an example of both.
See the code below.
|
1 2 3 4 5 6 7 8 |
print("Message") def fun(): print("Message from fun") return 5 x = fun() print(x) |
Output:
Message from fun
5
In the above example,
- We use the
print()function to display a message from outside and inside the function. - In the function
fun(), we have both thereturnandprintstatements. - The function
fun()prints a string and returns a value of 5. - The returned value is stored in a variable which is later displayed using the
print()function.
Conclusion
To conclude, we discussed return vs print in Python in this article. Both have their own use and are often confused when they are utilized in a function. The print() function displays some output. The return statement is used to return some value from a given function. Every function in Python returns some value but does not necessarily print anything. The print() function can be used anywhere in the program whereas the return statement is limited to a function.