Example HTML page

Tuples in Python

Tuples

A tuple is another data type in Python. Tuples are similar to the list. A tuple contains number of items separated by commas.

One could notice that a tuple is enclosed with ( ) and the items in it cannot be changed. Whereas a list is enclosed with [ ] and the items in it can be changed.

Creating tuples

We can create a tuple by inserting some items into a ( ) braces, separated by commas.

>>> t=( )                                                    #Creating an empty list

>>> t

( )

>>> t=(“hi”,54,12.7)                                  #Inserting elements

>>> t

(‘hi’, 54, 12.7)

We can also create a tuple with in another tuple i.e., nested tuple. It is shown in below example.

>>> a=(1,2,(“abc”,4),5)

>>> a

(1, 2, (‘abc’, 4), 5)

Accessing elements in a Tuple

We can access the elements in a tuples by the following way.

>>> tup=(“abc”,2,”xyz”,34.567)

>>> tup

(‘abc’, 2, ‘xyz’, 34.567)

>>> tup[1]

2

>>> tup[-3]

2

>>> tup[0]

‘abc’

>>> tup[:2]

(‘abc’, 2)

>>> tup[1:]

(2, ‘xyz’, 34.567)

>>> tup[1:-2]

(2,)

Updating Tuple

Tuples are immutable i.e., we cannot change/update the elements in tuple once it is created. It is only possible on nested elements which are it-self immutable like lists. It is shown in below example.

>>> a=(1,2,[3,4,5])

>>> a

(1, 2, [3, 4, 5])

>>> a[2][0]=9

>>> a

(1, 2, [9, 4, 5])

>>> a[2][2]=12

>>> a

(1, 2, [9, 4, 12])

In the above example [3,4,5] is declared as list and is considered as one of the elements in a tuple. Since the lists are mutable so that the elements are updated in such case. Otherwise we cannot update.

Deleting Tuple

As we mentioned above that we cannot update/delete elements in a tuple after it is created. But it is possible to delete entire tuple using del keyword. It is shown in below example.

>>> tuple=(“a”,123,5,76)

>>> tuple

(‘a’, 123, 5, 76)

>>> del(tuple)

Basic tuple operations

The following are some of the basic operations on tuples.

>>> x=(“city”,2,”cool”,”bird”,345)

>>> x

(‘city’, 2, ‘cool’, ‘bird’, 345)

>>> “cool” in x

True

>>> “place” not in x

True

>>> 345 not in x

False

>>> for num in (1345,13425,242565):                         #Iteration

print(num)

 

 

1345

13425

242565

>>> for str in (‘hi’,’how are you’):

print(str)

 

 

hi

how are you

>>> (“Cool”,3)+(4,9,12)                                                   #Concatenation

(‘Cool’, 3, 4, 9, 12)

>>> “Google”*4                                                               #Repetition

‘GoogleGoogleGoogleGoogle’

Built-in functions of Tuple

Some of the tuples built-in functions are shown in below.

1.all( )

This returns true if all the elements belongs to the tuple including an empty tuple. For this the in keyword is used.

Ex

>>> T=(“Firefox”,34,1.101,”Chrome”,22,( ),”Opera”)

>>> T

(‘Firefox’, 34, 1.101, ‘Chrome’, 22, ( ), ‘Opera’)

>>> 34 in T

True

>>> ( ) in T

True

>>> (2) in T

False

>>> ‘Chrome’ in T

True

2.len( )

This displays the length of the tuple.

Ex

>>> b=(1,2,3,4,6)

>>> b

(1, 2, 3, 4, 6)

>>> len(b)

5

3.max( ) and min( )

This returns the maximum and minimum element in a tuple.

Ex

>>> y=(123,345,12.3,1.23)

>>> y

(123, 345, 12.3, 1.23)

>>> max(y)

345

>>> min(y)

1.23

4.sum( )

This returns the sum of all elements in a tuple.

Ex

>>> z=(1988,1993,2018)

>>> z

(1988, 1993, 2018)

>>> sum(z)

5999

Tuple methods

Python has only two tuple methods which are shown in the following.

1.count(n)

It returns the number of elements in a tuple.

Ex

>>> tuple=(“C”,”O”,”L”,”O”,”R”,”F”,”U”,”L”)

>>> tuple

(‘C’, ‘O’, ‘L’, ‘O’, ‘R’, ‘F’, ‘U’, ‘L’)

>>> tuple.count(“O”)

2

>>> tuple.count(“R”)

1

>>> tuple.count(“N”)

0

2.index(n)

It returns the index position of particular element in a tuple.

Ex

>>> tuple=(“C”,”O”,”L”,”O”,”R”,”F”,”U”,”L”)

>>> tuple.index(“L”)

2

>>> tuple.index(“U”)

6

>>> tuple.index(“O”)

1

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest