English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The count() method returns the number of times an element appears in the tuple.
In short, the count() method inIn the tupleSearch for the given element and return how many times it appears.
The syntax of count() method is:
tuple.count(element)
The count() method takes one parameter:
element -The element to find the count of
The count() method returns the number of occurrences of the given element in the tuple.
# Vowel tuple vowels = ('a', 'e', 'i', 'o', 'i', 'o', 'e', 'i', 'u') # Count element 'i' count = vowels.count('i') # Output count print('Occurrences:', count) # Count element 'p' count = vowels.count('p') # Output count print('Occurrences:', count)
When you run this program, the output will be:
Occurrences: 3 Occurrences: 0
# Random tuple random = ('a', ('a', 'b'), ('a', 'b'), [3, 4] # Count elements ('a', 'b') count = random.count(('a', 'b')) # Output count print("Statistics ('a', 'b') count: # Count elements [3, 4]) count = random.count([3, 4] # Output count print("Statistics [3, 4]] Count:
When you run this program, the output will be:
Statistics ('a', 'b') count: 2 Statistics [3, 4]] Count: 1