INTRODUCTION TO TUPLES

 

In this tutorial we will learn about tuple , its creation  and some  operation that can be performed on tuples.python tutorials,In the previous tutorial we have learnt about list , how to create it ,and the operation that can be performed on it.

Tuples 

tuples are  the sequence of comma-separated values enclosed in parentheses . The parentheses are optional though. tuples are similar to list but the only difference between  list and tupple is that in list we use square brackets but in tuples we use parenthesis ,which is optional for us to put parenthesis or not.

Python tuples contain ordered sequence of items that can be of different data types.Tuples are immutable, which means a tupple cannot be modified once it is created.

Creation of tuples in python

creating a Python tupple is as simple as  we did in the list by just putting comma-separated values or items inside parentheses “( )”. It can be created without  using parentheses also .

The comma-separated values inside parentheses can be of any type , not necessarily of the same kind.

>>>tup=( )                                                                #creation of empty tupple

>>>tupp=tuple( )                                                     #creation using built-in function tupple

>>>tu=(1,)                                                              #creation of tupple using single value

>>>t=(1,2,3,4)                                                        #creation of tupple using similar data type

>>>a=1,2,3,4                                                         #creation of tupple using without parenthesis

>>>b=(1,2,”u”,”z”)                                               #creation of tupples using dis-similar datatype

>>>c=(1,2,(1,2,3,),[4,5])                                        # creation of  tupple using list and tuple

>>>x=(1,2,”g”,2.0)                                              #creation of tupples using integer,float,characters values

>>>print(t)                                                      #printing the values of tupple named as t

>>>(1,2,3,4)

>>>print(a)                                                   #printing value  of tupple named as a

>>>(1,2,3,4)

>>>print(b)                                                 #printing value of tupple  named as b

>>>(1,2,”u”,”z”)

Concatenation of two tuples in python

Concatenation of two tupples is done using ” +”, it is as same as we have done in list.

>>>a=(1,2,3)                 #creation of a tupple  named as a

>>>b=(4,5)                   #creation of tupple named as  b

>>>c=a+b                     #concatenation of two tupples and storing them in another variable c

>>>print(c)

>>>(1,2,3,4,5)           #printing the value of c

How to access elements from tuple in python

we can access tuples as same as the way we have accessed the elements in the list .

  • Indexing :-we use indexing operator [ ] to access elements . Index starts from 0.

>>>a=(1,2,3,4)

>>>print(a[0])              #trying to access value at the index position zero

>>>1                            #value at the index position zero

>>>print(a[-1])           #way to print the last value of a tupple

>>>4

  • Slicing :-In python ,here slicing is done  using ‘[ ]’ and ‘ : ‘.

>>>a=(1,2,3,4,5,6,7,8,9)          # creation of the tuple named as a

>>>x=a[2:5]

>>>print(x)                          #printing the whole tuple x

>>>(3,4,5)

>>>y=a[:3]                            #accessing  element from 0th index to index 3rd excluded

>>>print(y)

>>>(1,2,3)

>>z=a[5:]                               #accessing from 5th element to the end

>>>print(z)

>>>(6,7,8,9)

>>>u=a[:]                                #accessing the whole tupple

>>>print(u)

>>>(1,2,3,4,5,6,7,8,9)

How to delete tuple 

we can delete a whole  tupple using del() function , but we cannot delete a particular element .

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

>>>del(a)                             # deleted

>>>print(a)                                  #shows nothing

How to reverse the order of items in a tuple in python

we can reverse the tuple in the same way as we have done for lists.To reverse the order of elements in the tuple, Python has an inbuilt function called tuple-name.reverse() , which reverses the order of appearance of elements in the tupple.

>>>a=(1,2,3,4,5,6)

>>>print(a.reverse())

>>>(6,5,4,3,2,1)

To count number of occurrences of any element  in tuple 

To count the appearance of an item , Python has an inbuilt function tupple-name.count(item-name), which returns the number of times the item appears .

>>>a=(1,2,1,1,1,3)

>>>b=a.count(1)

>>>print(b)

>>4

How to find the maximum and minimum item in the python 

to find the maximum and minimum element we have max() and min() function.

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

>>>b=max(a)

>>>print(b)

>>>c=min(a)

>>>print(c)

>>>1

How to find Length of a Tuple in python

in python to find the length of a tupple we have len() function .

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

>>>b=len(a)               # to find the length of a tupple

>>>print(b)

>>>5

How to a Convert list to a Tuple 

we can convert a list into tuple using tuple() .

>>>a=[1,2,3,4]                 #creation of  a new list

>>>b=tuple(a)                #converting a list into a tupple

>>>print(b)

>>>(1,2,3,4)

>>>print(tuple(‘p’,’i’,’h’,’u’))             #converting some elements into a tupple

>>>(‘p’,’i’,’h’,’u’)

In this tutorial we have learnt about what do you mean by a tupple , how can we create a tupple in python programming language, how we are going to print a tupple, how can we access a tupple in seen in this tutorial , how can we delete a tupple in python,how can we convert a list into a tupple is seen here.

tuple and list differences

https://docs.python.org

 

 

%d bloggers like this: