Introduction
In the session, we are going to discuss tuples in Python. We will discuss the following topics:
- What are tuples?
- Add Tuples
- Update Tuples
- Delete Tuples
- 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.
To create a new Python file, go to New - New - Save the file on the local machine - Open the file through Python Shell.
Here, you can set the destination to save your file into the local machine.
What are tuples?
A 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 the parenthesis.
Add Tuples
If you want to create a new tuple, you should follow these steps on your Python file.
Let’s discuss 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,
- >>> tuple1 = ('Application','Bytes','Continue',1000,1001);
- >>> tuple2 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
- >>> tuple3 = "Z", "Y", "X";
- >>>
Look into the above example, you can assign the new string and integer value for the variable.
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
- >>> tuple1[0]
- 'Application'
- >>> tuple1[1]
- 'Bytes'
- >>> tuple1[2]
- 'Continue'
- >>>
If you want to select the specific values between the tuple variable use the following steps,
- >>> tuple1
- ('Application', 'Bytes', 'Continue', 1000, 1001)
- >>> tuple1[2:3]
- ('Continue',)
- >>> tuple1[2:4]
- ('Continue', 1000)
- >>>
We have an option to select in between values from the tuple variables. Find the attached screenshot for reference.
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.
- >>> tup1 = 1000
- >>> tup1
- 1000
- >>> tup1[0] = 50
- Traceback (most recent call last):
- File "<pyshell#2>", line 1, in <module>
- tup1[0] = 50
- TypeError: 'int' object does not support item assignment
- >>>
In the above example, you can confirm that you cannot update the value for the tuples.
Update
If you want to use the existing records with the new variable, you can merge the variables.
CODE
- >>> tuple2
- (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
- >>> tuple3
- ('Z', 'Y', 'X')
- >>> tupleNew = tuple2 + tuple3
- >>> tupleNew
- (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Z', 'Y', 'X')
- >>>
Now, the new tuple variable has the updated records.
Delete Tuples
We cannot remove the individual tuples element from the list. You should use ‘del’ keyword to remove entire tuples.
CODE
- >>> print (tuple1)
- ('Application', 'Bytes', 'Continue', 1000, 1001)
- >>> del tuple1
- >>> tuple1
- Traceback (most recent call last):
- File "<pyshell#19>", line 1, in <module>
- tuple1
- NameError: name 'tuple1' is not defined
- >>>
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.
Tuple Operations
Let’s discuss the list of tuple operations that we can use in Python.
- The tuples are responded 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.
Expressions |
Results |
Description |
len((1, 2, 3, 4)) |
4 |
Length |
(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) |
TRUE |
Membership |
for x in (1, 2, 3): print x, |
1 2 3 |
Iteration |
Summary
Hope this helps. Please give me the feedback, if you need further assistance.
Thank you.