Python Tuples

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.

Python Tuples
Python Tuples

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 
    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'>