For simple understanding, “tuple” is a set of data. So, it is a set, like list or dictionary, but what makes tuples tuples? Before we go into tuples, let’s grab a tuple first.

## Create a Tuple

To make a tuple, you need a bag of values enclosed in parentheses.

t = (1, "2", "abc", 16, "SCKT")


In case there is only one value, this becomes another type, depending on what value is it.

>>>tup = (16)
>>>type(tup)
<class 'int'>
>>>tup = ("SC")
>>>type(tup)
<class 'str'>


A “,” is needed to avoid this situation:

>>>tup = (16,)
>>>type(tup)
<class 'tuple'>


Another way to build a tuple is by using a built-in function, but I hardly use it because it’s limited in some ways.

>>>t=tuple()
>>>t
()
>>>t=('harry')
>>>t
('h', 'a', 'r', 'r', 'y')


Now a tuple is coming to life. Let’s see what we can do with it.

## Some stricks

Let’s try some operation we’ve been using in other objects so far.

• index
>>> t = ('s','o','n','c','o')
>>> t[1]
'o'

• slicing
>>> t = ('s','o','n','c','o')
>>> t[1:4]
('o', 'n', 'c')

• comparison
>>> (0,1,3)<(0,3,2)
True
>>> (1,2,3)<(3,2,1)
True
>>> ('S','C')<('K','T')
False


Can you find its logic?

Python will compare two tuples in pairs. It goes from the first element. If they are equal, then it will go on to the next pair. This process continues until Python finds something that differs. Then it will give the result and the elements after that are totally ignored.

Quick question, what result will this line of code give?

>>>(0,0,0)<(0,0,0,0,1)


But there is another way to get around with it. You can use another tuple to replace the part you want.

>>> t = ('S',) + t[1:]
>>> t
('S', 'o', 'n', 'c', 'o')


## Tuple Assignment

If you are too lazy to make several assignments, then tuple is for you (I think there are more subtle uses for this feature, but for now, I only think of that).

>>> (a,b,c) = (1,2,'a')
>>> c
'a'


Remember that the left-hand side of the assignment is always the variables, regardless they are in a tuple or not.

Now that tuples are immutable, but it doesn’t mean that you can’t re-assign.

>>> (a,b,c) = (1,'a','b')
>>> c
'b'


The number of elements in both sides should be the same, otherwise you would get a traceback.

>>> (a,b) = (1,2,3)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
(a,b) = (1,2,3)
ValueError: too many values to unpack (expected 2)


The number of variables and the number of values should be the same, but sometimes, the right-hand side comes in a different form.

>>>address = kt@coding101.com
>>>name
'kt'
>>>domain
'coding101.com'


## Sorting

Can you sort values in a tuple? The answer is no. But you can sort a list of tuples, which is kinda useful when you want to sort the keys in dictionary.

It takes time to explain all the ways you can take advantage of tuples to use upon dictionaries, so I’ll get back to it later. But first, let’s take a look on how to sort items in dictionary by keys.

>>>d = {'SC':16, 'KT':1608, 'An':2608}
>>>d.items()
dict_items([('SC', 16), ('KT', 1608), ('An', 2608)])
>>>sorted(d.items())
[('An', 2608), ('KT', 1608), ('SC', 16)]


Can you find any tuple in those lines of code? Well, we’ll see later.

## Conclusion

Tuple is like a limited version of list. It’s hard to use, but not totally useless. If you know the way, you will find that tuple is actually a fun guy who makes the situation less unpleasant.

If you want to know more about that fun guy, let’s check my next post. See ya!