Difference between return and print in Python
Last Updated :
05 Jun, 2023
In Python, we may use the print statements to display the final output of a code on the console, whereas the return statement returns a final value of a function execution which may be used further in the code. In this article, we will learn about Python return and print statements.
Return Statement in Python
Python return statement is used to exit the function and return a value. The return keyword is used to specify a value that a function should return when a function is called. It performs some calculations or operations and then returns the value or a set of values to the calling code.
Syntax of Return Statement
def function_name(parameters):
# The function body
return value
Example:
In this example, we can see that when the 'add_number' function is called, it returns the sum of 'a' and 'b', which is stored in the result variable. Then using the print statement, the result is printed on the console.
Python3
def add_numbers(a, b):
return a + b
result = add_numbers(7, 9)
print(result)
Output:
16
Print Statement in Python
The print statement is a built-in function in Python that is used to display output to the console or terminal.
Syntax of Print Statement
print(*objects, sep=' separator', end='\n', file=sys.stdout, flush=False)
Example:
In this example, we can see that the 'greet' function does not return any value but simply prints the value using the print statement when it is called.
Python3
def greet(name):
print("Hello, %s!" % name)
greet("ABC")
Output:
Hello, ABC!
Difference between Return and Print Statement in Python
Let us see the difference between the Python return and Python Print statement.
Return Statement
| Print Statement
|
---|
It is used to exit a function and return a value | It is used to display output to the console |
It returns a value that can be assigned to a variable or used in any expression | It displays output to the console but does not return the value |
It can be used multiple times in a function but only one value can be returned at a time | It can be used multiple times in a function but does not affect the function's return value |
Exiting the function with return ends the function and control returns to calling code | The print does not affect program flow and execution continues normally |
Example: return sum in a function that calculates the sum of two numbers | Example: print("Hello, world!") to display a message on the console |
Similar Reads
Difference between + and , Python Print In this article, we will learn about the difference between + and, in Python print, The print() function in Python is used to print some messages as the output on the screen. We can print a single element or even multiple elements on the screen. Python provides two ways to print multiple elements as
3 min read
Difference between Yield and Return in Python Python Yield It is generally used to convert a regular Python function into a generator. A generator is a special function in Python that returns a generator object to the caller. Since it stores the local variable states, hence overhead of memory allocation is controlled. Example: Python3 1== # Pyt
2 min read
Difference between end and sep in Python In this article we will discuss the difference between The end and sep are two parameters in Python's built-in print() function that will help to control how the output is formatted. end in PythonThe end is a parameter in Python's built-in print() function that controls what character(s) are printed
2 min read
Difference between Logging and Print in Python In Python, print and logging can be used for displaying information, but they serve different purposes. In this article, we will learn what is python logging with some examples and differences between logging and print in Python. Logging in Python Logging in Python is a technique to display useful m
3 min read
Python - Difference between := and == In this article, we will be discussing the major differences between Walrus(:=) and the Comparison operator (==):= in PythonThe := operator in Python, also known as the walrus operator or assignment expression, was introduced in Python 3.8. It enables assignment and evaluation to happen simultaneous
2 min read