Python Tuples:

A tuple is a collection of objects similar to a list in python for data science. Values stored in tuple are of different types and have to be indexed by using integers. Only thing which separates a tuple from a list, tuples are immutable. Another critical difference is that tuples are hashable. Values in a tuple are separated by commas(,). In tuples, sequence of values is closed in parenthesis. This is not a must, but it is easier to understand that it is a tuple. Elements in a tuple can be accessed by unpacking or indexing.

Creation of a Tuple:

Placing values in sequence separated by commas ‘,’ and with or without parenthesis leads to the creation of tuples. Similar to lists, tuples can have any number of elements and can contain any data type. The creation of tuples from a single element is possible, but it is a bit on the tricky side. Trailing commas are required to form a tuple.

Interesting fact- Tuple without using parenthesis is called Tuple Packing.

#Addition of elements in a Set

#Creating an empty tuple

Tuple1 = ()

print(“Initial empty Tuple: “)

print(Tuple1)

#Creating a Tuple with

#the use of Strings

Tuple1 = (‘PST’, ‘Analytics’)

print(“\nTuple with the use of String: “)

print(Tuple1)

#Creating a Tuple with

#the use of list

list1 = [1, 2, 4, 5, 6]

print(“\nTuple using List: “)

print(tuple(list1))

#Creating a Tuple

#with the use of loop

Tuple1 = (‘PST’)

n = 5

print(“\nTuple with a loop”)

for i in range(int(n)):

Tuple1 = (Tuple1,)

print(Tuple1)

#Creating a Tuple with the

#use of built-in function

Tuple1 = tuple(‘Geeks’)

print(“\nTuple with the use of function: “)

print(Tuple1)

#Creating a Tuple with

#Mixed Datatypes

Tuple1 = (5, ‘PST’, 7, ‘Analytics’)

print(“\nTuple with Mixed Datatypes: “)

print(Tuple1)

#Creating a Tuple

#with nested tuples

Tuple1 = (0, 1, 2, 3)

Tuple2 = (‘python’, ‘PST’)

Tuple3 = (Tuple1, Tuple2)

print(“\nTuple with nested tuples: “)

print(Tuple3)

#Creating a Tuple

#with repetition

Tuple1 = (‘PST’,) * 3

print(“\nTuple with repetition: “)

print(Tuple1)

Output:

Initial empty Tuple:

()

Tuple with the use of String:

(‘PST’, ‘Analytics’)

Tuple using List:

(1, 2, 4, 5, 6)

Tuple with a loop

(‘PST’,)

#((‘PST’,),)

(((‘PST’,),),)

#((((‘PST’,),),),)

(((((‘PST’,),),),),)

Tuple with the use of function:

(‘P’, ‘S’, ‘T’)

Tuple with Mixed Datatypes:

(5, ‘PST’, 7, ‘Analytics’)

Tuple with nested tuples:

((0, 1, 2, 3), (‘python’, ‘PST’))

Tuple with repetition:

(‘PST’, ‘PST’, ‘PST’)

Concatenation of Tuples:

Joining two or more tuples is known as concatenation. The ‘+’ operator is there to concatenate two tuples. Concatenation takes place from the end of the original tuple. Apart from concatenation, we cannot perform other arithmetic operations on tuple.

Point to remember- Concatenation can be there only in few data types. An error is shown when we try to concatenate a list and a tuple. These things must be kept in time while learing python for data science.

#Concatenaton of tuples

Tuple1 = (0, 1, 2, 3)

Tuple2 = (‘PST’, ‘For’, ‘Analytics’)

Tuple3 = Tuple1 + Tuple2

#Printing first Tuple

print(“Tuple 1: “)

print(Tuple1)

#Printing Second Tuple

print(“\nTuple2: “)

print(Tuple2)

#Printing Final Tuple

print(“\nTuples after Concatenaton: “)

print(Tuple3)

Output:

Tuple 1:

(0, 1, 2, 3)

Tuple2:

(‘PST’, ‘For’, ‘Analytics’)

Tuples after Concatenaton:

(0, 1, 2, 3, ‘PST’, ‘For’, ‘Analytics’)

 

Slicing of Tuple:

To fetch a specific range or a part of sub-elements slicing is performed. Slicing fetches a set of elements, but indexing only fetches a single element.

Point to remember- A tuple can be reversed using a negative increment value.

Understanding Tuples in Python for Data Science - PST Analytics

Deleting a Tuple:

Tuples being immutable do not allow the deletion of a part of it. Although the entire tuple can be deleted using the del() function.

#Deleting a Tuple

Tuple1 = (0, 1, 2, 3, 4)

del Tuple1

print(Tuple1)

Output:

Traceback (most recent call last):

File “/home/efa50fd0709dec08434191f32275928a.py”, line 7, in

print(Tuple1)

NameError: name ‘Tuple1’ is not defined

Built-in methods:

Understanding Tuples in Python for Data Science - PST Analytics

If you want to learn more, you can check this and this.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.