len() vs range()
len() and range() are both Python Built-in Functions APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. len(): Returns the length (number of items) of an object. Works with sequences, collections, and strings. Its syntax is len(s), and it returns integer length range(): Returns an immutable sequence of numbers. Commonly used in for loops. Its syntax is range(stop) / range(start, stop, step=1), and it returns range object Example of len(): len([1, 2, 3]) # 3
len("hello") # 5. Example of range(): list(range(5)) # [0, 1, 2, 3, 4]
list(range(2, 7, 2)) # [2, 4, 6]. Read the full references for len() and range() to see parameters, edge cases, and example code side by side. Other Built-in Functions APIs worth comparing: print(), type(), int(), float(), str(), bool().