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
#input
tuple = (1,2,3,4,5)
print(tuple)
#output
(1,2,3,4,5)
Tuple Checking Method
#input
empty_tuple=()
print(empty_tuple)
print(type(empty_tuple))
#output
()
<class 'tuple'>
List Convert to tuple method
#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
#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
#input
mixed_tuple=(1,"Hello World",3.14, True)
print(mixed_tuple)
#output
(1, 'Hello World', 3.14, True)
Tuple Concatenation Method
#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
#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
#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
#input
numbers = (1, 2, 3, 4, 5, 6,1,1)
print(numbers.count(1))
print(numbers.index(3))
#output
3
2
Packing and Unpacking a tuple
#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.14
Unpacking with * Operations
#input
numbers=(1,2,3,4,5,6)
first,*middle,last=numbers
print(first)
print(middle)
print(last)
#output
1
[2, 3, 4, 5]
6
Nested List Method
#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
#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
#input
for sub_tuple in nested_tuple:
for item in sub_tuple:
print(item,end=" ")
print()
#output
1 2 3
a b c
True False
Creating a tuple with the Range method
#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
#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
#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 9
Remove the Duplicates using the set method
#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.