Learn Tuples In Python

Tuples are useful types in Python. In this article, we will discuss basics of tuples and how to use tuples in Python, with code samples.

Introduction

In the session, we are going to discuss tuples in Python. We will discuss the following topics:

  1. What are tuples?
  2. Add Tuples
  3. Update Tuples
  4. Delete Tuples
  5. Tuple Operations

If you want to learn the basics and environment setup for the Python, please go through the following link.

Create a New File

Go to Start >>, Python 3.6 - IDLE (Python 3.6 32bit) - Click OK.

Tuples In Python

To create a new Python file, go to New - New - Save the file on the local machine - Open the file through Python Shell.

Tuples In Python

Here, you can set the destination to save your file into local machine.

What are tuples?
Tuple is a sequence of immutable Python Library Objects. It’s like a list. The difference between tuples and lists are, a tuple cannot be modified like lists.

If you want to create a new tuple, you can use more than one comma-separated value. The value should be inside parenthesis.

Add Tuples

If you want to create a new tuple, you should follow these steps on your Python file.

Let’s discuss about the example to create the new tuple.

CODE

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information,

  1. >>> tuple1 = ('Application','Bytes','Continue',1000,1001);  
  2. >>> tuple2 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);  
  3. >>> tuple3 = "Z""Y""X";  
  4. >>>   

Look into the above example, you can assign the new string and integer value for the variable.

Tuples In Python

Like this we can create multiple tuples in Python file.

ACCESS SPECIFIC TUPLE VARIABLE

Use the following steps, if you want to access the specific tuple variable.

CODE

  1. >>> tuple1[0]  
  2. 'Application'  
  3. >>> tuple1[1]  
  4. 'Bytes'  
  5. >>> tuple1[2]  
  6. 'Continue'  
  7. >>>   

Tuples In Python

If you want to select the specific values between the tuple variable use the following steps,
  1. >>> tuple1  
  2. ('Application''Bytes''Continue'10001001)  
  3. >>> tuple1[2:3]  
  4. ('Continue',)  
  5. >>> tuple1[2:4]  
  6. ('Continue'1000)  
  7. >>>   

We have an option to select in between values from the tuple variables. Find the attached screenshot for reference.

Tuples In Python
Update Tuples

The tuples are immutable. So, you cannot change or modify any existing record from the element. If you want to change, you should create a new tuple in your application.

CODE

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information.

  1. >>> tup1 = 1000  
  2. >>> tup1  
  3. 1000  
  4. >>> tup1[0] = 50  
  5. Traceback (most recent call last):  
  6. File "<pyshell#2>", line 1in <module>  
  7. tup1[0] = 50  
  8. TypeError: 'int' object does not support item assignment  
  9. >>>   

In the above example, you can confirm that you cannot update the value for the tuples.

Tuples In Python

Update

If you want to use the existing records with the new variable, you can merge the variables.

CODE

  1. >>> tuple2  
  2. (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)  
  3. >>> tuple3  
  4. ('Z''Y''X')  
  5. >>> tupleNew = tuple2 + tuple3  
  6. >>> tupleNew  
  7. (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Z''Y''X')  
  8. >>>   

Now, the new tuple variable has the updated records.

Tuples In Python
Delete Tuples

We cannot remove the individual tuples element from the list. You should use ‘del’ keyword to remove entire tuples.

CODE

  1. >>> print (tuple1)    
  2. ('Application''Bytes''Continue'10001001)    
  3. >>> del tuple1    
  4. >>> tuple1    
  5. Traceback (most recent call last):  
  6. File "<pyshell#19>", line 1in <module>  
  7. tuple1  
  8. NameError: name 'tuple1' is not defined  
  9. >>>  
In the above example, you can see that we have deleted the existing variable name “tuple1” from the list.

Then, I tried to access it. It’s showing an error on the window.

Tuples In Python
Tuple Operations

Let’s discuss about the list of tuple operations that we can use in Python.

  • The tuples are respond to the arithmetic operators like +, -, * and /.
  • We  are able to do the concatenation and repetition too.
  • Tuples will respond to all the strings from the declaration.
ExpressionsResultsDescription
len((1, 2, 3, 4))4Length
(1, 2, 3) + (4, 5, 6) + (7,8)(1, 2, 3, 4, 5, 6, 7, 8)Concatenation
('Welcome!',) * 4('Welcome!', 'Welcome!', 'Welcome!', 'Welcome!')Repetition
5 in (1, 2, 3,4 ,5)TRUEMembership
for x in (1, 2, 3): print x,1 2 3Iteration

Hope this helps. Please give me the feedback, if you need further assistance.

Thank you.