Python Tuple count() method with Examples

python tuple count

First of all, Let’s have a look at the count method, with which, we get how many times, an element has occurred in the tuple. We need to provide the element to the method, and the method returns the number of times that element has occurred in the tuple. Let’s have a look at a simple example, which demonstrates the tuple count method.

Python Tuple count() method

As you can see in the above program, we have a simple tuple, which contains some numbers. Then, we are trying to count how many times, the element 12 has occurred in the tuple. For this, we are using the count method. The count method returns the number of times the specified element has occurred in the tuple. We can store it somewhere, but since we are more interested in the output, we are directly printing it. If you try to execute the above program and observe the output, it comes out to be 12.

You can try for some other values, and some other tuple, and find out the number of occurrences of some element in the tuple. Note that if you provide some element, which is not at all there in the tuple, then the count method is going to return 0, which simply means that the number of occurrences of that specified element is zero.

So, whenever we want to get the number of occurrences of some particular element in the tuple, we can make use of the count method in our programs.