# Python Tuples from Scratch !!!¶

## Let us understand the basic concepts of python tuples.¶

Before starting I would like to tell you, I will write this tutorial in a question-and-answer format because it will be helpful especially during interviews. And throughout the tutorial, I will work only on a single example to maintain the uniformity.

## 1) What is a tuple and why is it used?¶

A tuple is a data structure which is used store heterogeneous data meaning data of different types. A tuple is used to group together related data such as a dog name, height, weight, breed, and color (I love dogs). A tuple comprises several values separated by commas.

## 2) How to create a tuple?¶

A tuple can be created by enclosing the values with a pair of brackets and separated by commas for instance:

Let us create a tuple called "tuple", and put in some values including both string and integer data this can be done as shown below:

In [0]:
# Creating a tuple and storing the values
tuple = ("Today's", "Date", "Is", 15, "August", 2019)
print(tuple)

("Today's", 'Date', 'Is', 15, 'August', 2019)

In [0]:
print(type(tuple))

<class 'tuple'>


As seen above the tuple can hold heterogeneous data (Hence proved :))

## 3) How do we access the values of a tuple?¶

Indexing can do this. The values of tuples can be accessed by their index to get the value. To do this, all you have to do is just place the number (index value) inside the pair of square brackets along with the name of the tuple.

In [0]:
# Accessing the values of a tuple
print(tuple[2])

Is

In [0]:
print(tuple[5])

2019


## 4) How to nest or combine two tuples?¶

Just placing the old tuple next to the newly created tuple separated by a comma can do nesting of two or more tuples.

In [0]:
# Nesting two tuples as one tuple
tuple1 = ("And", "its", "Thursday")
print(tuple1)

('And', 'its', 'Thursday')

In [0]:
nest = tuple, tuple1
print(nest)

(("Today's", 'Date', 'Is', 15, 'August', 2019), ('And', 'its', 'Thursday'))


## 5) Are tuples immutable or mutable?¶

Before answering the question you should know that immutable values are unchangeable and mutable values are changeable. Now let's answer the question, the answer is tuples are immutable, yes once a tuple is created we can never change their values. Don't believe me, I'll prove it to you.

In [0]:
# Tuples are immutable in nature
print(tuple[0])

Today's

In [0]:
# Changing the value of the 0th index to "Hi"
tuple[0] = ("Hi")
print(tuple)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-75-009213b2e131> in <module>()
----> 1 tuple[0] = ("Hi")
2 print(tuple)

TypeError: 'tuple' object does not support item assignment

Hence the above proves that tuples are immutable in nature.

## 6) Can tuples store identical data within them?¶

Yes, tuples can store identical data within them, we can store many identical values inside a tuple. For example:

In [0]:
# Storing identical data with a tuple
tuple = ("Today's", "Date", "Is", 15, "August", 2019)
print(tuple)

("Today's", 'Date', 'Is', 15, 'August', 2019)

In [0]:
tuple = [("Today's", "Date", "Is", 15, "August", 2019), ("Today's", "Date", "Is", 15, "August", 2019)]
print(tuple)

[("Today's", 'Date', 'Is', 15, 'August', 2019), ("Today's", 'Date', 'Is', 15, 'August', 2019)]


## 7) How can you loop through a tuple?¶

This question is rather direct using a looping construct we can loop through a tuple. Below I will use a for loop and iterate through the values inside a tuple, you can similarly use other looping constructs and get the results.

In [0]:
tuple = ("Today's", "Date", "Is", 15, "August", 2019)
print(tuple)

("Today's", 'Date', 'Is', 15, 'August', 2019)

In [0]:
for i in tuple:
print(i)

Today's
Date
Is
15
August
2019


## 8) How to access the index of the tuple using a loop?¶

We can do this using a for loop with a enumerate function. Enumerate is a built-in function of Python. It allows us to loop over something and have an automatic counter, to know more about enumerating read its complete documentation of it here. For example:

In [0]:
# Accessing the index of the tuple using enumerate function.
tuple = ("Today's", "Date", "Is", 15, "August", 2019)
print(tuple)

("Today's", 'Date', 'Is', 15, 'August', 2019)

In [0]:
for counter, value in enumerate(tuple):
print(counter, value)

0 Today's
1 Date
2 Is
3 15
4 August
5 2019


## 9) Can we remove the values or items from the tuples?¶

I think now you can answer this question easily. The answer is No, you cannot remove the values or items from the tuples but you can delete the tuple completely. This is because tuples are immutable. For example:

In [0]:
# Deleting an entire tuple using del
tuple = ("Today's", "Date", "Is", 15, "August", 2019)
tuple

Out[0]:
("Today's", 'Date', 'Is', 15, 'August', 2019)
In [0]:
del tuple
print(tuple)

<class 'tuple'>


As seen above the entire tuple can be deleted using del, so when we print the tuple you can see that there are no elements in it.

## 10) How to count the number of times the values have appeared in the tuple?¶

This can be done by using the count method of the tuple, the count method returns the number of times a value has appeared in a tuple. For example:

In [0]:
# Counting the number of times a value has appeared in the tuple
tuple = ("Today's", "Date", "Is", 15, "August", 2019, "And", "Day", "Is", "Thursday")
print(tuple)

("Today's", 'Date', 'Is', 15, 'August', 2019, 'And', 'Day', 'Is', 'Thursday')

In [0]:
print(tuple.count("Is"))

2

In [0]:
print(tuple.count(15))

1


So in the above, the value "Is" has appeared 2 times and similarly, 15 has appeared 1 time.

## 11) How to get the index of a value in a tuple?¶

This can be done by using the index method of the tuple, the index method searches for the first occurrence of the value, and return its position. For example:

In [0]:
# Counting the number of times a value has appeared in the tuple
tuple = ("Today's", "Date", "Is", 15, "August", 2019, "And", "Day", "Is", "Thursday")
print(tuple)

("Today's", 'Date', 'Is', 15, 'August', 2019, 'And', 'Day', 'Is', 'Thursday')

In [0]:
print(tuple.index("Date"))

1

In [0]:
print(tuple.index("August"))

4

In [0]:
print(tuple.index("Is"))

2


As seen above the index method returns the position of the value, but in the last case of "Is", the index method returns the first occurrence of "Is" in the tuple and return its position.

## 13) How to check if the value is present inside a tuple or not?¶

This can be check using the "in" keyword within an if statement. For example, let us check whether August is present in the tuple.

In [0]:
# Checking for the values present in the tuple
tuple = ("Today's", "Date", "Is", 15, "August", 2019)
print(tuple)

("Today's", 'Date', 'Is', 15, 'August', 2019)

In [0]:
# Case 1:
if "August" in tuple:
print(True)
print(tuple.index("August"))
else:
print(False)

True
4

In [0]:
# Case 2:
if "September" in tuple:
print(True)
print(tuple.index("September"))
else:
print(False)

False


As seen above "August" is present in position 4 of the tuple, but whereas "September" is not present in the tuple.

Hence above are the very important techniques or function of Tuples in Python written in a question-and-answer format (It is very useful usually during programming interviews). I referred to some examples from Python Tuples. I have written this tutorial a simple way such that everybody can understand and master the concepts of Tuples in Python without prior programming knowledge or experience. If you guys have some doubts in the code, the comment section is all yours.