Python for Data Science: Chapter 1: Basics of Python

Python: Tuple

Questions: 1. Enlist built in functions in tuple. 2. What is tuple? Give the difference.

Tuple

 

1. Introduction to Tuples

1. Definition Tuple

Tuple is a collection of elements which are enclosed within the parenthesis, and these elements are separated by commas.

How to Create Tuple?

T1 = (10, 20, 30, 40)

T2 = ('a','b','c','d')

T3 = ('A',10, 20)

T4 = ('aaa','bbb',30, 40)

The empty tuple can be created as

T1 = ()

How to write tuple?

Tuple is written within the parenthesis. Even if the tuple contains a single value, the comma is used as a separator. For example ‒

T1 = (10,)

Difference between tuple and list


Tuple

1. Tuple use parenthesis

2. Tuples can not be changed

List

1. List use square brackets

2. Lists can be changed.

2. Accessing Values in Tuple

The element in Tuple can be accessed using the index.

For example ‒

>>> t1=(10,20,'AAA', 'BBB')

>>> print(t1[0])

    10

>>> print(t1[1:3])

     (20, 'AAA')

>>> 

3. Deleting Values in Tuple

• We can delete a tuple using del statement. The code for this is as given below ‒

T1=(10,20,30)

del T1

print(T1)

• As an output of the above program, the error message will be displayed as T1 gets deleted.

4. Updating Tuple

Tuples are immutable. That means ‒ values in the tuple can not be changed. We can only extract the values of one tuple to create another tuple.

For example

T1 = (10,20,30)

T2 = (40,50)

T3 = T1+T2

print(T3)

Output

(10, 20, 30, 40, 50)

 

2. Basic Tuple Operations

1) len: The len function is used to find the total number of elements in the tuple.

>>> t1=(10,20,30,40)

>>> print(len(t1))

4

>>> 

2) Concatenation: The operator + is used to concatenate two tuples. For example

>>> t1=(10,20,30)

>>>t2=(40,50,60)

>>> print(t1+t2)

(10, 20, 30, 40, 50, 60)

>>> 

3) Repetition: For repetition of the elements in the Tuple, we use * operator. For example,

>>> t1=(10,20,30)

>>> print(t1*3)

(10, 20, 30, 10, 20, 30, 10, 20, 30)

>>> 

4) Membership:

 Membership means checking if the element is present in the tuple or not. The membership operation is performed using in operator.

>>> t1=(10,20,30,40,50)

>>> print(3 in t1)

False

>>> print(30 in t1)

True

>>> 

5) Iteration: Iteration means accessing individual element of tuple. With the help of for loop we can access the element of a tuple. Following screenshot illustrates this operation


In [16]:

 t1 = (10,20,30,40,50)

for i in t1:

      print(i)

10

20

30

40

50

 

3. Built in Tuple Function

There are some useful built in Tuple functions. These functions are enlisted in the following table‒

Function                    Purpose

cmp(t1,12) : This function compares tuple t1 with tuple t2.

It will return either 1, 0 or ‒1, depending upon whether the two tuples being compared are similar or not.

Here, cmp() will return negative (‒1) if tl<t2, zero (0) if t1= t2, positive (1) if tl>t2.

len(t1) : This function gives the total length of the tuple.

max(tl) : This function returns maximum value from given tuple t1.

min(tl) : This function returns minimum value from given tuple t1.

tuple(sequence) : This function converts the list into tuple.

The illustration of above functions is as shown below ‒

>>>t1=(10,20)

>>>t2=(10,20,30)

>>>print(cmp(t1, t2)

>>>‒1


In [17]:

t1=(10,20,30)

print(len(t1))

3

In [18]:

print(max(t1))

30

In [19]:

print(min(t1))

10

Tuple function: There is built in function tuple which is used to create the tuple.

For example

>>> t1=tuple()

>>> print(t1)

( )

>>> 

Another example

>>> t1=tuple("hello")

>>> print(t1)

('h', 'e', '1', '1', 'o')

>>> 


Review Questions

1. Enlist built in functions in tuple.

2. What is tuple? Give the difference.


Python for Data Science: Chapter 1: Basics of Python : Tag: Computer Programming, Python, Data Science : - Python: Tuple


Python for Data Science: Chapter 1: Basics of Python



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation