Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

srikanth soma
srikanth soma
1,572 Points

Return vs Print

I always confuse between print vs return how do I know when will be the right choice to use return and when will be the right choice to use print?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Think of print as a way to display a value to the user or a log file. Use return to pass values to back to the calling code for additional programming use of the value.

srikanth soma
srikanth soma
1,572 Points

In other words, print doesn't store values whereas return stores value?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

All functions and statements return a value that maybe used in other code. A function without an explicit return statement effectively gets a default return None automatically added.

Some statements return a value:

new_list = list(word) # returns a list which is pointed to by new_list

other_list = new_list.sort() # sorting happens "in place" so nothing is returned and other_list is pointed to None

A print statement has a return value of None. So a statement like:

return print("Hello")

Would also return None