Introduction
Welcome to my 25th blog post. Today I learned about 'Different Operations on Tuples' in python. In the last blog post, we saw that tuples are immutable and to perform operations on them we need to convert them to list first. Now let's dive deep onto what operations can be done on tuples in python.
So let's get started......
Manipulating Tuples
We know that tuples are immutable, hence if we want to add, remove or change tuple items, then first we must convert the tuple to a list. Then perform the operations on that list and convert it back to the tuple.
Example -
countries = ("Spain", "Italy", "India", "England", "Germany")
temp = list(countries) #convert tuple to list
temp.append("Russia") #add item
temp.pop(3) #remove item
temp[2] = "Finland" #change item
countries = tuple(temp)
print(countries)
Output -
('Spain', 'Italy', 'Finland', 'Germany', 'Russia')
In the above example, we converted tuple countries to a list and then we manipulated the data and again we converted the list back to a tuple.
However, we can directly concatenate two tuples without converting them to list.
Example -
countries = ("Pakistan", "Afghanistan", "Bangladesh", "SriLanka")
countries2 = ("Vietnam", "India", "China")
southEastAsia = countries + countries2
print(southEastAsia)
Output -
('Pakistan', 'Afghanistan', 'Bangladesh', 'SriLanka', 'Vietnam', 'India', 'China')
Tuple Methods
As a tuple is an immutable datatype collection of elements it has some limited built-in methods. Let us look at them below:
count() method - It returns the number of times the given element appears in the tuple.
Syntax -
tuple.count(element)
Example -
tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)
res = tuple1.count(3)
print('Count of 3 in Tuple1 is:', res)
3
index() method - It returns the first occurrence of the given element from the tuple
Syntax:
tuple.index(element, start, end)
Note: This method raises a ValueError if the element is not found in the tuple.
Example -
Tuple = (0, 1, 2, 3, 2, 3, 1, 3, 2)
res = Tuple.index(3)
print('First occurrence of 3 is', res)
Output
3
Resources Used
You can watch the video of Day#25 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day #25 we completed with learning operations on a tuple. Tuple being an immutable datatype has a limited number of oprations that can be performed.
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 #25
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.