This guide explains returning a value vs printing a value through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.
What You Will Learn
print() sends a representation to the output stream and returns None. return hands a value back to the caller, where it can be stored, tested or combined with other values.
When and Why to Use It
The following example focuses on a practical use of Returning a Value vs Printing a Value.
Step-by-Step Example
def print_total(first_value, second_value):
print(first_value + second_value)
def get_total(first_value, second_value):
return first_value + second_value
printed_result = print_total(2, 3)
returned_result = get_total(2, 3)
print(printed_result, returned_result)
Understanding the Example
The first function displays 5 but leaves printed_result as None. The second produces 5 that can be used by later statements.
Expected Output
5
None 5
Important Behavior and Edge Cases
A function that only prints cannot serve as a general calculation helper. Use print at the application boundary, not as a replacement for return.
Try It Yourself
Change a function that prints an average so it returns the average instead.
Common Beginner Mistakes
- A function that only prints cannot serve as a general calculation helper. Use print at the application boundary, not as a replacement for return.
- Confusing a returned value with printed output, or failing to verify a function’s behavior with different input values.
- Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.
Key Takeaways
- print() sends a representation to the output stream and returns None.
- The first function displays 5 but leaves printed_result as None.
- Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.