TUPLE
Know Python Bytes
w w w. k n o w p y t h o n b y te s . b l o g s p o t . c o m
Mrs. Payal Bhattacharjee, PGT(C.Sc.)
K V No.1 Kanchrapara
KVS-RO(Kolkata)
CONTENTS
( Learning Outcomes )
TUPLES in Python
XI CS 2020-21
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;
Nested Tuples;
finding the maximum, minimum, mean of values stored in a Tuple
Linear Search on a Tuple of numbers
Counting the frequency of elements in a Tuple
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 -
Functions/methods
n-1. Here n is number of items in a Tuple.
Tuples Slicing; • Tuples are immutable sequences i.e.
Nested Tuples; we canNOT change elements of a Tuple in
finding the maximum,
minimum, mean place.
linear search on Tuple • Tuples can contain values of mixed data
counting the frequency
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 -1
Tuple Definition
BACKWARD INDEX DIRECTION
Indexing in a Tuple
Creation of a Tuple
Traversal of a Tuple
Operations on a Tuple NOTE:-
Functions/methods
The index (also called subscript) is the numbered
Tuples Slicing;
Nested Tuples; position of a letter in the Tuple. In python, indices begin
finding the maximum, from
minimum, mean 0,1,2,…. upto (length-1) in the forward
linear search on Tuple direction and
counting the frequency
-1,-2,-3,….. (-length) in the backward direction.
where length is the length of the Tuple.
CREATION of Tuple
CONTENTS
Tuples
Tuple of numbers created
without name
Intro: Tuple of characters created
without name
Tuple Definition
Indexing in a Tuple
Creation of a Tuple of mixed data type created
without name
Tuple
Traversal of a Tuple Tuple of numbers with name T
Operations on a Tuple
Functions/methods
Empty Tuple created without name
Tuples Slicing;
Nested Tuples; T
finding the maximum,
minimum, mean
linear search on Tuple Empty Tuple created with name t using
counting the frequency funcion Tuple()
Creation of Tuple contd.(Long Tuple and Nested Tuple)
CONTENTS
Tuples
Creation of a long Tuple with name MultiTens
Intro:
Tuple Definition
Indexing in a Tuple
Creation of a Tuple
Traversal of a Tuple
Operations on a Tuple
Functions/methods
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean Creation of a Nested Tuple with name
linear search on Tuple
counting the frequency T that is Tuple inside a Tuple
Creation of Tuple from existing sequences
CONTENTS Creating Tuple from Creating Tuple from tuple
string
Tuples Intro:
Tuple Definition
Indexing in a Tuple
Creation of a Tuple
from string,tuple, Creating Tuple by taking input from the keyboard(user)
Tuple(input()),
eval(input())
Traversal of a Tuple
Assign/change values in Tuple
Operations on a Tuple **Note: All the elements are considered as string
Functions/methods
Tuples Slicing; Creating Tuple by taking input from the keyboard [using eval()]
Nested Tuples; ]method]
finding the maximum,
minimum, mean
linear search on Tuple
counting the frequency
**Note: eval( ) identifies the type by looking at the given
expression
TRAVERSAL of a Tuple
CONTENTS
Tuples
Intro:
Tuple Definition
Indexing in a Tuple
Creation of a Tuple
Traversal of a Tuple
Operations on a Tuple
Functions/methods
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
counting the frequency
TRAVERSAL of NESTED Tuple
CONTENTS
Enclosing Tuple
Tuples Intro:
Tuple Definition
Indexing in a Tuple
Nested Tuple 1 Nested Tuple 2
Creation of a Tuple
Traversal of a
Tuple
Assign /change values in
Tuple Note: Denoting Nested elements using Index nos.
N[0] 10
Operations on a Tuple
N[1] [15,30]
Functions/methods
Tuples Slicing; N[1][0]15
Nested Tuples; N[1][1]30
finding the maximum, N[2] *80,’Mask’,60+
minimum, mean
N[2][0]80
linear search on Tuple
counting the frequency N[2][1]‘Mask’
N[2][2]60
Assign/Change values in a Tuple
Assigning /
CONTENTS Changing
values of
Tuples Intro: elements of
a Tuple is
Tuple Definition NOT
Indexing in a Tuple POSSIBLE.
Creation of a Tuple
Traversal of a Tuple
Assign /change
values in Tuple
Operations on a Tuple
Functions/methods
Tuples Slicing; Assigning / Changing values of
Nested Tuples; elements of a Tuple can be done
by converting the tuple to a LIST
finding the maximum,
using list() method , changing
minimum, mean
value of the list and then again
linear search on Tuple converting it to a tuple using
counting the frequency tuple() method.
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 number
OPERATIONS
ON Tuple
Concatenation/Joining
Repetition/Replication
Membership Tuple CANNOT be concatenated with a string
Comparison of Tuples
Functions/methods
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean Tuple CANNOT be joined with a complex nos.
linear search on Tuple
counting the frequency
Concatenation refers
to Joining of two Tuple CAN be concatenated or
objects joined with another Tuple
OPERATIONS on Tuple Replication
CONTENTS
Tuples Introduction: Replication refers to Repetition of objects as it
OPERATIONS is. Here, in TUPLE we can see NON-
ON Tuple VECTORISED operation with a tuple where the
entire tuple as a whole is replicated.
Concatenation/Joining
Repetition/ NOTE: Vectorised operation, refers to the
Replication replication of individual elements of the object.
Membership
Comparison of Tuples Replicating the unnamed
Functions/methods Tuple two times
Tuples Slicing;
Nested Tuples;
finding the maximum,
minimum, mean
linear search on Tuple
Replicating the named Tuple RL
counting the frequency
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
Replication LHS of in operator in the sequence mentioned in the RHS
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 operators
Tuples Introduction:
==,!=,<,<=,>,>= are used
OPERATIONS in comparing two objects.
ON Tuple Here, for example two
Tuple objects are
Concatenation/Joining compared.
Repetition/
Relational operators ==,!=
Replication
can be used in comparing
Membership in
two objects eg. Tuple with
Tuple
number/string/tuple. But,
Comparison Relational operators
<,<=,>,>= always returns
of Tuples ERROR when used in
Functions/methods
Tuples Slicing;
comparing two objects eg.
Nested Tuples; Tuple with
finding the maximum, number/string/tuple.
minimum, mean Eg. Tuple CANNOT be
linear search on Tuple compared with a number
counting the frequency
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()
CONTENTS The tuple() function creates a Tuple object.
A Tuple object is a collection which is ordered and
changeable.
Tuples Introduction:
tuple() function
Operations on a Tuple takes
Functions/m elements/values
inside the
ethods parantheses i.e. ()
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(),
index(), sorted() NOTE: The sorted( )
,min(), max(), sum() method won’t be able
Tuples Slicing; to sort the values of a
Nested Tuples; Tuple if it contains
finding the maximum, complex
minimum, mean numbers/tuple /
linear search on mixed datatype as its
elements.
Tuple
counting the
frequency
Functions / Methods on Tuple- min(),max(),sum()
CONTENTS min( ) method,
Returns the minimum out of the elements/values/items of
the given Tuple
Tuples Introduction:
Operations on a Tuple max( ) method,
Functions/ Returns the maximum out of the elements/values/items of
the given Tuple
methods
len(), tuple(), count(), sum( ) method,
index(), sorted(),min(), Returns the sum of all the values/elements/items of the
max(), sum() given Tuple.
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.
L[start:stop:step] creates a Tuple slice out of Tuple T with elements falling
between indexes start and stop, not including value at the stop index, and skipping
step elements in between.
NOTE: In absence of any start, stop, step values, we have Default start index as the beginning,
stop index as the last index and step value as +1.
FORWARD INDEX DIRECTION
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
BACKWARD INDEX DIRECTION
Tuple slicing
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, stop index is (10-1=9)
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 -4(backward), stop index is (6-1=5 (forward)). Here, it is
not getting any value in the range, hence empty.
Start index -4(which is ‘Y’ as per the backward index,) stop index is 25
which is beyond the last index. 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 -3(backward), stop index is -12(backward). Here, it is getting value in
the range as step value is -1 mentioned in reverse.
Start index -20(which is beyond backward index,) stop index is -5 which takes value till
(-5-1=-6). Here, -6 is ‘N’.
Start index 1, stop index is 13 (till 13-1=12),step
value is 2 in forward direction. Hence, it will
fetch/take alternate elements with a skip of 1
value in between
TRY TO UNDERSTAND YOURSELF. A task for you.
? Also write the slice for Tuple to get the output
as
Default start and stop is beginning and end index of the Tuple. Here, step is 3.
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,
minimum, mean
linear search on Tuple
counting the frequency
Note: Denoting Nested elements
using Index nos.
M[0] 10
M[1] [15,30]
M[1][0]15
M[1][1]30
M[2] *80,’Mask’,60+
M[2][0]80
M[2][1]‘Mask’
M[2][2]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 Slicing; tuple beforehand
Nested Tuples;
finding
the
maximum
, minimum, mean
linear search
on Tuple OUTPUT
counting the
frequency
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
minimum,
maximum , mean
linear search
on Tuple OUTPUT
counting the
frequency
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
on Tuple
counting the OUTPUT
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