Code in Python
Machine Learning and Deep Learning Blogs
search

How to work with Tuples in Python

A tuple is a sequence data type that is similar to list. Tuples are immutable lists. It contains a set of numbers or characters or a combination of both separated by commas and enclosed within parenthesis (()). Lists are enclosed in square brackets ([]) whereas tuple is within parenthesis (()).

The one thing that separates tuples from lists is that elements and size of a list can be changed whereas tuple cannot be updated. Tuples can be considered as read-only lists.

Example:

tuple1 = ('python', 45, 56, 12, 'learning')
tuple2 = ('code', 3, 4, 7, 'program')
print(tuple1)
print(tuple1[0])
print(tuple2[2:4])
print(tuple1[2:])
print(tuple1*2)
print(tuple1+tuple2)

Output:

('python', 45, 56, 12, 'learning')
python
(4, 7)
(56, 12, 'learning')
('python', 45, 56, 12, 'learning', 'python', 45, 56, 12, 'learning')
('python', 45, 56, 12, 'learning', 'code', 3, 4, 7, 'program')

In the above example, if you try to change an element of the tuple1, for e.g., tuple1[0] = 100, it would show an error message because elements and size of tuples cannot be changed. Whereas, in the case of lists, you can add as many new elements as you want.

There are various operations that can be done on tuple.

a) You can print out any element of the tuple by just mentioning the index of the element. Tuple indices start with 0. All you have to do is, enter the index of the element in square brackets followed by the variable referring the tuple and it will print out the element.

b) You can print out a particular part of the tuple just by mentioning the starting index and the ending index in square brackets separated by a colon (:) followed by the variable referring tuple.

c) If you want to print out a particular part of tuple starting from an index and going until the end if the tuple, then you don’t have to mention the ending index in square brackets. Leave that part blank and the interpreter will automatically understand that you want to print the tuple till the end from a particular index.
Same goes for the case where you want to print the part starting from the beginning and going to a particular index. All you have to do is leave the starting index part blank in square brackets and mention the ending index.

d) Tuples can be multiplied by a number. Here, by multiplying, we mean that the tuple will print out itself as many times as the number by which it is multiplied.

e) You can add two tuples. Which you add two or more tuples, the elements of the tuples get arranged in a single tuple.

REMEMBER: Adding tuples doesn’t mean that the elements of the tuples will get added and output will be the addition of the elements of the two tuples. No, it just combines the elements in a single tuple.

So why do we need tuples?

Tuple is not used frequently as lists in practice, but their immutability is the whole point. If you use lists to enter a collection of elements, it can be changed anywhere; if you use a tuple, it cannot. That is, tuples provide a sort of integrity constraint that is convenient for larger programs.

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)

4 Comments

  1. I love it when individuɑls get togethеr and share thoughts.
    Great website, continue the good work!

  2. I’m really enjoүing the design and layⲟut of youг blog.
    It’s a very easy on the eyes whicһ makes it much more enjoyaƄle for mе to come
    һere and viѕit more often. Did you hire out a desіgner to
    ϲreate your theme? Outstanding work!

  3. Thank you for this informative read, I have shared it on Facebook.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python