Print Vs Return in Python: The Ultimate Showdown

Print and return are two keywords that may cause confusion while working in Python. Both are used to provide an output to the code, but their methods of implementation are different. Through this blog, you’ll understand Python print vs return in detail.

print in python

Python’s print statement is used to solve two purposes:

  • To display output on the screen
  • To debug the code by checking a variable’s value

Print’s return type

It has no return type. It directly outputs the code to the screen.

Working

Let’s have a look at the working of print in Python. The given example prints ‘Pythonpool’ with the help of a function, func.

def func():
#prints Pythonpool
  print("Pythonpool")

# Call the function.
func()

The output will be :

Pythonpool

Other uses of print

Print's return type in python
print vs return python

If we want to display many values all at once, we can use print. Also, one can do formatting while using print directly. This saves us time.

print(1, 2, 3, 4, 5)
print("The sum of 1 and 2 is {0}.".format(1 + 2))

Return in python

It returns a specific answer as per the required data type. With the help of return, you can store or pass a variable’s value to another function, too. It structures the code well, i.e., it defines the proper structure of the code.

Working

def add_numbers(a, b):
  """Returns the sum of two numbers."""
  sum = a + b
  return sum

# Call the function and store the result.
result = add_numbers(1, 2)

return for early termination of a function

There might be a few cases wherein the desired problem’s solution is not met. Here, one can use the return keyword to check whether the goal has been accomplished. If not, the return statement will return none. The given example portrays the same. Here, if a number is matched with a value, it is returned.

def index(list1, value):
 #Returns index of the first occurrence of the given value in the list 
  for i in range(len(list1)):
    if list1[i] == value:
      return i
    else:
 # If the value is not found, return None.
      return None

# Function Call
ans = index([1, 2, 3, 4, 5], 3)

# If the index is not None, print it.
if ans is not None:
  print(ans)

Return for multiple values

You can use return for multiple values also. In this case, you can suggest comma-separated variables.

def func(a, b):
  sum = a + b
  mul = a * b
  return sum, mul

#Function calling
s, m = func(1, 2)

# Print the values.
print(s, m)

Difference between print and return

Let’s consider the given example to comprehend the difference between the two keywords.

def add_numbers(a, b):
  """Returns the sum of two numbers."""
  sum = a + b
  print(sum)  # Prints the sum to the console.
  return sum  # Returns the sum to the caller.

# Call the function and store the result.
result = add_numbers(1, 2)

# Print the result.
print(result)

#output will be:
#3
#3

It is suggested to use print to check the final output of a code. You can consider the tabulated differences for a consolidated understanding:

returnprint
It returns a value. It directly prints to the console.
Return value is as per the function. There is no return value.
It is used to pass one function’s value to another function. It is used to debug the code or see the output.
print vs return python

Print and Return in the same function

You can use print and return in the same function depending on the need. However, it is not suggested to use print inside a function unless required. In case you wish to check whether the code is working properly or not, you may use it then.

Nothing to return

There are a few functions which need not return anything. In such cases, we don’t specify anything. Also, it might happen that the code is not fetching something exact. The given example passes a list of numbers as an argument and returns the even numbers.

<pre class="wp-block-syntaxhighlighter-code">def fn(num):
#Returns None if the given <a href="https://www.pythonpool.com/python-check-if-list-is-empty/" target="_blank" rel="noopener">list is empty</a>
  if not num:
    return None
  else:
    return [i for i in num if i % 2 == 0]
even_numbers = fn([1, 8, 6, 4, 5])</pre>

FAQS

Can you use print() or return() together?

Yes, you may use them together.

What will happen if there is no return statement in a function?

In this case, the function will have nothing to return.

Can there be a None return type?

If you haven’t specified a return type, python, by default, returns None, which means there’s no value to return.

Does the print function return anything?

No, it displays the output.

Conclusion

This article covers the difference between print and return. It explains the usage of print and return. It suggests additional uses of print and clearly differentiates use cases of Python print vs return. In a tabulated format, it draws emphasis on the areas where one should prefer print to return and vice versa.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments