Python Tuple count() Method

Python count() method counts the occurrence of an element in the tuple. It returns the occurrence of the the element passed during call.

It required a parameter which is to be counted. It returns error if the parameter is missing.

If the item is missing in the tuple, it simply returns 0. Signature and examples of this method are given below.

Signature

Parameters

elem: Element to be counted.

Return

It returns occurrence of the element.

Let?s see some examples of count() method to understand it?s functionality.

Python Tuple count() Method Example 1

Let?s first see an example to count the occurrence of 2 in the tuple.

Output:

(2, 4, 6, 8, 10)
Occurences: 1
(2, 4, 6, 8, 10, 2)
Occurences: 2

Python Tuple count() Method Example 2

If the count element is missing, it simply returns 0 except any errror or exception. See the below. Example.

Output:

(2, 4, 6, 8, 10)
Occurences: 0

Python Tuple count() Method Example 3

Elements can be of any type (special chars, spaces etc) and the count() method works for all. See an example which returns occurrences of an empty string.

Output:

(12, 4, 6, 8, 10, '', '', '', '@', '@', '$', '@')
Occurences: 3
Occurences: 3