English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Python

Contrôle de flux Python

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python tuple count() usage and example

Tuple methods in Python

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)

count() parameter

The count() method takes one parameter:

  • element -The element to find the count of

count() return value

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

Example1: Calculate the occurrence of elements 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

Example2: Calculate the occurrence of tuples and list them in the tuple

# 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

Tuple methods in Python