An array in Python is used to store multiple values of the same data type in a single variable.
The count()
method is used to return the number of occurrences of a value or item in an array.
array.count(x)
x
is the value we want to check for its number of occurrences.
The parameter value of the count()
method is the value or element whose count we want to take in the given array.
The count()
method returns the number of occurrences of a value in an array.
# importing the array moduleimport array as arr# creating an integer data type arrayx = arr.array('i', [1,2,3,4,5,6,1,2,3,2,4,7])# using the count function for the value 2 in the arrayy = x.count(2)# printing the number of countsprint('The number of occurrences of the value 2 in the array is: ', y)
array.array()
function to create an array variable x
.count()
function to take the count or number of occurrences of the number 2
in the array we created.