
Hello Techies,
This blog is about “Tuple in Python”. A tuple is a collection of ordered and immutable objects. So in this blog, we will learn about the usage of the Tuple in Python.
Check out my previous blog on built-in datatypes in Python.
Let’s start and check, How to use Tuple in Python step by step.
Below are the Key Points that we are covering in this blog:
- Python Tuple (Introduction)
- Create Tuple in Python
- Index in Tuple Python
- Accessing Tuple Element
- Modifying Tuple Element
- Delete or Remove elements from Tuple
- Python Tuple Slice
- Python Tuple Methods
Table of Contents
#1. Python Tuple(Introduction)
- A Tuple is same as list in Python. The difference between the two is that once you assign an element to a tuple, you can’t change it, which means the tuples are immutable, whereas we can change the elements of the list.
- Tuples can have many objects and they can be of different types (such as integers, floats, lists, strings, etc.)
- The tuple is represented using a square bracket ().
- Example: data = (1, -89, “test”, “TechPlusLifeStyle”)
#2. Create Tuple in Python
A tuple is created by placing all the elements separated by commas in parentheses (). Brackets are optional, however, it is good practice to use them.
Syntax
tuple_name = (item1, item2, item3, ...)
Example
data = (1, -89, "test", "TechPlusLifeStyle")
type(data)
data1 = 1, -89, "test", "TechPlusLifeStyle"
type(data1)
Output
<class 'tuple'>
<class 'tuple'>
It is not enough to have one element in parentheses. We need a trailing comma to show that, it’s a tuple.
data = ("TechPlusLifeStyle")
type(data)
data1 = "TechPlusLifeStyle"
type(data1)
data3 = ("TechPlusLifeStyle",)
type(data3)
data4 = "TechPlusLifeStyle",
type(data4)
Output
<class 'str'>
<class 'str'>
<class 'tuple'>
<class 'tuple'>
#3. Index in Tuple Python
An index represents the position number of a tuple’s elements. The index starts at 0 onwards and is written in square braces.
Example:- data = (1, -89, “test”, “TechPlusLifeStyle”)
This index shows positive and negative indices in 2 ways. The positive index starts from the 0 positions while the negative index -1 refers to the last element, -2 refers to the second last element, and so on.
Positive Index
Index | Data |
---|---|
[0] | 1 |
[1] | -89 |
[2] | test |
[3] | TechPlusLifeStyle |
Negative Index
Index | Data |
---|---|
[-1] | TechPlusLifeStyle |
[-2] | test |
[-3] | -89 |
[-4] | 1 |
#4. Accessing Tuple Element
There are several ways in which we can access elements in Tuple. Let’s check the above example for Positive and Negative Indexing.
Example for Positive indexing
data = (1, -89, "test", "TechPlusLifeStyle")
print(data[0])
print(data[1])
print(data[2])
print(data[3])
Output
1
-89
test
TechPlusLifeStyle
Example for Negative indexing
data = (1, -89, "test", "TechPlusLifeStyle")
print(data[-4])
print(data[-3])
print(data[-2])
print(data[-1])
Output
1
-89
test
TechPlusLifeStyle
#5. Add or Update Tuple Element
Tuples are immutable so we can’t modify Tuple’s elements like a list data type.
Tuple’s elements cannot be changed once they are assigned, but if the element is a mutable like list data type, then its nested items can be changed.
data = (1, -89, "test", "TechPlusLifeStyle", [5, 7, 6])
data[4][1] = 9
print(data)
Output
[1, -89, "test",(1, -89, 'test', 'TechPlusLifeStyle', [5, 9, 6])
We can use the + operator to combine two different tuples. We can also repeat the elements in the tuple for a given number using the * operator.
Example
data = (1, -89, "test", "TechPlusLifeStyle")
data1 = (5, 7, "test2")
print(data + data1)
data3 = ("test", "test1")
print(data3 * 4)
Output
(1, -89, 'test', 'TechPlusLifeStyle', 5, 7, 'test2')
('test', 'test1', 'test', 'test1', 'test', 'test1', 'test', 'test1')
#6. Delete or Remove elements from Tuple
Deleting a tuple completely, however, is possible using the del keyword, but we can’t delete elements from Tuple, because Tuple is immutable so we can’t change Tuple’s elements.
data = (1, 2, 3, 4)
del data
print(data)
Output
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'data' is not defined
Interview question Q: Difference between Tuple and List? Ans: 1) List objects are mutable and Tuple objects are immutable. 2) Tuples are faster than the list. Q: Why is tuple faster than the list in Python? Ans: Tuple is stored in a single block of memory. The list is stored in two blocks of memory. That's why Tuple is faster than Python's list.
#7. Python Tuple Slice
Slice Operator: Using (colon) we can enter the range of items in the Tuple.
data = (1, 2, 3, 4, 5, 6, 7, 8)
# elements 2nd to 5th
print(data [1:5])
# elements beginning to 6th
print(data [:-3])
# elements 3rd to end
print(data [2:])
# elements beginning to end
print(data [:])
Output
(2, 3, 4, 5) # output of data[1:5]
(1, 2, 3, 4, 5, 6) # output of data[:-3]
(3, 4, 5, 6, 7, 8) # output of data[2:]
(1, 2, 3, 4, 5, 6, 7, 8) # output of data[:]
#8. Python Tuple Methods
The methods available with the Tuple objects in Python programming are in the table below.
Method Name | Description |
---|---|
index() | This method returns the index of the first matching item |
count() | Calculate the number of items passed as arguments and return the count |
Some examples of Python methods
data = (4, 8, 1, 6, 8)
# index() method
print(data.index(8))
# count() method
print(data.count(8))
Output
1
2
I hope you understand the use of “Tuple in Python”. Still, you have any doubts please comment below.
Check out the official site for more details on “Tuple in Python”.
