Python tuple Method - count()

‮gi.www‬iftidea.com

The count() method is a built-in tuple method in Python that returns the number of occurrences of a specified element in a tuple.

The syntax for using count() method is as follows:

tuple.count(element)

Here, tuple is the original tuple, and element is the element whose occurrences need to be counted.

The method returns an integer that represents the number of occurrences of the specified element in the tuple.

Here's an example of using the count() method:

tuple1 = (1, 2, 3, 4, 1, 1, 5, 6, 7, 8, 1)
count = tuple1.count(1)
print(count)

Output:

4

In the example above, the count() method was used to count the number of occurrences of the element 1 in the tuple (1, 2, 3, 4, 1, 1, 5, 6, 7, 8, 1). The resulting count is stored in the variable count. Note that the method only counts the occurrences of the specified element in the tuple and does not modify the tuple.