Introduction
Python tuple is almost similar to Python Lists. It's an ordered collection of comma-separated values between two parentheses. The major difference between tuples and lists is, tuples are immutable(the internal state of an object can not be changed once it's created, at runtime) but lists are not.
In this tutorial, you'll learn about Python Tuple and all its operations.
Example
('Python', 'PySeek', 'Tutorial', 2022)
Create a Tuple
Creation of a Python Tuple is very easy. See the example below. There 'Lang_Tuple' is a Tuple variable.
Lang_Tuple = ("Python", "Basic Tutorial", "Tuple", 2022)
Tuple without parentheses
You can define a Python Tuple this way too.
Lang_Tuple = "Python", "Basic Tutorial", "Tuple", 2022
Access elements from a Tuple
We learned how to define a Tuple in Python but how do we access the data elements from there in our code? Let's learn it.
In the example below, I've defined a Python Tuple called 'language' and from there implemented various methods to access the data element in various ways.
languages = ("Python", "C++", "C#", "Java", "PHP", "HTML5")
# It will print all the values
print(languages)
# It will print the values from index no. 1.
print(languages[1])
# It will print the last item from the tuple.
print(languages[-1])
# Starting and ending range is declared here. It will print
# all the values from index no. 1 to 3.
print(languages[1:4])
# It will print all the values from the 4th rightmost index
# to the second rightmost index.
print(languages[-4:-1])
Output
('Python', 'C++', 'C#', 'Java', 'PHP', 'HTML5')
C++
HTML5
('C++', 'C#', 'Java')
('C#', 'Java', 'PHP')
How to append an element to a Python Tuple?
Since Python tuples are immutable, we cannot add or append any elements to them at runtime. But there is a solution to this. These steps will indicate how to get rid of this situation.
Steps
1. Covert the tuple to a Python List.
2. Perform the append operation on that list to add the new element (Learn it from here).
3. Re-convert the list to a tuple again.
Don't worry it's much easier than you think. The below code will help you.
tom = ("Ethan Hunt", "US", 58)
# Converting the tuple to a list
cruise = list(tom)
cruise.append("My favorite hero")
# Converting the list to a tuple again
tom = tuple(cruise)
print(tom)
Output
('Ethan Hunt', 'US', 58, 'My favorite hero')
Tuple Concatenation in python
We can't change or update any values of a tuple at runtime because tuples are immutable. But, we can add more items to a tuple using the concatenation method. Let's see the example below.
tom = ("New York", "US")
movies = ("The Mummy", "Jack Reacher", "Mission Impossible")
# Concatenate two tuples
tom = tom + movies
# Concatenate a single item to the tuple
tom = tom + (3,)
print(tom)
# It will add all the values up to the 4th index
# from the variable: tom
tom_cruise = ("Tom Cruise",) + tom[:4]
print(tom_cruise)
Output
('New York', 'US', 'The Mummy', 'Jack Reacher', 'Mission Impossible', 3)
('Tom Cruise', 'New York', 'US', 'The Mummy', 'Jack Reacher')
Remove elements from a tuple
To remove one or more elements from a tuple you need to follow the same steps as we did during the append operation. Here is the code for you.
tom = ("Tom Cruise", "US", 1962)
# Converting the tuple to a list
tom_cruise = list(tom)
# removing an element
tom_cruise.remove(1962)
# again re-converting the list to a tuple
tom = tuple(tom_cruise)
print(tom)
Output
('Tom Cruise', 'US')
Delete the entire tuple
Deleting an entire tuple is much easier than removing items from it. There is a method for this, called 'del' which can do the job easily.
del tom
Learn Next: Dictionary in Python
Summary
In this tutorial, you've learned what are Python Tuples and various related operations. For example, how to define a tuple, access elements from a tuple, add and remove operations, concatenate tuples, delete a tuple, etc. We learned the whole topic by covering several programming examples.
For any query related to this topic, just leave your comment below; You will get an immediate response.
Thanks for reading!💙
PySeek