You are on page 1of 20

Tuples

V Balasubramanian

Oct 31, 2023

V Balasubramanian Tuples Oct 31, 2023 1 / 20


Table of Contents

1 Introduction

2 Tuples

3 Immutability

4 Tuple Assignment

5 Tuples as return Values

6 Built-in Functions with Tuples

7 Test Your Knowledge

8 Summary

9 Reference

V Balasubramanian Tuples Oct 31, 2023 2 / 20


Introduction

Strings, are made up of characters.


Lists, are made up of elements of any type.
Strings are immutable.
Lists are mutable.

V Balasubramanian Tuples Oct 31, 2023 3 / 20


Tuples
A tuple that is similar to a list except that it is immutable.
A tuple is a comma-separated list of values:
>>> Tuple = ’ a ’ , ’ b ’ , ’ c ’ , ’ d ’ , ’ e ’
>>> Tuple
( ’a ’ , ’b ’ , ’ c ’ , ’d ’ , ’ e ’ )
It is not necessary, it is conventional to enclose tuples in parentheses:
>>> Tuple = ( ’ a ’ , ’ b ’ , ’ c ’ , ’ d ’ , ’ e ’ )
>>> Tuple
( ’a ’ , ’b ’ , ’ c ’ , ’d ’ , ’ e ’ )
To create a tuple with a single element, we have to include the final
comma:
>>> T=() # empty t u p l e
>>> type (T)
<c l a s s ’ t u p l e ’>
V Balasubramanian Tuples Oct 31, 2023 4 / 20
Tuples

Without the comma, Python treats (’a’) as a string in parentheses:


>>> T=( ’ a ’ )
>>> type (T) −−− <c l a s s ’ s t r ’>
>>> T=( ’ a ’ , )
>>> type (T) −−− <c l a s s ’ t u p l e ’>
The operations on tuples are the same as the operations on lists. The
index operator selects an element from a tuple.
>>> T=( ’ a ’ , ’ b ’ , ’ c ’ , ’ d ’ , ’ e ’ )
>>> T [ 2 ]
’c ’
The slice operator selects a range of elements.
>>> T [ 1 : 4 ]
( ’b ’ , ’ c ’ , ’d ’ )

V Balasubramanian Tuples Oct 31, 2023 5 / 20


Tuples
# t u p l e w i t h mixed d a t a t y p e s
my tuple = ( 1 , ” H e l l o ” , 3 . 4 )
print ( my tuple ) # Output : ( 1 , ” H e l l o ” , 3 . 4 )
# nested tuple
my tuple = ( ”mouse” , [ 8 , 4 , 6 ] , ( 1 , 2 , 3 ) )
print ( my tuple , my tuple [ 0 ] [ 3 ] )
# Output : (” mouse ” , [ 8 , 4 , 6 ] , ( 1 , 2 , 3 ) ) ’s ’
# t u p l e can be c r e a t e d w i t h o u t p a r e n t h e s e s
# also c a l l e d t up l e packing
my tuple = 3 , 4 . 6 , ” dog ”
print ( my tuple ) # Output : ( 3 , 4 . 6 , ” dog ”)
# t u p l e unpacking i s a l s o p o s s i b l e
a , b , c = my tuple
print ( a ) # Output : 3
print ( b ) # 4 . 6
print ( c ) # dog
V Balasubramanian Tuples Oct 31, 2023 6 / 20
Immutability

Modify one of the elements of the tuple:


>>> T=( ’ a ’ , ’ b ’ , ’ c ’ , ’ d ’ , ’ e ’ )
>>> T[ 0 ] = ’A ’
TypeError : ’ t u p l e ’ object d o e s not
s u p p o r t item a s s i g n m e n t
To modify the elements of a tuple, replace it with a different tuple
name:
>>> T=( ’A ’ ,)+T [ 1 : ]
>>> T
( ’A ’ , ’ b ’ , ’ c ’ , ’ d ’ , ’ e ’ )

V Balasubramanian Tuples Oct 31, 2023 7 / 20


Tuple Assignment

Swapping two variables a and b:


temp = a
a = b
b = temp

Python provides a form of assignment called as tuple assignment


that solves swap neatly:
a, b = b, a

The left side is a tuple of variables.


The right side is a tuple of values.
Each value is assigned to its respective variable

V Balasubramanian Tuples Oct 31, 2023 8 / 20


Tuple Assignment

All the expressions on the right side are evaluated before any of
the assignments.
This feature makes tuple assignment quite versatile.
>>> x=15
>>> x , y=5, x+2
>>> print ( x , y )
5 17

The number of variables on the left and the number of values on


the right have to be the same:
>>> a , b , c , d = 1 , 2 , 3
V a l u e E r r o r : not enough v a l u e s t o
unpack ( e x p e c t e d 4 , g o t 3 )

V Balasubramanian Tuples Oct 31, 2023 9 / 20


Tuples as return Values

Functions can return tuples as return values.


For example,function that swaps two parameters:
def swap ( x , y ) :
return y , x
Assign the return values to a tuple with two variables:
a , b=5 ,4
a , b=swap ( a , b )
print ( a , b )

V Balasubramanian Tuples Oct 31, 2023 10 / 20


Tuples as return Values

There is a danger in trying to encapsulate swap, which is the following


tempting mistake:
def swap1 ( x , y ) : # i n c o r r e c t v e r s i o n
x, y = y, x
Calling this function like this:
a , b=15 ,14
swap1 ( a , b )
print ( a , b )
a and x are not aliases for the same value.
Changing x inside swap1 makes x refer to a different value, but it has
no effect on a in main.
Similarly, changing y has no effect on b.

V Balasubramanian Tuples Oct 31, 2023 11 / 20


Tuples Operations

V Balasubramanian Tuples Oct 31, 2023 12 / 20


Built-in Functions with Tuples

Built-in functions are commonly used with tuples to perform different


tasks.
len(tuple) Gives the total length of the tuple.
max(tuple) Returns item from the tuple with max value.
min(tuple) Returns item from the tuple with min value.
tuple(seq) Converts a list into tuple.

V Balasubramanian Tuples Oct 31, 2023 13 / 20


Built-in Functions with Tuples

t u p l e 1 = ( ’ 123 ’ , ’ xyz ’ , ’ zara ’ )


print ( len ( t u p l e 1 ) ) −−− 3
print (max( t u p l e 1 ) ) −−− zara
print (min( t u p l e 1 ) ) −−− 123

a L i s t = [ 1 2 3 , ’ xyz ’ , ’ z a r a ’ , ’ abc ’ ]
print ( a L i s t ) −−− [ 1 2 3 , ’ xyz ’ , ’ z a r a ’ , ’ abc ’ ]
aTuple = tuple ( a L i s t )
print ( aTuple ) −−− ( 1 2 3 , ’ xyz ’ , ’ z a r a ’ , ’ abc ’ )

V Balasubramanian Tuples Oct 31, 2023 14 / 20


Built-in Functions with Tuples
t =(1 ,2 ,3 ,4 ,5 ,4 ,6 ,3 ,4)
print ( t [ 0 ] ) −−− 1
print ( t [ 1 : 3 ] ) −−−(2,3)
print ( t [ : : − 1 ] ) − − ( 4 , 3 , 6 , 4 , 5 , 4 , 3 , 2 , 1 )
print ( t . i n d e x (3))−−−2
print ( t . count (4))−−−3
print ( 3 in t)−−−True
print (sum( t ))−−−32
t =( ’ q ’ , ’ u ’ , ’ e ’ )
print ( tuple ( enumerate ( t ) ) ) − −((0, ’ q ’ ) , ( 1 , ’ u ’ ) , ( 2 , ’ e ’ ) )
print ( tuple ( enumerate ( t , 1 0 0 ) ) ) − − ( ( 1 0 0 , ’ q ’ ) ,
(101 , ’u ’ ) ,(102 , ’ e ’ ))
print (max( t ) ) −−u
print (min( t ))−−e
print ( tuple ( sorted ( t ))) − −( ’ e ’ , ’ q ’ , ’ u ’ )
print ( tuple ( reversed ( t ))) − −( ’ e ’ , ’ u ’ , ’ q ’ )
V Balasubramanian Tuples Oct 31, 2023 15 / 20
Linear Search
n=int ( input ( ” Enter t h e number o f e l e m e n t s i n l i s t ” ) )
e l e =()
f o r i in range ( n ) :
i p=int ( input ( ” e n t e r t h e e l e m e n t a t %d”%( i ) ) )
e l e=e l e +( ip , )
print ( e l e )

s e a r c h=int ( input ( ” Enter t h e e l e m e n t t o be s e a r c h e d ” ) )


found=F a l s e
fo r i in range ( len ( e l e ) ) :
i f s e a r c h==e l e [ i ] :
print ( ”Found i n i n d e x ” , i )
found=True
break
i f found==F a l s e :
print ( ” e l e m e n t not found ” , −1)
V Balasubramanian Tuples Oct 31, 2023 16 / 20
Matrix Addition

X= ( ( 1 1 , 2 9 , 3 0 ) , ( 1 4 , 2 5 , 1 6 ) )
Y= ( ( 1 2 , 2 0 , 2 ) , ( 1 , 1 0 , 5 ) )
r e s u l t =()

# i t e r a t e t h r o u g h rows
fo r i in range ( len (X ) ) :
# i t e r a t e t h r o u g h columns
row =()
for j in range ( len (X [ 0 ] ) ) :
row = row+(X[ i ] [ j ] + Y[ i ] [ j ] , )
r e s u l t=r e s u l t +(row , )
print ( r e s u l t )
for r in r e s u l t :
print ( r )

V Balasubramanian Tuples Oct 31, 2023 17 / 20


Test Your Knowledge

1 Write a program that has a list of numbers(+ve, -ve). Make a new


tuple that has only +ve values.
2 Write a program to print maximum and minimum in a tuple.
3 Reverse a tuple using loop.
4 Give the properties of tuples

V Balasubramanian Tuples Oct 31, 2023 18 / 20


Summary

Introduction to Tuple is discussed:


Creating a new tuple.
Accessing Elements in a tuple.
Immutability.
Tuple assignment, Tuple as return value.
Built-in functions.

V Balasubramanian Tuples Oct 31, 2023 19 / 20


Reference

Think Python 2nd Edition by Allen B. Downey.

V Balasubramanian Tuples Oct 31, 2023 20 / 20

You might also like