Python code to count the number of occurrences