Tuples
Tuples
Indraja Aland
INTRODUCTION
Python tuples are immutable.
Means you cannot change the elements of Tuple..
It is collection of objects.
Objects can be of same data type or different datatypes.
It is enclosed within parenthesis ( )
Tuples can be used as dictionary key as they are immutable.
Eg:
T=(1,2,3,5,7)
A=(1,2.5,5.6,“Mon”)
Fruits=( “mango” , “apple”, “orange”)
TUPLE CHARACTERISTICS:
List are Ordered:
Tuple item are stored in an order and this order cannot be changed.
Immutable:
Tuples are unchangeable or immutable.
Allows Duplicate values:
Tuples can contain duplicate items.
Allows Different Type values:
The tuples can contain items of different data types.
Creating a Tuple:
Tuple are defined in Python by enclosing the data elements inside
parenthesis and separated by comma.
Empty Tuple:
tuple( ) function is used..
A tuple with a single element can be created by putting comma after
element inside parenthesis.
Eg:
t=(18,)
print(t)
OUTPUT:
(18,)
TUPLE OPERATIONS
Operations performed on list data type are :
LIST OPERATIONS
Operations performed on list data type are :
LIST OPERATIONS
Operations performed on list data type are :
ACCESSING ELEMENTS OF A TUPLE
Elements of a tuple are indexed.
Tuple have positive indexing and negative indexing
Eg:
T=(‘Monday’,’Tuesday’,’4.5,6)
Positive Index 0 1 2 3
Negative Index -4 -3 -2 -1
LIST OPERATIONS
SLICING
A slice means ‘ a part of ’ something..
Colon ( : ) is used for slicing.
Eg:
S=("Hi", "Life", "Wonderful“,2,’h’)
0 1 2 3 4
Hi Life Wonderful 2 h
-5 -4 -3 -2 -1
SLICING
i+=1 OUTPUT:
OUTPUT: Apple
Apple orange
orange Apple
Apple
LIST BUILT-IN FUNCTION
Python provides built-in function that can be used with tuple.
Commonly used built-in function are as follows:
len( )
Used to find number of elements in a tuple.
Eg:
L=(1, 2, 3, "hi“)
print(len(L))
OUTPUT:
4
Function Description Examples
tuple ( ) It is also called a constructor. T=tuple( )
It is used to create a tuple with values and also create an print (T)
empty tuple. OUTPUT:
()
count( ) Syntax: tuple.count(value ) T1=("I","Love","Python","Python","is","fu
It returns the total number of times an has appeared in a n","language“)
tuple. a=L1.count("Python")
print(a)
OUTPUT:
2
index( ) Syntax: tuple.index(value) T1=(1,2,3,'apple','orange‘)
It returns the index of the first occurrence of a given i=T1.index('apple')
element in the tuple. print("Index of apple is:",i)
If the element is not present in the tuple, it will give an OUTPUT:
error. Index of apple is: 3
T1=(1,2,3,'apple','orange‘)
i=T1.index('Python')
print("Index of Python is:",i)
OUTPUT:
Value Error
Function Description Examples
ASCENDING:
T1=(7,2,3,9,5)
a=sorted(T1)
print("Sorted in ascending order: ",a)
It sorts the element of OUTPUT:
a given sequence in a Sorted in ascending order: [2, 3, 5, 7, 9]
specific order.
DESCENDING:
In ascending or T1=(7,2,3,9,5)
sorted( ) descending order. a=sorted(T1, reverse=True)
print("Sorted in descending order: ",a)
It returns the sorted OUTPUT:
list and does not Sorted in ascending order: [9, 7, 5, 3, 2]
modify the original
T1=["Soham","Mohan","Ramesh","Durgesh"]
tuple. a=sorted(T1,reverse=True)
print("Sorted in descending order: ",a)
OUTPUT:
Sorted in descending order: ['Soham', 'Ramesh', 'Mohan', 'Durgesh']
Function Description Examples
T1=(645454,64543,6565565)
print("Minimum of all is: ",min(T1))
OUTPUT:
Minimum of all is: 64543
T1=("Rohan","Mohan","Amar“)
It returns the
print("Minimum of all is: ",min(T1))
minimum value OUTPUT:
min( ) present in the Minimum of all is: Amar
tuple.
Syntax: min(tuple) T=(123,'Amit',345,'cat',213)
print("Minimum value in list is: ",min(T))
OUTPUT:
TypeError: '<' not supported between instances of 'str'
and 'int'
Function Description Examples
T=(123,345,213)
print("Maximum value in list is: ",max(T))
OUTPUT:
Maximum value in list is: 345
T=('Amit','Siya',‘Cat‘)
It returns the print("Maximum value in list is: ",max(T))
maximum value OUTPUT:
max() Maximum value in list is: Siya
present in the tuple.
Syntax: max(tuple)
T=(123,'Amit',345,'cat',213)
print("Maximum value in list is: ",max(T))
OUTPUT:
TypeError: '>' not supported between instances of 'str'
and 'int'
Function Description Examples