Tuple Data types in Python

Rina Mondal
4 min readJan 23, 2024

--

In Python, a Tuple is an ordered sequence of elements. It is similar to a list. However, unlike lists, tuples are immutable, meaning their elements cannot be changed or modified after the tuple is created. Here’s a basic example of creating a tuple:

my_tuple = (1, 2, 3, 'hello', 3.14)
print(my_tuple)

Creating a Tuple:

Tuples are defined by enclosing a comma-separated sequence of values within parentheses ( ). When creating a tuple with a single element, you need to include a comma after the element. This is because parentheses alone can be used for grouping expressions, so the comma distinguishes a tuple from just using parentheses. Here’s an example:

#Empty tuple
t1 = ()
print(t1)
# Output: ()



#Creating a single emenent tuple
my_singleton_tuple = (42,) '''(42,) is a tuple with a single element
(the number 42). The comma is important to indicate
that it's a tuple. If you omit the comma, it won't be recognized as a tuple'''



# Without the comma, it's not a tuple
not_a_tuple = (42)
print(type(not_a_tuple))
#O/t- <class 'int'>
#So, always include the comma after a single element when creating a tuple.



#without brackets also, it can be a tuple.
tuple_without_brackets = 42,
print(type(tuple_without_brackets))
#O/t- <class 'tuple'>



# tuple using type conversion
t3 = tuple('hello')
print t3 # Output: ('h', 'e', 'l', 'l', 'o')

Characteristic of a Tuple:

  1. Heterogeneous Elements: Tuples can contain elements of different data types, including numbers, strings, and other tuples.
# Creating a tuple with elements of different data types
mixed_tuple = (10, 'apple', True, 3.14, (2,3))

2. Ordered: Tuples maintain the order of elements. The order in which elements are added to a tuple is preserved when accessing or iterating over the tuple.

my_tuple = (1, 2, 3, 'hello', 3.14)
print(my_tuple[0]) # Output: 1
print(my_tuple[3]) # Output: 'hello'

3. Immutable : Editing, Deleting or Adding is not possible in tuple data types. No function like insert, append, extend can be performed. No Particular item can be deleted. If we wish to delete the whole tuple, that is only possible by del command.

# Creating a tuple
my_tuple = (1, 2, 3)

# Displaying the tuple before deletion
print("Original tuple:", my_tuple)

# Deleting the entire tuple
del my_tuple

# Attempting to access the tuple after deletion will result in an error
# Uncommenting the line below will raise an error
# print("After deletion:", my_tuple)

Accessing item from a Tuple:

  1. Indexing: To access items from a tuple in Python, you can use indexing.
# Creating a tuple
t1 = (10, 'apple', 3.14, True, 'banana')


# Accessing items by index
first_item = t1[0]
second_item = t1[1]
# Displaying the items
print("First item:", first_item)
print("Second item:", second_item)

#Output-
First item: 10
Second item: apple


# Accessing items with negative indexing
last_item = t1[-1]
second_last_item = t1[-2]
print("Last item:", last_item)
print("Second last item:", second_last_item)

#Output
Last item: banana
Second last item: True

2. Slicing: Slicing is a way to extract a portion of a tuple (or any iterable) in Python.

# Creating a tuple
t1 = (10, 'apple', 3.14, True, 'banana', 7, 'cherry')


# Slicing the tuple
print(t1[1:5])
#Output
('apple', 3.14, True, 'banana')


# Slicing with negative
print(t1[-3:-1])
#Output
('banana',7)



# Printing the tuple in reverse order
print(t1[::-1])
#Output
('cherry', 7, 'banana', True, 3.14, 'apple', 10)

Operations on Tuple:

  1. Concatenate Operator: If you have two tuples, t1 and t2, you can use the + operator to concatenate them, creating a new tuple.
# Creating two tuples
t1 = (1, 2, 3)
t2 = ('a', 'b', 'c')


# Concatenating tuples using the + operator
concatenate_tuple = t1 + t2
print(concatenate_tuple)


#Output
(1, 2, 3, 'a', 'b', 'c')

2. * Operator: The * operator can be used to repeat a tuple.

# Creating tuple
t1 = (1, 2, 3)


# Repeating a tuple using the * operator
repeated_tuple = t1 * 3
print(repeated_tuple)


#Output
(1, 2, 3, 1, 2, 3, 1, 2, 3)

3. Membership operator: check whether a particular element is present in a given iterable. For tuples, you can use the in keyword to perform membership testing.

# Creating tuple
t1 = (1, 2, 3)


# Checking membership in tuples
2 in t1
#Output
True

Iterating Over Tuple: Iterating over a tuple in Python can be done using a for loop. Here's an example with a tuple named t1:

# Creating a tuple named t1
t1 = (1, 2, 3, 'apple', True)


# Iterating over the tuple
for item in t1:
print(item)

#Output
1
2
3
apple
True

Functions Performed on Tuple: In the examples given below, different functions are performed.

#len/sum/max/min/sorted/count/index
t1 = (10, 5, 8, 20, 5, 10, 8, 15)


# Length of the tuple
len(t1)
print(len(t1))
#O/t- 8



# Sum of the tuple elements
sum(t1)
print(sum(t1))
#O/t-81


# Maximum and minimum values in the tuple
print(max(t1))
print(min(t1))
#O/t-20 5


# Sorted list from the tuple elements
sorted(t1)
#O/t- [20,15,10,10,8,8,5,5]



#Sorted list from the tuple elements in reverse order
sorted(t1,reverse=True)
#O/t- [5,5,8,8,10,10,15,20]


# Count of occurrences of the value 5
t1.count(5)
#O/t- 2


# Index of the first occurrence of value 8
t1.index(5)
#O/t-10

--

--

Rina Mondal

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.