Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/ekhaliya/public_html/topcse/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: Cannot modify header information - headers already sent by (output started at /home/ekhaliya/public_html/topcse/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php:340) in /home/ekhaliya/public_html/topcse/wp-content/plugins/sg-cachepress/core/Supercacher/Supercacher_Helper.php on line 77
tuple and its introduction : using tutorial topcse.com - Top CSE



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 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


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


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


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


>>>(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.


>>>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


  • 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


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


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



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



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



How to delete tuple 

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


>>>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.




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 .





How to find the maximum and minimum item in the python 

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







How to find Length of a Tuple in python

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


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



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(tuple(‘p’,’i’,’h’,’u’))             #converting some elements into a tupple


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




%d bloggers like this: