In Python, tuples are immutable sequences, similar to lists, but their elements cannot be changed after creation. Tuples have a set of built-in methods that allow you to perform various operations on them. Here are some commonly used tuple methods in Python:
tuple.count(element)
: This method returns the number of occurrences of the specified element in the tuple.
my_tuple = (1, 2, 3, 4, 3, 2, 3)
print(my_tuple.count(3)) # Output: 3
tuple.index(element, [start, [end]])
: This method returns the index of the first occurrence of the specified element in the tuple. You can also provide optional start and end parameters to search within a specific range of the tuple.
my_tuple = (10, 20, 30, 40, 50)
print(my_tuple.index(30)) # Output: 2
len(tuple)
: This built-in function returns the number of elements in the tuple.
my_tuple = (1, 2, 3, 4, 5)
print(len(my_tuple)) # Output: 5
tuple + another_tuple
: This is used to concatenate two tuples, creating a new tuple that includes all elements from both tuples.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple) # Output: (1, 2, 3, 4, 5, 6)
tuple * n
: This creates a new tuple by repeating the elements of the original tuple n
times.
my_tuple = (1, 2)
repeated_tuple = my_tuple * 3
print(repeated_tuple) # Output: (1, 2, 1, 2, 1, 2)
element in tuple
: This expression checks if a specific element is present in the tuple and returns a Boolean value (True or False).
my_tuple = (1, 2, 3, 4, 5)
print(3 in my_tuple) # Output: True
print(6 in my_tuple) # Output: False
Note: Tuples are immutable, the above methods do not modify the original tuple but instead return a new tuple or a result based on the original tuple.
Tuple methods in Python example
Here’s an example code that demonstrates the usage of tuple methods in Python:
# Create a tuple
my_tuple = (1, 2, 3, 4, 3, 2, 3)
# count of occurrences of the specified element
count_3 = my_tuple.count(3)
print("Count of 3 in the tuple:", count_3)
# Returns the index of the first occurrence of the specified element
index_of_3 = my_tuple.index(3)
print("Index of 3 in the tuple:", index_of_3)
#Returns the number of elements in the tuple
length_of_tuple = len(my_tuple)
print("Length of the tuple:", length_of_tuple)
# Concatenating tuples using the + operator
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print("Concatenated tuple:", concatenated_tuple)
# Repeating elements in a tuple using the * operator
my_tuple = (1, 2)
repeated_tuple = my_tuple * 3
print("Repeated tuple:", repeated_tuple)
# Checking if an element is present in the tuple using the in keyword
my_tuple = (1, 2, 3, 4, 5)
print("Is 3 present in the tuple?", 3 in my_tuple)
print("Is 6 present in the tuple?", 6 in my_tuple)
Output:
Here’s the tabular format of tuple methods in Python.
Method | Description |
---|---|
tuple.count(element) | Returns the count of occurrences of the element in tuple |
tuple.index(element, [start, [end]]) | Returns the index of the first occurrence of the element in tuple (optional start and end parameters for search range) |
len(tuple) | Returns the number of elements in the tuple |
tuple + another_tuple | Concatenates two tuples, creating a new tuple |
tuple * n | Creates a new tuple by repeating elements n times |
element in tuple | Checks if an element is present in the tuple |
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.