0% found this document useful (0 votes)
9 views20 pages

Python Tuples: Definition and Operations

The document provides an overview of tuples in Python, highlighting their immutable nature, methods of declaration, and various operations such as concatenation, replication, and slicing. It also covers accessing tuple elements, membership testing, pre-defined functions, and methods associated with tuples. Additionally, it includes a list of programming exercises related to tuples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Python Tuples: Definition and Operations

The document provides an overview of tuples in Python, highlighting their immutable nature, methods of declaration, and various operations such as concatenation, replication, and slicing. It also covers accessing tuple elements, membership testing, pre-defined functions, and methods associated with tuples. Additionally, it includes a list of programming exercises related to tuples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Tuples

Features of Tuples
 In Python, Tuples consider as a immutable data type i.e. Values in the tuples
cannot be modified.
 In Python there are two ways to declare a tuple variable:
(a) Using ( ) bracket
(b) Using tuple ( ) pre-defined function
 A tuple which contains no element is called Empty tuple.
 A tuple is a collection of values or an ordered sequence of values/items.
 The items in a tuple can be of any type such as string, integers, float or even a
tuple.
 Each tuple element is separated by commas.
2
Features of Tuples
 In Python we can identify each tuple element by their index location.
 In Python we can access individual element of a tuple using positive index
location as well as negative index location.
We can perform following operations on list variable:
(a) Concatenation
(b) Replication
(c) Traversing
(d) Slicing

3
Declaring / Creating a tuple
 In Python there are two ways to declare a tuple variable :
(a) Using ( ) bracket : Tuple variable can be created by putting comma-
separated values/items within square bracket. The syntax for creating a list is
tuplevariablename = ( element1, element2, element3, element4…..)

4
Declaring / Creating a Tuple
 (b) Using tuple ( ) pre-defined function : This function takes sequence types and
convert a given sequence into a tuple. If no sequence is specified then it will
create an empty tuple.

5
Accessing Tuple Elements
A tuple is a sequence of values which can be of any type and they are
indexed by integer value. The individual elements of a tuple can be accessed
through their indexes given in square brackets.

6
Operations on Tuple
 Concatenation : Concatenation is a process in which multiple
sequences/tuples can be combined together with help of + operators. Python
allows combining or adding of two tuples using ‘+’ concatenation operator.

 Replication : Multiply ( * ) operator replicates the tuples for a specified


number of times.

7
Operations on Tuple
 Traversing : Traversing a tuple means accessing each element of a tuple.
This can be done by using either for or while looping statement.

8
Operations on Tuples
Slicing is used to retrieve a subset of values. Remember index on last limit is
not included in the tuple slice.
tuplevariablename[start:stop:step]

9
Membership Testing
The ‘in’ operator checks whether a given element is contained in a tuple. It
returns true if element appears in the list, otherwise returns false. The ‘not
in’ operator returns true if element does not appear in the sequence, other
wise returns false.

10
Pre-Defined Functions / Methods / Statement
Pre-Defined Functions : tuple ( ) , len( ) , sorted( ), min( ), max( ), sum ( )

Pre-Defined Methods :

count( ), index( )

11
Pre-Defined Functions
 len( ) function : This function is used to find total number of elements or
length of the tuple variable.

 sorted( ) function : This function is used to arrange tuple elements in


ascending or descending order. By default it will arrange tuple element in
ascending order.

12
Pre-Defined Functions
 min( ) function : This function is used to find minimum number from a
given tuple.
 max( ) function : This function is used to find maximum number from a
given tuple.
 sum ( ) function : This function is used to find sum of all elements from a
given tuple.

13
Pre-Defined Methods
 count ( ) : This method is used to count the frequency of a given element in
a given tuple.

 Index( ) : This method is used to find the index location of a specified


element from a given tuple.

14
Nested Tuple

15
How to input tuple element at the run time?

16
To find sum of all tuple elements without using sum( ) function

17
Packing and Unpacking of Tuple
Write a Python code to input an tuple and add an element at specified index location.

18
List of Programs
1) Write a Python code to declare (different types of tuples) and print all
elements of a tuples.
2) Write a Python code to print for adding and removing elements from a
tuples.
3) Write a Python code to input a tuple and add an element at the specified
index location.
4) Write a Python code to input a tuple and remove an element from the
specified index location.
5) Write a Python code to input a tuple and remove an element from the
tuple according to specified name.
6) Write a Python code to input a tuple and count the frequency of a given
element without using count( ) method.
7) Write a Python code to input a tuple and calculate the sum and mean of a
given tuple of numbers. 19
List of Programs
8) Write a Python code to input a tuple and display even and odd elements
separately from a given tuple.
9) Write a Python code to input a tuple and search an input element in a
given tuple of numbers.
10) Write a Python code to input a tuple, replicates it twice and then prints
the sorted tuple in ascending and descending order.
11) Write a Python code to input a tuple and count total even and odd
elements separately from a given list.
12) Write a Python code to input a tuple of integers and replace elements
having odd values with thrice their value and elements having even
values with twice their value.
13) Write a Python code to input a tuple of integers and divide all the list
elements by 7 which are divisible by 7 and multiply other elements by 3.
20

You might also like