Return vs Print in Python

Return vs Print in Python

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.
  • return is 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,

Output:

Sample Message

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.

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.

Output:

Message
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 the return and print statements.
  • 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.


import_contacts

You may also like:

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Floor division in Python
    10 April

    Floor division in Python

    Table of ContentsWhat is Floor Division in Python?How to implement floor division in Python?Using the // operator to implement floor division in Python.Using the // operator to implement floor division on negative integers in Python.Using the // operator to implement floor division on floating-point numbers in Python.Using the // operator to implement floor division with […]

  • How to unpack dictionary in Python
    22 March

    How to Unpack Dictionary in Python

    Table of ContentsDictionaries in PythonHow to Unpack Dictionary in PythonUsing the Unpack Operator (**) to Unpack Dictionary in PythonUsing the for Loop to Unpack Dictionary in PythonUsing the iteritems() Function to Unpack Dictionary in PythonUsing the items() Function to Unpack Dictionary in PythonUsing the keys and values Functions to Unpack Dictionary in PythonConclusion In this […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.