Python Tuple

  1. Python tuple is a immutable object  ,it is combination of homogeneous or heterogeneous data types
  2. To access the elements in tuple we can use index,which start from 0 and stop with n-1
  3. In tuple elements are stored in sequence manner
  4. In tuple every element is separated by comma
  5. We can create empty tuple also in python.
  6. In tuple elements are enclosed in paranthesis “()”

       Different Types of Syntax:

Creating Empty tuple:

tuple=()

Note: It will create empty tuple

Create tuple with data:

tuple_name=(data1,data2,data3……….datan)


How print the complete tuple:

Example:

COMPILE & RUN

Output:

How to access the tuple elements:

To access the elements in tuple there two tw type of indexing is available.
They are

1.Forward Indexing

2.Backward Indexing


1.Forward Indexing:

The starting index ( mostly  ‘0 ‘ ) is positive and ending index (n-1) is also positive then we can call it as Forward Indexing i.e almost it is in ascendiing order.i.e consit tuple having an elements are 1,2,3,4,5,6,7

my_tuple=(1,2,3,4,5,6,7)

In this case start index is ‘0’ and end index is ‘6’

my_tuple[0] is 1

my_tuple[1] is 2

my_tuple[2] is 3

my_tuple[3] is 4

my_tuple[4] is 5

my_tuple[5] is 6

my_tuple[6] is 7

Below is the example is illustrating accessing the tuple elements for different data types

Examples: 

COMPILE & RUN

Output:

 

 2.Backward Indexing:

The starting index ( mostly  ‘-n’ ) is nagative number and ending index (-n+(n-1)) is also negative number then we can call it as Backward Indexing i.e consider tuple having an elements are 1,2,3,4,5,6,7 i.e here no.of elements are 7 so starting index (-n) means -7 and ending index means (-n+(n-1)) -1

my_tuple=[1,2,3,4,5,6,7]

In this case start index is ‘0’ and end index is ‘6’

my_tuple[-7] is 1

my_tuple[-6] is 2

my_tuple[-5] is 3

my_tuple[-4] is 4

my_tuple[-3] is 5

my_tuple[-2] is 6

my_tuple[-1] is 7

Below is the example is illustrating accessing the tuple elements for different data types

Example:

COMPILE & RUN

Output:

 

Note: If we observe above both examples ,here we can consider “tuple__nums “ tuple_ explanation

tuple__nums [0]=tuple__num[-4]=10

tuple__nums [1]=tuple__num[-3]=20

tuple__nums [2]=tuple__num[-2]=30

tuple__nums [3]=tuple__num[-1]=40

How to slice the element in tuple:

tuple slice can be used slice particular part of the tuple and as well as to modify the particular part of the tuple we can use the slice mechanism in tuple

Example:

COMPILE & RUN

Output:

 

How to replicate the python tuple:

Here Replicate means adding the same tuple multiple time wit all elements, we can achieve this mechanism by using ateristic symbol (‘*’)

Example:

COMPILE & RUN

Output:

 

   How to add  tuple,delete the tuple:

Adding Two tuple:

Example:

COMPILE & RUN

Output:

 

Update Tuple :

We should not update the tuple because of it is immutable,it means it is having the constant values

1.del method

1.del method:

 We delete the complete tuple,But we can’t delete the specific or individual elements in tuple by usong delete method.

Syntax:

del(index)  Note: Here index might be a single value ,slice of tuple also we can use

Example:

COMPILE & RUN

Output:

 

Max,Min,Count,Len and Sum Methods:

Max Method:

            This Method is used to find the biggest element in the tuple.

Min Method:

            This Method is used to find the smallest element in the tuple.

Count Method:

            This Method is used to count the occurance of specific element in the tuple.

Len Method:

            This Method is used to count the totla elements  in the tuple.

Sum Method:

            This Method is used find the sum of the elements in the tuple.

Example:

COMPILE & RUN

Output:

 

Share this website !!!
Facebook Twitter Linkedin