python / beginner
Snippet
Measuring Container Size
The len() function is a built-in tool that returns the number of items in an object, such as the number of elements in a list or characters in a string.
snippet.py
1
2
3
fruits = ["apple", "banana", "cherry"]count = len(fruits)print(count)
Breakdown
1
fruits = ["apple", "banana", "cherry"]
Creates a list with three string elements.
2
count = len(fruits)
Calls len() to calculate the total number of items in the list.
3
print(count)
Outputs the integer 3.