Can `print` and `return` be used separately?

0137

Yes, print and return can be used separately in Python. You can use either one without the other, depending on your needs. Here are examples of each case:

Using print Only

You can use print to display information without returning any value from the function:

def greet(name):
    print(f"Hello, {name}!")  # Only prints the message

greet("Alice")  # Output: Hello, Alice!

Using return Only

You can use return to send a value back from the function without printing anything:

def greet(name):
    return f"Hello, {name}!"  # Returns the message

result = greet("Alice")
print(result)  # Output: Hello, Alice!

Summary

  • You can use print to display output without returning a value.
  • You can use return to send a value back to the caller without printing it.

0 Comments

no data
Be the first to share your comment!