0% found this document useful (0 votes)
9 views

(v) Tuple in Python

The document provides a comprehensive overview of tuples in Python, covering their definition, creation, indexing, and various operations such as concatenation, replication, and membership testing. It details built-in functions and methods applicable to tuples, including len(), count(), and sorted(), as well as the concept of tuple slicing and nested tuples. Additionally, it emphasizes the immutability of tuples and the limitations compared to lists.

Uploaded by

jainavish45
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

(v) Tuple in Python

The document provides a comprehensive overview of tuples in Python, covering their definition, creation, indexing, and various operations such as concatenation, replication, and membership testing. It details built-in functions and methods applicable to tuples, including len(), count(), and sorted(), as well as the concept of tuple slicing and nested tuples. Additionally, it emphasizes the immutability of tuples and the limitations compared to lists.

Uploaded by

jainavish45
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

TUPLE

Know Python Bytes


www. knowpythonbytes. blogspot. com
Mrs. Payal
Bhattacharjee, PGT( C.
CONTENT
( Learning
S
TUPLES in
Outcomes ) 

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

Intro: Tuple of characters created


without name
Tuple
Definitio
n Tuple of mixed data type created
Indexing without name
in a Tuple
Creation Tuple of numbers with name T
of a
Tuple Empty Tuple created without name
Traversal of
a Tuple T
Operations on a
Tuple
Functions/ Empty Tuple created with name t using
methods funcion Tuple()
 Tuples Slicing;
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 Creation of a Nested Tuple with name
Functions/
methods
 Tuples Slicing;
T that is Tuple inside a Tuple
 Nested Tuples;
finding the
maximum,
minim
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
Creating Tuple by taking input from the keyboard(user)
from string,tuple,
Tuple(input()),
eval(input())
Traversal of a Tuple
Assign/change values in Tuple
**Note: All the elements are considered as string
Operations on a
Tuple
Functions/methods Creating Tuple by taking input from the keyboard [using eval()]
 Tuples Slicing; ]method]
 Nested Tuples;
finding the
maximum,
minimum,
mean
 linear search on **Note: eval( ) identifies the type by looking at the given
Tuple 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
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 Note: Denoting Nested elements using Index nos.
Assign /change
N[0]  10
Operations on values
a Tuplein
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]
 linear search on Tuple
 counting the frequency [0]80 N[2]
[1]‘Mask’ N[2]
Assign/Change values in a Tuple
Assigning /

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

index(), sorted() NOTE: The sorted( )


method won’t be able
,min(), max(), sum() to sort the values of a
 Tuples Slicing; Tuple if it contains
 Nested Tuples; complex
finding the maximum, numbers/tuple /
minimum, mean mixed ts
 linear search on datatype as i
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,
Returns the maximum out of the elements/values/items
Functions/ of the given Tuple
methods
len(), tuple(), count(), sum( ) method,
index(), sorted(),min(), Returns the sum of all the values/elements/items of
max(), sum() the 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

? Also write the slice for Tuple to get the output

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

You might also like