Tuples is an ordered sequences of items, just like lists. The main difference is, once the tuple is defined it cannot be changed, that is we cannot add, remove, and change the value’s. Tuple items are indexed.
Creating a tuple
There are two ways to create a tuple, to create a tuple use commas to separate values. The values are enclosed with parentheses(), the parentheses are optional but it can improve readability.
Example:
my_tuple = ("HTML", "Python", "Java")
print(my_tuple)
Output:
('HTML', 'Python', 'Java')
The another way of creating a tuple is by using tuple() function,
Example:
my_tuple = tuple(["HTML", "Python", "Java"])
print(type(my_tuple))
print(my_tuple)
Output:
<class 'tuple'>
('HTML', 'Python', 'Java')
Accessing Elements in Tuple
Like list each value in a tuple has an assigned index value, so the elements of a tuple can be easily accessed by it’s specific index number. The first value in the tuple is at index 0, the second value is at 1, etc.
Example:
my_tuple =("HTML", "Python", "Java")
print(my_tuple[1])
Output:
Python
Range of Index in a Tuple
We can access range of items in tuple by specifying the starting and ending index value, they are separated using colon[:].
Example:
my_tuple =("HTML", "Python", "Java", "CSS", "JavaScript", "C#")
print(my_tuple[1:5])
Output:
('Python', 'Java', 'CSS', 'JavaScript')
Updating Tuples
Once a tuple is created we cannot update or change the values of tuple elements. If we want to update a element in a tuple we want to convert tuple into list then update the elements in the list and then convert the list into tuple.
Example:
tuple1 = ("HTML", "Python", "Java", "CSS", "JavaScript", "C#")
print(tuple1)
list1 = list(tuple1)
list1[2] = "C"
tuple1 = tuple(list1)
print("\n")
print("The updated tuple is:")
print("\n")
print(tuple1)
Output:
('HTML', 'Python', 'Java', 'CSS', 'JavaScript', 'C#')
The updated tuple is:
('HTML', 'Python', 'C', 'CSS', 'JavaScript', 'C#')
Delete Tuple Elements
We cannot remove an individual element in a tuple as tuples are immutable. To delete entire tuple use the del keyword with tuple name.
Example:
my_tuple = ("Python", "HTML", "Java", "PHP")
print(my_tuple)
print(type(my_tuple))
del my_tuple
print(my_tuple)
Output:
('Python', 'HTML', 'Java', 'PHP')
<class 'tuple'>
Traceback (most recent call last):
File "mytup.py", line 5, in <module>
print(my_tuple)
NameError: name 'my_tuple' is not defined
Concatenation of Tuples
To Concatenation two tuples use + operator, this will concatenate two tuples.
Example:
my_tuple1 = ("Python", "HTML", "Java", "PHP")
my_tuple2 = ("CSS", "Java Script", "C#", "R")
my_tuple3 = my_tuple1 + my_tuple2
print(my_tuple3)
Output:
('Python', 'HTML', 'Java', 'PHP', 'CSS', 'Java Script', 'C#', 'R')
Count function
count() function is used to count a certain value in a tuple.
Example:
my_tuple = (1, 3, 9, 6, 7, 8, 9, 8, 2, 3, 8, 9, 6, 7, 6, 7, 3, 2, 9, 7)
print(my_tuple.count(9))
Output:
4
len function
Tuple is sequenced so we can count the total number of elements in a tuple by using len() function.
Example:
my_tuple = (1, 3, 9, 6, 7, 8, 9, 8, 2, 3, 8, 9, 6, 7, 6, 7, 3, 2, 9, 7)
print(len(my_tuple))
Output:
20
Loop in Tuple
We can loop the items in a tuple by using for loop.
Example:
my_tuple = ("Python", "Java", "HTML", "JavaScript", "CSS")
for i in my_tuple:
print(i)
Output:
Python
Java
HTML
JavaScript
CSS
Tuple with one item
To create a tuple with one item we want to add comma after the first item.
Example:
my_tuple1 = ("Python", )
my_tuple2 = ("Python")
print(type(my_tuple1))
print(type(my_tuple2))
Output:
<class 'tuple'>
<class 'str'>
Howdy! I could have sworn I’ve been to this blog before but after browsing
through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back
frequently!
Thanks for your support bro. Keep learning.
Great post! We will be linking to this great post on our website.
Keep up the great writing.
Thanks for your valuable feedback and keep supporting us.
Hello, I enjoy readibg all of your article.
I like to write a little comment to support you.
Greetings! Very helpful advice in this particular article!
It’s the little changes thast make the most important changes.
Many thanks for sharing!
Hello, after reading this amazing article i amm as well happy to share my familiarity here with mates!
With thanks ffor sharing your superb website!
Would love to perpetually get updated outstanding weeb blog!
I got this site from my pal who shared with me concerning this website and now this time I am browsing this
website aand reading very informative articles
or reviews at this place.
Thank you for this very good posts. I was wanting to know whether you were
planning of publishing similar posts to this. Keep up writing superb content
articles!
Really interesting information, I am sure this post has touched all internet users, its really really pleasant
piece of writing on building up new website.