Professional Documents
Culture Documents
Tuples - Ipynb - Colaboratory
Tuples - Ipynb - Colaboratory
alt text
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.
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:
print(type(tuple))
<class 'tuple'>
As seen above the tuple can hold heterogeneous data (Hence proved :))
Is
print(tuple[5])
2019
Today's
6) Can tuples
SEARCH store identical data within them?
STACK OVERFLOW
Yes, tuples can store identical data within them, we can store many identical values inside a tuple. For
example:
tuple = [("Today's", "Date", "Is", 15, "August", 2019), ("Today's", "Date", "Is", 15, "August", 201
print(tuple)
[("Today's", 'Date', 'Is', 15, 'August', 2019), ("Today's", 'Date', 'Is', 15, 'August', 2019)
for i in tuple:
print(i)
Today's
Date
Is
15
August
2019
0 Today's
1 Date
2 Is
3 15
4 August
5 2019
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:
# 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')
print(tuple.count("Is"))
print(tuple.count(15))
So in the above, the value "Is" has appeared 2 times and similarly, 15 has appeared 1 time.
("Today's", 'Date', 'Is', 15, 'August', 2019, 'And', 'Day', 'Is', 'Thursday')
print(tuple.index("Date"))
print(tuple.index("August"))
print(tuple.index("Is"))
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.
# Case 1:
if "August" in tuple:
print(True)
print(tuple.index("August"))
else:
print(False)
True
4
# 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.
Thank you.