0% found this document useful (0 votes)
11 views25 pages

Tuples

The document provides an overview of Python tuples, highlighting their immutable nature, characteristics, and operations such as creation, accessing elements, slicing, and membership. It also covers built-in functions for tuples, methods for modifying tuples indirectly, and explains packing and unpacking of tuples. Additionally, it introduces the concept of nested tuples.

Uploaded by

vedanttawde2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

Tuples

The document provides an overview of Python tuples, highlighting their immutable nature, characteristics, and operations such as creation, accessing elements, slicing, and membership. It also covers built-in functions for tuples, methods for modifying tuples indirectly, and explains packing and unpacking of tuples. Additionally, it introduces the concept of nested tuples.

Uploaded by

vedanttawde2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

TUPLES Grade 11

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)

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

S.No Slicing Output


1 print (S[2]) Wonderful
2 print(S[-2]) 2
3 print(S[1:3]) ('Life', 'Wonderful')
4 print(S[0:2]) ('Hi', 'Life')
5 print(S[-3:-1]) ('Wonderful', 2)
6 print (S[-3::]) ('Wonderful', 2, 'h')
7 a=S[-2:-1]= =S[1:2] False
print(a)
8 a=S[-1:-2:-1]= =S[0:2] False
print(a)
LIST OPERATIONS
CONCATENATION
 To join two values together.
The (+) plus operator is used to perform string concatenation
Concatenation allows you to add duplicate values.
T1=(1,2,3)
T2=("Hello",1,3)
print(T1+T2)
OUTPUT:
(1, 2, 3, 'Hello', 1, 3)
STRING OPERATIONS
REPETITION
It is used to repeat elements in the sequence by specified number of times..
The ( * ) multiplication operator is used to perform repetition.
Items are not copied but are referenced multiple times.
Eg: Eg:
fruits=("Apple","Orange“) fruits=(0,2,3)*3
print(fruits*2) print(fruits)
OUTPUT: OUTPUT:
(‘Apple', 'Orange', 'Apple', 'Orange‘) (0, 2, 3, 0, 2, 3, 0, 2, 3)
LIST OPERATIONS
MEMBERSHIP
 Membership operators are used to check if value exists in the tuple or not..
Types of membership operators are:

Operator Usage Example Output


True- if value/variable is found fruits=("Apple","Orange“)
in True
in the sequence print("Apple" in fruits)
True- if value/ variable is not fruits=("Apple","Orange“)
not in False
found in sequence print("Apple" not in fruits)
TRAVERSING A LIST
 Traversing means visiting every element of the tuple at least once.
 It starts at the beginning.
 It is also called as “Iterating”.
USING WHILE LOOP:
USING FOR LOOP:
a =("Apple", "orange", "Apple“)
a =("Apple", "orange", "Apple“)
i=0
for i in range(len(a)):
while i <len(a):
print(a[i]) print(a[i])

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

It adds the elements of T=(2,4.5,3.3,6)


print("Total value in list is: ",sum(L))
sum() the sequence and OUTPUT:
returns the sum. Total value in list is: 15.8
MODIFYING TUPLE
 Tuples are immutable .
 But tuples can be modified using explicit methods.

Adding New elements in a Tuple:


You can add or modify an element of a tuple by following indirect method.
To insert a new item in tuple, it needs to be converted into a list using list( )
function.
ADDING NEW ELEMENTS IN A TUPLE:
#Converting tuple into list # Inserting an element at desired location
T=(1,2,3,4,5,"apple") Temp[2]=40
print(Temp)
Temp=list(T)
OUTPUT:
print(Temp)
[1, 2, 40, 4, 5, 'apple']
OUTPUT:
[1, 2, 3, 4, 5, 'apple'] # Converting list back to Tuple
T_new=tuple(Temp)
print(T_new)
OUTPUT:
(1, 2, 40, 4, 5, 'apple')
MODIFYING TUPLE
Packing and Unpacking Tuple:
Tuple can be modified using packing-unpacking method.
PACKING: To create a tuple from a set of values.
UNPACKING: Separating individual values from a tuple.
Syntax: For unpacking in Python:
<variable1>,<variable2>,<variable3>,……=tuple_name
The number of variables at left side of the assignment must match with the number
of elements in the tuple at right side
Eg: a is assigned value 2 T1=(1,2,3,4,5)
T1=(2,3,4,5) b is assigned value 3 a,b,c,d,e=T1
c is assigned value 4 print(a,b,c)
a,b,c,d=T1 d is assigned value 5 OUTPUT:
123
PYTHON NESTED TUPLE
 A tuple within tuple.
 Each sub-tuple is considered as an element of the main tuple.
Eg:
T=((“one”,”two”,”three”),(“four,five”))

You might also like