Python Tuple count() Method
Python Tuple count() Method
The tuple.count() method in Python returns the number of times a specified value appears in the tuple.
Syntax
tuple.count(value)
Parameter
value- This parameter represents the element to search in the tuple
Return
This method returns the number of occurrences of a specified element in the tuple.
Example 1
# the Python program expalining
# the tuple.count() method
# initializing the tuple values
tupleVal = (1, 2, 3,4,5, 7, 8, 7, 5, 5, 6, 4, 4)
print("Tuple:",tupleVal)
# counting the number os occureneces of 4 in the given tuple
occureneces = tupleVal.count(4)
# printing the returned value
print("The item '4' occured",occureneces,"times"))
Output
Tuple: (1, 2, 3, 4, 5, 7, 8, 7, 5, 5, 6, 4, 4) The item '4' occured 3 times
Example 2
# Python program explaining
# the tuple.count() method
# initializing a random tuple
Tupleval = ('a', ('b', 'c'), ('a', 'b'), [8, 4],[8,4])
# counting the occurences of item ('b', 'c')
countVal = Tupleval.count(('b', 'c'))
# printing the count value
print("The count of ('b', 'c') is:", countVal)
# count element [3, 4]
countVal = Tupleval.count([8, 4])
# printing the count value
print("The count of [8, 4] is:", countVal)
Output
The count of ('b', 'c') is: 1
The count of [8, 4] is: 2
Example 3
# Python program explaining
# the tuple.count() method
# initializing a random vowels tuple
vowels = ('a', 'i', 'o', 'i', 'e', 'i', 'u')
# paasing an element that is not present in the tuple
count = vowels.count('z')
# will return 0 as z is not present in the tuple
print('The count of z is:', count)
Output
The count of i is: 0
