Learn about Python Tuple

Introduction

Python tuple is almost similar to the Python Lists. It's an ordered collection of comma-separated items between two parentheses. The major difference between tuple and list is, tuples are immutable(the internal state of an object can not be changed once it's created) but lists are not. In this section, you'll learn about Python Tuple and its operations.

Example of Tuple: ('Python', 'PySeek', 'Tutorial', 2022)

Create a Tuple

Tpl = ("Python", "Basic Tutorial", "Tuple", 2022)

here 'Tpl' is a tuple variable.

Tuple without parentheses

You can create a python tuple this way too.

PySeek = "Python", "Basic Tutorial", "Tuple", 2022

Access elements from a Tuple

We have successfully created a Python tuple but how do we access the data elements from there in our code? The below code will help you.


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 to a tuple in python?

Since Python tuples are immutable, we cannot add or append any elements to them at runtime. But there is a solution for this. Follow these steps below:

Steps

1. Covert the tuple to a python list

2. Perform the append operation on that list (Learn it from here)

3. Again re-convert the list to a tuple

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 how.


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 in the blink of an eye.


del tom

Summary

In this tutorial, you've learned what are Python Tuples and its operations. For example, how to define a tuple, access elements from a tuple, add and remove operations, concatenation of tuples, deleting a tuple, etc. We did various examples to

For any query related to this topic, just leave your comment below; You will get an immediate response.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post