Python tuple count() is a built-in method that helps us calculate the occurrence of one element in the tuple and returns the counted number. It searches the given component of a tuple and returns how often the element has occurred in it.
Syntax
tuple.count(element)
Return Value
Python tuple count() method returns the number of occurrences of one element in the tuple.
Example 1
# Declaring tuple
marks = (75, 64, 91, 67, 75, 64)
# Now we will print the tuple
print("Tuple values are: ", marks)
# We will count the occurence of 75 and 64
print("Frequency of 75 is :", marks.count(75))
print("Frequency of 64 is :", marks.count(64))
# Printing total marks
print("Total marks : ", sum(marks))
Output
Tuple values are: (75, 64, 91, 67, 75, 64)
Frequency of 75 is : 2
Frequency of 64 is : 2
Total marks : 436
In the above program, we have declared a tuple containing the marks of a student, and we have printed that. After that, we checked the frequency of 75, which means in how many subjects the student got 75, the same for 64. Then, at last, we printed the total marks the student got.
Example 2
vowels = ('a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o')
# count element 'a'
count = vowels.count('a')
# print count
print('The count of a is:', count)
# count element 'k'
count = vowels.count('k')
# print count
print('The count of k is:', count)
Output
The count of a is: 2
The count of k is: 0
In the above code, a character appears 2 times, so it returns 2, and k is not in the tuple, so it returns 0.
That’s it.