(v) Tuple in Python
(v) Tuple in Python
XI CS 2020-21Python
Tuples Introduction: Definition, Creation of a Tuple,
Traversal of a Tuple.
Operations on a Tuple – concatenation/joining,
repetition/replication, membership;
Functions/methods–
len(), tuple(), count(),
index(),
sorted(),min(), max(),
sum()
Tuples Slicing;
TUPLE
INTRODUCTION
What is a Tuple ?
CONTENTS
• A Tuple is a collection of items which are
Tuples formed by placing comma-separated
Intro: values in round brackets, also we say them
as parenthesis ()
Tuple Definition
Indexing in a Tuple • Each item/element has its own index
Creation of a Tuple number.
Traversal of a Tuple. • Index of first item is 0 and the last item is
Operations on a
Tuple -
n-1. Here n is number of items in a Tuple.
Functions/ • Tuples are immutable sequences i.e.
methods
we canNOT change elements of a Tuple in
Tuples Slicing;
Nested Tuples; place.
finding the • Tuples can contain values of mixed data
maximum,
minimum, mean types.
INDEXING in a Tuple
CONTENTS FORWARD INDEX DIRECTION
0 1 2 3 4 5
Tuples 10 20 30 40 50 60
Intro: -6 -5 -4 -3 -2
Tuple
Definitio BACKWARD INDEX
n -1
Indexing in a DIRECTION
Tuple NOTE:-
Creation of a Tuple The index (also called subscript) is the numbered
Traversal of a Tuple position of a letter in the Tuple. In python, indices begin
Operations on a Tuple
from
Functions/methods
Tuples Slicing; 0,1,2,…. upto (length-1) in the forward
Nested Tuples; direction and
finding the maximum,
minimum, mean -1,-2,-3,….. (-length) in the backward
linear search on Tuple direction.
CREATION of Tuple
CONTENTS
Tuple of numbers created
Tuples without name
CONTENTS Changing
values of
elements of
Tuples Intro: a Tuple is
NOT
Tuple Definition POSSIBLE.
Indexing in a Tuple
Creation of a Tuple
Traversal of a Tuple
Assign /change
values in Tuple
Operations on a Tuple
Functions/methods Assigning / Changing values of
Tuples Slicing; elements of a Tuple can be done
Nested Tuples; by converting the tuple to a LIST
finding the maximum, using list() method , changing
value of the list and then again
minimum, mean converting it to a tuple using
linear search on Tuple tuple() method.
counting the frequency In this way, you also get a copy of
the tuple in the form of a List.
Here, T is the tuple and L is the
List.
OPERATIONS on Tuple Concatenation
CONTENTS Tuple CAN be concatenated or
joined with another Tuple
Tuples Introduction: Tuple CANNOT be concatenated with a
OPERATIONS number
ON Tuple
Concatenation/Joining
Repetition/Replication
Membership Tuple CANNOT be concatenated with a
Comparison of Tuples string
Functions/methods
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple Tuple CANNOT be joined with a complex
counting the frequency nos.
Concatenation
refers to Joining
of two objects
Tuple CAN be
OPERATIONS on Tuple Replication
CONTENTS
Tuples Introduction: Replication refers to Repetition of
OPERATIONS objects as it is. Here, in TUPLE we can
ON Tuple see NON- VECTORISED operation with a
tuple where the entire tuple as a whole
Concatenation/Joining is replicated.
Repetition/
Replication NOTE: Vectorised operation, refers to
Membership the replication of individual elements of
Comparison of Tuples the object. Replicating the
unnamed Tuple
Functions/methods
two times
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
Replicating the named Tuple
counting the frequency
RL
four times
OPERATIONS on Tuple Membership
Membership operator tests for Membership in a sequence.
CONTENTS Returns Boolean True or False
Tuples Introduction: OPERATOR DESCRIPTION
OPERATIONS
in Results to True value if it finds a variable at the LHS of in
ON Tuple operator in the sequence mentioned in the RHS of the in
operator, else it returns False
Concatenation/Joining
Repetition/ not in Results to True value if it does not find a variable at the
LHS of in operator in the sequence mentioned in the RHS
Replication of the in operator, else it returns False
Membership
in Tuple
Comparison of Tuples
Functions/methods
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
counting the frequency
OPERATIONS on Comparison of Tuple
CONTENTS Comparison of Tuple returns Boolean True or False.
Relational
Tuples Introduction:
==,!=,<,<=,>,>=
operators are
OPERATIONS used in comparing two
ON Tuple objects.
Here, for example
Concatenation/Joining two Tuple objects
Repetition/ are compared.
Relational operators
Replication
==,!= can be used in
Membership in
comparing two objects
Tuple
eg. Tuple with
Comparison number/string/tuple.
But, Relational
of Tuples operators
Functions/methods
Tuples Slicing; <,<=,>,>= always
Nested Tuples; returns ERROR when
finding the used in comparing
maximum, two Eg.
objects eg.
Tuple CANNOT
minimum, Tuple
be with
mean compared with a
number/string/tu
number
linear search on ple.
Functions / Methods on Tuple-len()
CONTENTS len() function counts the no. of elements in the
Tuple
Tuples Introduction:
Operations on a Tuple
Functions/
methods
len(), tuple(),
count(),
index(), sorted(),min(),
max(),
sum()
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean TOTAL 5 elements of the Tuple object
linear search on Tuple
counting the frequency
Functions / Methods on Tuple- tuple()
The tuple() function creates a Tuple object.
CONTENTS A Tuple object is a collection which is ordered and
changeable.
Tuples Introduction: tuple() function
Operations on a Tuple takes
elements/values
Functions/m inside the
parantheses i.e. ()
ethods
len(), tuple(),
count(), index(),
sorted(),min(), max(), sum()
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
counting the frequency NOTE: Python language is case-sensitive.
Here, Z is the Tuple object , so, z in small letters gives an error.
Functions / Methods on Tuple- count()
CONTENTS count( ) method,
returns the count of the item (no. of times the
Tuples Introduction:
item /element/value appears) that is passed as
Operations on a Tuple
argument. If the given item is not in the
Functions/ Tuple, it will return 0
methods
len(), tuple(),
count(), index(),
sorted(),min(), max(),
sum()
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
counting the frequency
Functions / Methods on Tuple- index()
CONTENTS index( ) method, returns the index of
the first matched item in the Tuple
Tuples Introduction:
Operations on a Tuple Forward
Index 0 1 2 3 4 5
Functions/
methods
len(), tuple(), count(),
index(),
sorted(),min(),
max(), sum()
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
counting the frequency
Functions / Methods on Tuple- sorted()
CONTENTS sorted( ) method,
sorted( ) method and sorted(reverse=True) sorts or
Tuples Introduction:
orders the items of the Tuple, by default in increasing order.
Operations on a Tuple
sorted(reverse=True ) for getting the Tuple in decreasing order
Functions
/ methods
len(), tuple(), count(),
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
counting the frequency
Functions / Methods on Tuple- min(),max(),sum() contd..
Tuple of string . Maximum and
Minimum is done on the basis
of the ASCII values of the
characters from the beginning.
Eg. ‘A’=65, ‘a’ = 97, ‘b’=98 and
so on
A Tuple with
mixed data
type cannot
return values
for the min,
max and
sum
functions.
NOTE
Unlike a List object,
an object of Tuple do not have the following
methods:
– Reverse()
– Append()
– Extend()
– Clear()
– Remove()
– Pop()
Tuple slicing
L[start:stop] creates a Tuple slice out of Tuple T with elements falling between
indexes start and stop, not including value at the stop index.
Start index 5, stop index is 50 which is beyond the last index in forward
direction. Hence, it will stop at the last index
Start index 10, stop index is 30 which is beyond the last index
in forward direction. Hence, it will stop at the last index
Start index -10(which is ‘Y’ as per the
backward index,) stop index is 12-
1=11 (forward indexing). Hence, it
will stop at the (stop index-1)., ‘Y’ is
at 11 index no.
TUPLE SLICING contd.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
K N O W P Y T H O N B Y T E S
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Start index -3(backward), stop index is -12(backward). Here, it is not getting any value
in the range as no step value mentioned in reverse. Hence empty.
Start index -12(which is ‘W’ as per the backward index,) stop index is -3 (which takes
values till index -3-1= -4)
Start index -20(which is beyond backward index,) stop index is -5 which takes value till
(-5-1=-6). Here, -6 is ‘N’.
Default start and stop is beginning and end index of the Tuple. Here, step is 3.
as
NESTED Tuples
A Tuple within a Tuple is Nested Tuple.
CONTENTS
Tuples Intro Enclosing Tuple
Operations on a Tuple
Functions/methods
Tuples Slicing;
Nested Tuple 1 Nested Tuple 2
Nested
Tuples;
finding the
maximum,
minimu
counting m,
themean
frequency
Note:
linear Denoting
search on Nested elements
Tuple using Index nos.
M[0] 10 M[1]
[15,30]
M[1
]
[0]
15
M[1][1]30
M[2] *80,’Mask’,60+
Program to find the MAXIMUM element from a Tuple of element along
with its index in the Tuple without using max() library function
CONTENTS
Tuples Intro
Operations on NOTE: We are not taking
a Tuple
append method as Tuple is
Functions/
methods immutable. So, declare the
Tuples tuple beforehand
Slicing;
Nested
Tuples;
finding
the
maximu
m OUTPUT
, minimum,
mean
linear search
on Tuple
counting the
Program to find the MINIMUM element from a Tuple of element along
with its index in the Tuple without using min() library function
CONTENTS
Tuples Intro
Operations on a
Tuple
Functions/
methods
Tuples
Slicing;
Nested
Tuples;
finding
the
minimu
m, OUTPUT
maximum ,
mean
linear search
on Tuple
counting the
Program to find the MEAN (AVERAGE) of all the elements of
the Tuple
CONTENTS
Tuples Intro
Operations on a
Tuple
Functions/
methods
Tuples
Slicing;
Nested
Tuples;
finding
the mean,
maximum,
minimum
linear search OUTPUT
on Tuple
counting the
frequency
Program to search an element in the Tuple (LINEAR SEARCH)
CONTENTS
Tuples Intro
Operations on a
Tuple
Functions/
methods
Tuples Slicing;
Nested Tuples;
finding the
mean, maximum,
minimum
linear
search on
Tuple
counting the
frequency
OUTPUT
Program to count frequency (/no. of times of occurrence) of an element in the Tuple
(FREQUENCY COUNT)
CONTENTS
Tuples Intro
Operations on a
Tuple
Functions/
methods
Tuples Slicing;
Nested Tuples;
finding the
mean, maximum,
minimum
linear search
on Tuple
counting
the
frequency
OUTPUT
Bibliograhy and References
• Google
• Wikipedia
• XI Computer Science , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
• XI Informatics Practices , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
Programming is
an ART….
Add your colours to it and
make your own