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
printto display output without returning a value. - You can use
returnto send a value back to the caller without printing it.
