loading...

Discussion on: What does the len() function do in Python ?

Collapse
amalshaji profile image
Amal Shaji

len returns the length of an iterator or the __len__ method of a class if defined

>>> a = [1, 2, 3, 4]
>>> len(a)
4
>>> a = "amalshaji"
>>> len(a)
9
>>> a = (1, 2, 3, 4)
>>> len(a)
4
>>> a = {1, 2, 3, 4}
>>> len(a)
4
>>> class Amal:
...     def __init__(self):
...             pass
...     def __len__(self):
...             return 5
...
>>> a = Amal()
>>> len(a)
5
Collapse
ddsry21 profile image
Python Programming By DDSRY Author

Awesome, Thank you for sharing this.