Home  >  Article  >  Backend Development  >  What is the fundamental difference between 'print' and 'return' in Python?

What is the fundamental difference between 'print' and 'return' in Python?

DDD
DDDOriginal
2024-11-15 00:28:02739browse

What is the fundamental difference between

Formal Distinction between "print" and "return" in Python

In Python, the "print" function displays the specified argument on the console output, while the "return" statement ends the function execution and returns a value to the caller.

To illustrate their difference, consider the example function below:

def print_and_return(param1):
    print(param1)  # Displays param1 on the console
    return param1  # Returns the value of param1

When you call this function, the output on the console will be the value of param1. However, the function also returns that value, allowing you to use it in further code.

Contrast this with the following function:

def print_without_return(param1):
    print(param1)  # Displays param1 on the console

Here, there is no return statement. As a result, the function does not return a value, and any attempt to access the returned value will result in None (the default return value in Python).

To summarize, "print" displays arguments on the console, while "return" ends the function and provides a value that can be used by the caller. Understanding this difference is crucial for controlling function behavior and working effectively with returned values.

The above is the detailed content of What is the fundamental difference between 'print' and 'return' in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn