Python Tuple count() method with Examples

In the previous article, we have discussed Python List sort() Method with Examples
Tuples in Python:

Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A tuple, for example, can be used to hold a list of employee names.

Tuple count() method in Python:

The count() function returns the number of occurrences of the particular element in the given tuple.

Syntax:

giventuple.count(gvnelement)

Parameters:

The count() method only accepts one argument.

  • gvnelement: The element whose frequency is to be calculated

Return Value:

The count() method returns the number of occurrences of the given element in the given tuple.

Examples:

Example1:

Input:

Given Tuple = ('hello', 'this', 'is', 'python', 'is', 'programs')
Given Element = "is"

Output:

The count of the given element { is } in the given tuple ('hello', 'this', 'is', 'python', 'is', 'programs') is :
2

Example2:

Input:

Given tuple = (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28)
Given Element = 28

Output:

The count of the given element { 28 } in the given tuple (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28) is :
4

Tuple count() method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

Example-1:

String Tuple:

  • Give the string tuple as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given tuple and store the result in a variable say resltcnt(It has the count of the given element in the given tuple).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the string tuple as static input and store it in a variable.
gvntupl = ('hello', 'this', 'is', 'python', 'is', 'programs')
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = "is"
# Pass the given element above as the argument to the count function
# for the given tuple and store the result in a variable say resltcnt
# (It has the count of the given element in the given tuple).
resltcnt = gvntupl.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given tuple', gvntupl, 'is :')
print(resltcnt)

Output:

The count of the given element { is } in the given tuple ('hello', 'this', 'is', 'python', 'is', 'programs') is :
2

Example-2:

Integer Tuple:

  • Give the Integer tuple as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given tuple and store the result in a variable say resltcnt(It has the count of the given element in the given tuple).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the Integer tuple as static input and store it in a variable.
gvntupl = (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28)
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = 28
# Pass the given element above as the argument to the count function
# for the given tuple and store the result in a variable say resltcnt
# (It has the count of the given element in the given tuple).
resltcnt = gvntupl.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given tuple', gvntupl, 'is :')
print(resltcnt)

Output:

The count of the given element { 28 } in the given tuple (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28) is :
4

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the Integer tuple as user input using tuple (),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the element as user input using the int(), input() functions, and store it in another variable.
  • Pass the given element above as the argument to the count function for the given tuple and store the result in a variable say resltcnt(It has the count of the given element in the given tuple).
  • Print the value of resltcnt.
  • The Exit of Program.

Below is the implementation:

# Give the Integer tuple as user input using tuple (),map(),input(),and split() functions.
# Store it in a variable.
gvntupl = tuple(map(int, input('Enter some random tuple elements = ').split()))
# Give the element as user input using the int(), input() functions,
# and store it in another variable.
gvnelemeent = int(input('Enter some random element = '))
# Pass the given element above as the argument to the count function
# for the given tuple and store the result in a variable say resltcnt
# (It has the count of the given element in the given tuple).
resltcnt = gvntupl.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given tuple', gvntupl, 'is :')
print(resltcnt)

Output:

Enter some random tuple elements = 12 7 1 8 3 2 1 7 1 1
Enter some random element = 1
The count of the given element { 1 } in the given tuple (12, 7, 1, 8, 3, 2, 1, 7, 1, 1) is :
4

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.