Tuple Methods
count()
Count the total occurrences of a specific item in a tuple.
tuple.count(value)
tuple.count() scans the tuple from start to finish and returns the number of times value appears.
Because tuples are immutable, count() does not modify the tuple and works efficiently with any hashable or non-hashable item.
Parameters
| Name | Description |
|---|---|
| value | The item to search for and count in the tuple. |
Returns
An integer representing the number of occurrences (0 if not found).
Try it
Run it and change it
grades = ("A", "B", "A", "C", "A", "B")
print("Count of A:", grades.count("A"))
print("Count of D:", grades.count("D"))Worth knowing
- Non-existent items: If the item is not present,
count()returns0without raising an error. - Equality comparison: Uses Python's standard equality (
==), meaning1andTrueor0andFalseare treated as equal.