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 trialMalachi Gabriel
374 Pointswhat is the difference between the "len" and "return" because isn't they both returning the value in a function?
I would like to know what is the difference between the "len" and "return" because isn't they both returning the value in a function? In the create a funtion video we used "len" to return a value and in "repeating values" video we used the "return" keyword to return a value. When do I use the len and when do I use the return?
3 Answers
Curtis Vanzandt
9,165 Pointslen()
is a method built into Python. It returns the length of an object. For strings, it returns the amount of characters in that string.
return
is a keyword used when writing functions. It's used to return a value, be it a string, integer, boolean value, or whatever.
Yuyang Peng
5,665 Pointslen() contains return, and that is why len() returns a value the function len() would look like //just a basic idea len(parameter): return //the length of parameter Curtis already explained very well, I hope this adds some information to it.
Pablo Franco
3,668 Pointsreturn
is the keyword used by len()
, and all the functions that need to return a value, to actually return a value.
For example:
The len() function uses the return keyword once it has calculated the number of character of the string that was passed in.
def len(string):
# Calculate the length
return result