Tuples

  • Tuples are immutable i.e... We cannot modify its elements. So operations like add append, insert or remove are not possible.

  • Lists are mutable i.e.. We can modify its elements.

Creating Tuple elements

  • Elements are separated by , inside ( ) brackets.

  • Elements can be of same datatype or may have different data types.

  • tupl = () # empty tuple

  • tupl = (1, ) # with one element

  • tupl = ( 1,2, ‘abc’, 2.5 ) # tuple with different datatypes

  • tupl = 1,2,3,4,5 # without brackets are considered as tuple not a list

We can also create a tuple from a list

  • list = [1,2,3 ]

  • tupl = tuple(list)

Create tuple using range

  • tupl = tuple( range ( 1, 9 ) ) # numbers from 1 to 9 are creating tuple from 1 to 8

Accessing Tuple elements tupl=(10,20,30,40,50)

  • tupl [0] represents the first element 10

  • tupl [-1] represents the last element 50

  • tupl [-5] represents 10

  • tupl [:] all elements of tuple

  • tupl [1:4] gives 20,30,40

  • tupl [::2] gives 10,30, 50

  • tupl [::-2] gives 50, 30, 10

  • tupl [-4:-2] gives 20,30

Using For Loop

for i in tupl

print (i)

Basic operations on Tuples

There are five basic operations on tuples

  • Length

  • Concatenation

  • Repetition

  • Membership

  • Iteration

Basic Operations on tuples

  • Length of the tuple

len (tupl_1) returns 5

  • Concat two tuples

tupl_2= ( 60,70,80)

tupl_3 = tupl_1 + tupl_2

print(tupl_3)

  • Repetition

  • tupl_3=tupl_2 * 3

  • print( tupl_3 ) will print ( 60,70,80, 60,70,80 , 60,70,80 )

  • Membership

  • val = 10

  • val in tupl_1 returns true

  • val not in tupl_1 returns false

  • Iteration Operation

for i in tupl_1:

print( i )

Functions to process tuples

Function

Description


len()

Returns the length of the tuple

len ( tupl )

min()

Returns the minimum value in the tuple

min ( tupl )

max ()

Returns the largest value in the tuple

max ( tupl )

count ()

Returns how many times a value is found in the tuple

tupl.count(10)

index()

Returns first occurrence of the value in tuple

tupl.index(30)

sorted()

Sorts the values in ascending order

sorted ( tupl )

sorted( , reverse=True)

Sorts the values in descending order

sorted (tupl, reverse=True)


Example:

t1=(1,2,3,4,5)

print("len function",len(t1))

print("min function",min(t1))

print("max function",max(t1))

print("count function 5",t1.count(5))

print("Index function",t1.index(2))

t3=(9,4,6,2,10)

t2=sorted(t3)

print("sorted tuple ascending order",t2)

print("tuple t3",t3)

t4=sorted(t3,reverse=True)

print("sorted tuple Descending order",t4)


#nested tuple

s1=(2,'def')

s2=(1,'pqr')

student=(s1,s2)

print(student)

print("Length of student",len(student))


print("Sorted student",sorted(student))


print("According name")

print(sorted(student, key=lambda student:student[1]))

Output:


('len function', 5)

('min function', 1)

('max function', 5)

('count function 5', 1)

('Index function', 1)

('sorted tuple ascending order', [2, 4, 6, 9, 10])

('tuple t3', (9, 4, 6, 2, 10))

('sorted tuple Descending order', [10, 9, 6, 4, 2])

((2, 'def'), (1, 'pqr'))

('Length of student', 2)

('Sorted student', [(1, 'pqr'), (2, 'def')])

According name

[(2, 'def'), (1, 'pqr')]

Nested Tuples and its sorting.

  • A tuple inserted into another tuple is called nested tuple

  • tupl = (10, 20, 30 , (100,101) )

  • (100,101) inside tupl will be considered as a single element

  • len ( tupl) will be 4

  • For example

  • student = ( ( 101, ‘abc’), (102, ‘def’) , ( 103, ‘pqr’ ) )

  • len(student)

  • will be 3

  • print ( tupl [ 0 ] ) will return ( 101, ‘abc’ )

Sorting Tuples

  • sorted() function is used to sort the tuples in ascending order

print ( sorted ( student ) will return [ (101, 'abc'), (102, 'pqr'), (103, 'def') ]

It sorts according to rollno 101..102..103..

  • To sort according to name of the student

print ( sorted (student, key=lambda student: student [1] ) )

student[0] specifies the rollno , student [1] specifies the name.