Similar to lists, tuples are an important data type in Python. Tuples follow a specific order; however, unlike lists, their elements cannot be changed. The elements in a tuple are separated by commas and enclosed in parentheses.
# This program is an example of tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
print(testTuple)
Result:
('C', 'C++', 'C#', 'Java', 'Python')
Accessing a Specific Element
To access a specific element, use the index or position of the element, similar to strings or lists.
# This program is an example of tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
print(testTuple[3])
Result:
Java
You can also use negative indexing to access a specific element.
# This program is an example of tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
print(testTuple[-1])
Result:
Python
Slicing a Tuple
To slice a tuple, use the colon operator (:) similar to strings and lists.
# This program is an example of tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
outputTuple=testTuple[1:3] # Slicing from a tuple
print(outputTuple)
Result:
('C++', 'C#')
Adding Two Tuples
You can join two tuples using the plus operator (+).
# This program is an example of joining tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
testTuple2=("Javascript","PHP") # Defining a tuple
outputTuple=testTuple+testTuple2 # joining two tuples
print(outputTuple)
Result:
('C', 'C++', 'C#', 'Java', 'Python', 'Javascript', 'PHP')
Looping Through a Tuple
You can use loops to iterate through each element in a tuple, similar to lists or strings.
# This program is an example of looping a tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
for i in testTuple:
print(i)
Result:
C
C++
C#
Java
Python
Tuple methods
Tuples have some built-in methods that make working with them easier. Below are a few commonly used methods (in alphabetical order).
count()
Counts the number of times a specified element appears in the tuple.
# This program is an example of count() method for a tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
countTuple=testTuple.count("C#") # count() method
print(countTuple)
Result:
1
index()
Finds the index of the first occurrence of a specified element.
# This program is an example of index() method for a tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
indexTuple=testTuple.index("C#") # index() method
print(indexTuple)
Result:
2
len()
Returns the number of elements in the tuple.
# This program is an example of len() method for a tuple in python
testTuple=("C","C++","C#","Java","Python") # Defining a tuple
toupleLength=len(testTuple) # len() method
print(toupleLength)
Result:
5
max()
Finds the highest value element in the tuple.
# This program is an example of max() method for a tuple in python
testTuple=(100,200,50,500,30) # Defining a tuple
toupleMax=max(testTuple) # max() method
print(toupleMax)
Result:
500
min()
Finds the lowest value element in the tuple.
# This program is an example of min() method for a tuple in python
testTuple=(100,200,50,500,30) # Defining a tuple
toupleMin=min(testTuple) # min() method
print(toupleMin)
Result:
30
tuple()
Converts a sequence type (like a list) into a tuple.
# This program is an example of tuple() method for a tuple in python
testList=["C","C++","C#","Java","Python"] # Defining a list
testTuple=tuple(testList) # tuple() method
print(testTuple)
Result:
('C', 'C++', 'C#', 'Java', 'Python')