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
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(),
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 -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.
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 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