What about other data types?

The len() function can be used with various data types in Python, and it returns the following:

  1. Strings: Returns the number of characters in the string.

    len("Hello")  # Output: 5
  2. Lists: Returns the number of elements in the list.

    len([1, 2, 3])  # Output: 3
  3. Tuples: Returns the number of elements in the tuple.

    len((1, 2, 3))  # Output: 3
  4. Dictionaries: Returns the number of key-value pairs in the dictionary.

    len({'a': 1, 'b': 2})  # Output: 2
  5. Sets: Returns the number of elements in the set.

    len({1, 2, 3})  # Output: 3
  6. Other collections: Any object that implements the __len__() method can be used with len().

In general, len() provides a way to determine the size or count of elements in various data structures.

0 Comments

no data
Be the first to share your comment!