Introduction
Welcome to my 24th blog post. Today I learned about 'Tuples' in python. Tuple is an interesting datatype that is unmutable but performs similar functions to lists. Now let's dive deep into the details of tuples and its methods for a better understanding
So let's get started......
What is a Tuple?
Tuples are ordered collections of data items.
They store multiple items similar to a list in a single variable.
They are separated by comma and inserted in round brackets ().
Tuple is a unmutable datatype in python i.e unchangeable meaning we cannot alter a tuple after creation.
Note: If you are creating a tuple with one element then β,β is necessary else it will consider it as string or int type depending on the element in tuple.
tup1 = (2) #this will be considered as type int
print(type(tup1), "this is tup1")
tup2 = (2,) #this will be considered as type tuple
print(type(tup2), "this is tup2")
Example 1 -
tuple1 = (1,2,2,3,5,4,6)
tuple2 = ("Red", "Green", "Blue")
print(tuple1)
print(tuple2)
(1, 2, 2, 3, 5, 4, 6)
('Red', 'Green', 'Blue')
Example 2 - This includes data as a combination of int and strings.
details = ("Abhijeet", 18, "FYBScIT", 9.8)
print(details)
('Abhijeet', 18, 'FYBScIT', 9.8)
Tuple Indexes
Just like lists, each element in the tuple has its unique index. This index can be used to access any particular item from the tuple. The first item has index [0], second item has index [1], third item has index [2] and so on.
Example -
country = ("Spain", "Italy", "India",)
# [0] [1] [2]
Now let a take a look at how we can access tuple items
Positive Indexing
As we have seen that tuple items have index, as such we can access items using these indexes.
Example -
country = ("Spain", "Italy", "India",)
# [0] [1] [2]
print(country[0])
print(country[1])
print(country[2])
Spain
Italy
India
Negative Indexing
Similar to positive indexing, negative indexing is also used to access items, but from the end of the tuple. The last item has index [-1], second last item has index [-2], third last item has index [-3] and so on.
Example -
country = ("Spain", "Italy", "India", "England", "Germany")
# [0] [1] [2] [3] [4]
print(country[-1]) # Similar to print(country[len(country) - 1])
print(country[-3])
print(country[-4])
Germany
India
Italy
Checking for Item In Tuple
We can check if a given item is present in the tuple. This is done using the 'in' keyword.
Example 1 -
tup = (1, 2, 76, 342, 32, "green", True)
print(type(tup), tup)
if 3421 in tup:
print("Yes 342 is present in this tuple")
else:
print("3421 not present")
<class 'tuple'> (1, 2, 76, 342, 32, 'green', True)
3421 not present
Example 2 -
country = ("Spain", "Italy", "India", "England", "Germany")
if "Germany" in country:
print("Germany is present.")
else:
print("Germany is absent.")
Germany is present.
Example 3 -
country = ("Spain", "Italy", "India", "England", "Germany")
if "Russia" in country:
print("Russia is present.")
else:
print("Russia is absent.")
Russia is absent.
Range of Index
We can print a range of tuple items by specifying where you want to start, where you want to end and if you want to skip elements in between the range.
Syntax -
Note: The jump index is optional.
Tuple[start : end : jumpIndex]
Example - Printing elements within a particular range
animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[3:7]) #using positive indexes
print(animals[-7:-2]) #using negative indexes
('mouse', 'pig', 'horse', 'donkey')
('bat', 'mouse', 'pig', 'horse', 'donkey')
In the above examples, we provided the start and end indexes. The end index value will not be included.
Example: Printing all elements from a given index till the end
animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[4:]) #using positive indexes
print(animals[-4:]) #using negative indexes
('pig', 'horse', 'donkey', 'goat', 'cow')
('horse', 'donkey', 'goat', 'cow')
Note: When no index is provided, the interpreter prints all values till the end.
Example: Printing all elements from start to a given index
animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[:6]) #using positive indexes
print(animals[:-3]) #using negative indexes
('cat', 'dog', 'bat', 'mouse', 'pig', 'horse')
('cat', 'dog', 'bat', 'mouse', 'pig', 'horse')
Note: When no index is provided, the interpreter takes the start index from the beginning to end.
Example: Print alternate values
animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[::2]) #using positive indexes
print(animals[-8:-1:2]) #using negative indexes
('cat', 'bat', 'pig', 'donkey', 'cow')
('dog', 'mouse', 'horse', 'goat')
Here we used the 3rd parameter jump index to skip some values. '2' indicates that it will skip to every 2nd value.
Example: printing every 3rd consecutive within given range
animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[1:8:3])
('dog', 'pig', 'goat')
Here jump index is 3, which means the interpreter will consider every 3rd value.
Resources Used
You can watch the video of Day#24 by clicking on the below link πππππ
Conclusion
Thanks, guys for going through this blog post. On day #24 we learned a lot about the tuple and its methods i.e commonly used tuple methods among many. We also wrote a bunch of code while practically solving the methods.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #24
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.