tuples-in-python

Tuples In Python

Tuples

Like Eren Yeager let’s move forward. Next up in Learn Python Tutorials is Tuple. A tuple is a data type that is enclosed within parenthesis i.e., (). But wait for a minute look at this example:

>>>e_anime = "Erased"
>>>type(e_anime)
<class 'str'>

>>>e_anime = "Erased",
>>>type(e_anime)
<class 'tuple'>

>>>episode = 12
>>>type(episode)
<class 'int'>

>>>episode = 12,
>>>type(episode)
<class 'tuple'>

I haven’t given any parenthesis, but still, we have got Tuple as a data type there. The actual definition of Tuple is “The collection of values that are separated by a comma, and are enclosed with parenthesis[which is optional]”. I hope that makes sense.

Let us Take a Good Look at Tuple

e_anime = "Erased","Evangelion"
e_anime
('Erased', 'Evangelion')
#notice the output is enclosed within parenthesis, that means it is a tuple.

ten_tables = tuple(range(0,101,10))
ten_tables
(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

Where Tuple is used more?

def check(a,b):
    return a,b

ans = check(10,20)
type(ans)

<class 'tuple'>
def swap(a,b):
    print(“Before:",a,b)
    a,b=b,a
    return a,b

ans = swap(10,15)
print(“After:",ans[0],ans[1])
print(type(ans))
Output:
Before: 10 15
After: 15 10

Tuple Operations

Ah man, come on already. We have already enough with the List operation in the previous article. But don’t worry you already know how to use these attributes. It’s no new topic.

1. count()

num = tuple(range(10,20))
num1 = (10,12,17,20)
num = num+num1
num
(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 12, 17, 20)
num.count(11)
1
num.count(12)
2
num.count(24)
0

2. index()

>>> a_to_e_anime_name = ("Attack On Titan","Bungou Stray Dogs","Code Geass","Death Note","Erased")
>>> a_to_e_anime_name
('Attack On Titan', 'Bungou Stray Dogs', 'Code Geass', 'Death Note', 'Erased')
>>> len(a_to_e_anime_name)
5
>>> a_to_e_anime_name.index("Code Geass")
2

See I told you, it’s no new topic.

Tuple Slicing

>>> count_n_slice = tuple(range(0,20))
>>> count_n_slice
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
>>> count_n_slice[:10]
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> count_n_slice[::-1]
(19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
>>> count_n_slice[::-3]
(19, 16, 13, 10, 7, 4, 1)
>>> count_n_slice[1:2:3]
(1,)
>>> count_n_slice[1:15:3]
(1, 4, 7, 10, 13)

Tuple slicing is exactly the same as list slicing. Slicing usually requires two index values, i.e., the start and end value. You are also free to omit the start or end value and yet slice the tuple. For example when you need starting 5 values, no need to specify the start value instead you can omit and specify the end value as 5, which will give us the first 5 elements.

Tuple is Immutable

A tuple that is immutable once declared can’t be modified. This is where Tuple is different from List, we will discuss this later. But for now, let us see some examples of Tuple being immutable.

straw_hats_members = ("Luffy","Zoro","Nami","Usopp","Sanji")
straw_hats_members[0]
'Luffy'
straw_hats_members[0]="Shanks"
TypeError: 'tuple' object does not support item assignment

Since Tuple is immutable we can’t insert a new element once we have assigned a tuple. In addition, you can’t even modify the element once

Tuple to String and List and Vice Versa

You can perform type conversion of Tuple to List/String and vice versa. The syntax is the same as that of Type conversion which you know all along.

First, let us convert Tuple to String and Vice Versa.

>>> anime_2021 = ("Blue Period","Takt.op Destiny","Komi can't communicate","Platinum End","Mieruko-chan")
>>> type(anime_2021)
<class 'tuple'>
>>> len(font)
5

>>> anime_2021 = str(anime_2021)
>>> len(anime_2021)
92
>>> type(anime_2021)
<class 'str'>
>>> name = "Komi-san"
>>> type(name)
<class 'str'>

>>> len(name)
8
>>> name = tuple(name)
>>> type(name)
<class 'tuple'>

>>> len(name)
8
>>> print(name)
('K', 'o', 'm', 'i', '-', 's', 'a', 'n')

While converting Tuple to String, every element including the brackets is considered as one element. Whereas while converting string to a tuple, the length remains the same, treating every string index as one tuple element.

Tuple to List and Vice Versa

>>> font = ("arial","carlito","Open sans","Times new roman")
>>> type(font)
<class 'tuple'>

>>> len(font)
4
>>> font = list(font)
>>> type(font)
<class 'list'>

>>> len(font)
4
>>> print(font)
['arial', 'carlito', 'Open sans', 'Times new roman']

Well converting Tuple to List and Vice versa doesn’t affect the length of elements. But String to Tuple and vice versa is a different story.

Why Should We Use Tuple When We Have List?

Tuple does exactly the same task as that of the List. Both can store the values of different Data types. Then Why Tuple? Shouldn’t we be satisfied with the List?

>>> tuple_example = ("Python",30,"days")
>>> type(tuple_example)
<class 'tuple'>

>>> print(tuple_example)
('Python', 30, 'days')
>>> list_example = ["Python",30,"Days"]
>>> type(list_example)
<class 'list'>

>>> list_example[1]=40
>>> list_example
['Python', 40, 'Days']

>>> tuple_example[1] = 40
TypeError: 'tuple' object does not support item assignment

With this example, you know where I am getting at. Obviously, Tuple does that operation, which is already available in the List. What if you don’t need your elements to be changed. I mean think about this a moment, List is mutable and the list elements can be modified, if you have to complete Python in 30 days and you store this in a List, you can change that 30 into any number. But you do need that modification instead use Tuple, where the elements are inevitable. Now you have an idea about List and Tuple, thus we will conclude here.

Conclusion

You can try 30 Days Python Programming by solving amazing code daily. Anime Vyuh Github Organization has a public repo, which will help you with #30DaysOfPython.

Check Out The Previous Tutorials Part On Learn Python.

If you like our content, support us by Buying a Coffee.