Tuples in Python
Tuples in Python
Tuples in Python
Class - 11
Tuples are a lot like lists, and that's why we can define them in a pretty similar way as we did to define
the lists. Simply put, a tuple is a sequence of data.
What makes them different from lists is that tuples are immutable, i.e., the data inside the tuples can't
be modified, which is opposite in the case of lists. Other than this, tuples are very similar to lists and
that would make it much easier for us to understand as we already know about lists.
Defining a Tuple
A tuple is a standard data type of Python than can store a sequence of values belonging to any type and
the values should be enclosed in parenthesis i.e. round brackets. Tuples are immutable sequences
of python.
To define a tuple, we just have to assign a single variable with multiple values separated by commas,
and that variable will be known as a Tuple.
Example:-
OR
t = tuple()
OR
>>> t = 4,
OR
>>> t = (4,)
>>> t = ('a')
>>> t
'a'
OR
>>> t = 4,
>>> t
(4,)
OR
>>> t = (4,)
>>> t
(4,)
Nested tuples:-
one tuple contain another tuple within it.
Example:
t = tuple(sequence)
Example:
Example:
Example:
Output:
Example:
Output:-
Note: In the above example, you see using tuple( ) method, even you
not put parenthesis, it will create tuple using individual characters
as elements
Example:
Output:
So, before accessing elements first we understand the indexing of elements which is
similar as lists in python.
Indexing in Tuples
Indexing in tuples is also pretty similar to that in lists, the first element has index zero, and it keeps on
increasing for the next consecutive elements. Also, backward indexing is also valid in tuples, i.e., the
last element can be accessed using the index -1 and the consecutive previous numbers by -2, -3 and so
on. Let's take an example,
Example:
t = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100) # t is a tuple
Example:
Concatenation (+) and Replication operators (*) – works same as for other
sequences.
Example:-
Example:-
Traversing a tuple – accessing all elements in tuple also works same as Lists.
Example:
Output:
OR
Output:
Joining tuples:-
the ‘+’ operator is used to join tuples.
Example:
Example:
Example: ‘+’ operator works if both the operands must be of tuple types. But if not then
it generates error….as shown below:
So, the solution of the problem is to make the number as tuple types…..as shown below:
Slicing in Tuples
Slicing in tuples, works exactly the same like in the case of lists. Let's start with an example:
Slicing can be done backwards as well using the negative indexes for traversing the tuple from
backward direction.
Example:
on the above example 0 indicate starting value, 7 indicate ending value, and 2 indicate the step value
Example:-
Comparing Tuples:-
Example:-
Example:-
Example:-
Output:
Deleting a Tuple:-
In order to delete a tuple, the del keyword is used.
Example:-
Tuple Functions:-
1) len() function:-
As you might have already guessed, this function is used to get the number of elements inside any
tuple.
2) max() function :-
To find the maximum value in a tuple, we can use the max() function.
3) min() function :-
To find the minimum value, min() function can be used.
4) sum() function:-
It returns the sum of all elements of a tuple.
output:
5) sorted() function:-
arrange the elements in ascending order and returns a list of sorted elements.
Output:-
Output:-
6) index() function :-
It returns the index of an existing element of a tuple.
7) count() function :-
It returns number of occurrence of a given elements in a tuple.
Tasks:-
1) Write a program that inputs two tuples and creates a third that contains all elements of the
first followed by all elements of the second.
2) Write a program which takes a tuple as input from the user and find the second largest
element in the tuple. you can use any of the standard python tuple’s functions to obtain your
result.