In the last session, we discussed the List operations. Now, we will look at Python's Tuple Operations and examples. The tuples are the most important topic in Python.
Let’s start
Definition of Tuple in Python:
A Tuple is an immutable (unchangeable) data structure.
It is represented using Round brackets
( ).Tuples are used to store multiple values in a single variable.
They can hold different data types such as integers, floats, strings, and Boolean values.
Tuples allow duplicate values.
Commas separate multiple values in a Tuple ( , ) .
Introduction to Tuples
Code
#input
tuple = (1,2,3,4,5)
print(tuple)
#output
(1,2,3,4,5)Tuple Checking Method
Code
#input
empty_tuple=()
print(empty_tuple)
print(type(empty_tuple))
#output
()
<class 'tuple'>List Convert to tuple method
Code
#input
numbers=[1,2,3,4,5,6]
print("Before modify", type(numbers))
numbers=tuple([1,2,3,4,5,6])
print("After Modify", type(numbers))
#output
Before modify <class 'list'>
After Modify <class 'tuple'>Tuple Convert into List method
Code
#input
numbers=(1,2,3,4,5,6)
print("Before modify", type(numbers))
numbers=list([1,2,3,4,5,6])
print("After Modify", type(numbers))
#output
Before modify <class 'tuple'>
After Modify <class 'list'>Mixed Tuple method
Code
#input
mixed_tuple=(1,"Hello World",3.14, True)
print(mixed_tuple)
#output
(1, 'Hello World', 3.14, True)Tuple Concatenation Method
Code
#input
mixed_tuple=(1,"Hello World",3.14, True)
numbers = (1, 2, 3, 4, 5, 6)
#output
(1, 'Hello World', 3.14, True, 1, 2, 3, 4, 5, 6)Mixed Tuple with * Operations
Code
#input
mixed_tuple=(1,"Hello World",3.14, True)
print(mixed_tuple*3)
#output
(1, 'Hello World', 3.14, True, 1, 'Hello World', 3.14, True, 1, 'Hello World', 3.14, True)Numbers with * Operations
Code
#input
numbers = (1, 2, 3, 4, 5, 6)
print(numbers*3)
#output
(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6)Count and Index method in Tuple
Code
#input
numbers = (1, 2, 3, 4, 5, 6,1,1)
print(numbers.count(1))
print(numbers.index(3))
#output
3
2Packing and Unpacking a tuple
Code
#input
##packing a tuple
packed_tuple=1,"Hello",3.14
print(packed_tuple)
#output
(1, 'Hello', 3.14)
##unpacking a tuple
#input
a,b,c=packed_tuple
print(a)
print(b)
print(c)
#output
1
Hello
3.14Unpacking with * Operations
Code
#input
numbers=(1,2,3,4,5,6)
first,*middle,last=numbers
print(first)
print(middle)
print(last)
#output
1
[2, 3, 4, 5]
6Nested List Method
Code
#input
## Nested List
lst=[[1,2,3,4],[6,7,8,9],[1,"Hello",3.14,"c"]]
lst[0][0:3]
#output
[1, 2, 3]Nested Tuple Method
Code
#input
lst=[[1,2,3,4],[6,7,8,9],(1,"Hello",3.14,"c")]
lst[2][0:3]
#output
(1, 'Hello', 3.14)Iterating over Nested Tuples
Code
#input
for sub_tuple in nested_tuple:
for item in sub_tuple:
print(item,end=" ")
print()
#output
1 2 3
a b c
True FalseCreating a tuple with the Range method
Code
#input
tpl = tuple(range(1, 11))
print(tpl)
#output
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)Add the values in a Tuple using the List method
Code
#input
tpl = (1, 2, 3, 4, 5)
lst = list(tpl)
lst.append(6)
tpl = tuple(lst)
print(tpl)
#output
(1, 2, 3, 4, 5, 6)Nested Tuple Iterations
Code
#input
nested_tpl = (
(1, 2, 3),
(4, 5, 6),
(7, 8, 9)
)
for tpl in nested_tpl:
for elem in tpl:
print(elem, end = " ")
print()
#output
1 2 3
4 5 6
7 8 9Remove the Duplicates using the set method
Code
#input
tpl = (1, 2, 2, 3, 4, 4, 4, 5)
unique_set = set(tpl)
print(unique_set)
#output
{1, 2, 3, 4, 5}Now, We have covered Tuple operations with various examples and methods. In the next article, we will dive into set operations in Python.


Join the conversation! Your thoughts help the community grow.