Tuples in Python

Swadhin Ray
CERTIFIED EXPERT
Published:
Edited by: Andrew Leniart
In this article, we will see what are Tuple and how we use them in Python programming.

Tuples:

 

Tuples are one of the built-in types of sequences out of six and just like lists. There is another article published on how to use Lists in Python. We want to store the values for the rest of our program then we can use Tuples.  Now for an example, if we want to store the name of some category of any Test plan when comes to compliance like "Audit, SOX, Risk Management or ORM" for continuing some program. 


First, we need to declare a tuple and give it a name, the initial values will be used within parentheses "()". All the values will be separated by a comma

 

my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category
print my_category[0]
print my_category[2]

The result from the above code will be as below :


  ('Audit', 'SOX', 'Risk Management', 'ORM')
   Audit 
   Risk Management

Sometimes we need to remove or delete the entire Tuple, so to delete the entire tuple we can use the command "del", below is the example on how to use del or delete then entire tuple. 


my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category
#print my_category[0]
#print my_category[2]
del my_category
print my_category


  ('Audit', 'SOX', 'Risk Management', 'ORM')
Traceback (most recent call last):  File "python", line 7, in <module> NameError: name 'my_category' is not defined
   

We can see from the above result we are getting that the tuple which was created is not defined because after creating it we immediately deleted the entire tuple. 


So if we want to find or verify if a particular item is present in a tuple then we can use "in". In the example below, we are trying to see if SOX is present in the sample tuple created or not. 


     my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
      print my_category
   ('Audit', 'SOX', 'Risk Management', 'ORM')
   'SOX' in my_category
   => True   

We can see that the result is "True" if the item is present in a tuple. When an item is not present then the result will be false. 


  'Compliance' in my_category
   => False   

 

Arithmetic Operators in Tuples. 


Using concatenation in Tuple.


To concatenate the tuples we need to use plus operator "+".  The below example shows how to use concatenation in tuples. 

my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category
print (my_category+ ('Compliance','Others'))



 ('Audit', 'SOX', 'Risk Management', 'ORM') ('Audit', 'SOX', 'Risk Management', 'ORM', 'Compliance', 'Others')
   


Using Multiplication Operator in Tuple. 


If in case we need to duplicate the values in a tuple then we can use multiplication operator to achieve. Below example will show how to use multiplication operator in a tuple. 


my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category*2
print ("# Below is the output of the actual tuple")
print my_category


 ('Audit', 'SOX', 'Risk Management', 'ORM', 'Audit', 'SOX', 'Risk Management', 'ORM') 

# Below is the output of the actual tuple 
 ('Audit', 'SOX', 'Risk Management', 'ORM')
   


We can see the using addition or multiplication do not modify the actual tuple. 


Assignments are not possible in tuples how we usually do it on a list in Python.  The example below will show how we use the assignment in List and the error on applying the same rule on Tuple.  


list =[1,2,3,4]
print list
print list[0]
list[0]=9
print list


[1, 2, 3, 4] 
1
[9, 2, 3, 4]
   

In the above example, we can see we have changed the value from 1 to 9 by assigning it. Now let us try to assign zero back to the list, for doing this I am converting the list into a tuple and will try to assign. 


list =[1,2,3,4]
print ('Below is the output of a list ')
print list
print list[0]
list[0]=9
print ('Below is the output of a list after assigning a value 9')
print list
print ('Below is the output of a tuple by converting a list into a tuple ')
print tuple(list)
print ('Get the first value from the tuple')
print tuple(list)[0]
print ('Assign back 0 in place of 9 in a Tuple')
tuple(list)[0]=0
print tuple(list)

The result from the above code. 


Below is the output of a list 
[1, 2, 3, 4]
1 
Below is the output of a list after assigning a value 9 
[9, 2, 3, 4] 
Below is the output of a tuple by converting a list into a tuple 
(9, 2, 3, 4) 
Get the first value from the tuple
9
Assign back 0 in place of 9 in a Tuple
Traceback (most recent call last):  File "python", line 13, in <module> TypeError: 'tuple' object does not support item assignment
   

 We can see the error saying 'tuple' object does not support item assignment


Now we know how to use Tuple in Python programming. This is a basic explanation on how to use Tuple in Python programming language


Thank you for reading my article, please feel free to leave me some feedback or to suggest any future topics.

I'll be looking forward to hearing from you – Swadhin Ray (Sloba)


For more information about me, please check out my Experts Exchange Profile page.



0
1,517 Views
Swadhin Ray
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.