Pyton

Python Tuples

Python Tuples

Tuples in Python are the collection of objects that are arranged in a sequence. The tuples are one of the data structures in Python. The tuples work in the same way as the list. The major difference and between the tuples list is that the list is mutable, whereas the tuples are immutable. The immutable means that the tuple cannot be altered when it is declared. Tuples can store heterogeneous types of elements, i.e., integer, string, floating-point number, and complex numbers. This article explains the Python tuples.

Creating the tuples in Python

The tuples in Python are created by using a pair of parentheses. All the elements in a tuple are disjointed by a comma. Although, while declaring the tuples, inserting the parenthesis is optional. Nevertheless, it is a good approach to use parenthesis while declaring the tuples. It makes our script more structured and well organized, though. The tuples with only one element can also be created. However, do not forget to add the comma after writing first and only one element inside the tuple. Let's create tuples.

#creating an empty tuple
mytup = ()
#prinitng the tuple
print(mytup)
#creating a tuple of integers
mytup = (1,2,3,4,5,6,7,8,9)
#printing the tuple
print(mytup)
#creating a tuple of floating point number
mytup = (1.1,1.2,1.3,1.4,1.5)
#printing the tuple
print(mytup)
#creating a tuple of string values
mytup = ('Hello','welcome','to','linuxhint')
print(mytup)
#creating a tuple of mixed data types
mytup = (1,1.3,'HELLO',2+3j)
#printing the tuple
print(mytup)
#creating a tuple with * operator
#it will create 5 instances of the given string
mytup = ('linuxhint',)*5
#printing the tuple
print(mytup)
#list inside the tuple
mytup = (1,2,[1,2,3])
#printing the tuple
print(mytup)
#creating tuple without paranthesis
mytup = 1,2,3,10.3,'kamran'
#printing the tuple
print(mytup)
#creating tuple with only one element
mytup = ('linuxhint',)
#printing the tuple
print(mytup)

Output

Accessing the tuple elements

Once the tuple is created, we can access the elements of tuples very easily. The following are the ways to access the tuple elements:

  1. Using the index operator
  2. Using the slicing operator

First, let's discuss the index operator. The pair of square brackets [] is known as the index operator in Python. The index value always starts from zero. If the total number of elements in a tuple is 10, then the index value will be from 0 to 9. While using the indexing operator, we mention the index number of the element inside the square brackets, and it returns a single value.

Besides, the slicing operator (:) returns a range of elements in a given sequence. Let's see the use of index operator and slicing operator for accessing the tuple elements in Python.

#creating a tuple of integers
mytup = (1,2,3,4,5,6,7,8,9)
#accessing the elements using the index operator
#acesing the first element
print(mytup[0])
#acesing the second element
print(mytup[1])
#acesing the third element
print(mytup[2])
#acesing the fourth element
print(mytup[3])
#acesing the fifth element
print(mytup[4])
#acesing the sixth element
print(mytup[5])
#acesing the seventh element
print(mytup[6])
#acesing the eigth element
print(mytup[7])
#acesing the ninth element
print(mytup[8])
#using the slicing operator to access the tuple elements
#printing the range of elements from 0 to 3
print(mytup[0:3])
#printing the range of elements from 0 to 6
print(mytup[0:6])
#printing the range of elements from 6 to 9
print(mytup[6:9])

Output

Moreover, we can also concatenate the two 2 or more operators using the '+' operator.

#creating a tuple of integers
mytup1 = (1,2,3,4,5,6,7,8,9)
#creating a tuple of string values
mytup2 = ('Hello','welcome','to','linuxhint')
#concatenating the 2 tuples
mytup3 = mytup1+mytup2
#printing the concatenated tuple
print(mytup3)

Output

Updating the tuples

As discussed previously, the tuples are immutable; it means that its item cannot be changed. However, if we have some mutable objects like lists inside the tuples, then we can update its value. The particular value of the list can be accessed using the index operator. Let's see a simple program.

#creating a tuple with the list
mytup1 = (1,2,3,4,5,[6,7,8])
#printing the tuple before updating it
print(mytup1)
#updating the list items
mytup1[5][0] = 'Kamran'
mytup1[5][1] = 'Sattar'
mytup1[5][2] = 'Awaisi'
#prinitng the updated tuple
print(mytup1)

Output

Deleting the tuple

The elements or items inside the tuple cannot be deleted. However, we can delete or remove the whole tuple using the del keyword. Deleting a specific tuple will cause an error.

#creating a tuple with the list
mytup1 = (1,2,3,4,5,[6,7,8])
del mytup1[0]

Output

Let's delete the whole tuple.

#creating a tuple with list
mytup1 = (1,2,3,4,5,[6,7,8])
#deleting the tuple
del mytup1

Output

The output does not show any error; it means that the tuple is deleted successfully.

Determining the length of the tuple

The length of the tuple can be determined using the len() function. The len() function returns the total number of elements or items of the tuple.

#creating a tuple with list
mytup1 = (1,2,3,4,5,[6,7,8])
#printing the length of tuple
print(len(mytup1))

Output

The max and min tuple

Python provides two built-in max() and min() functions that return the maximum item and minimum item inside the tuple, respectively. These functions take the tuple object as an argument.

Let's print the max and min items of tuple.

#creating a tuple
mytup1 = (1,2,3,4,5,6,7,8)
#printing the max tuple
print("The max tuple is: ",max(mytup1))
#printing the min tuple
print("The min tuple is:",min(mytup1))

Output

The tuple conversion

A tuple can easily be converted to a list in the following way:

#creating a tuple
mytup1 = (1,2,3,4,5,6,7,8)
#coverting the tuple to a list
print(list(mytup1))

Output

The tuple is successfully converted to a list.

Similarly, the list can be converted to a tuple.

#creating a tuple with the list
mylist = [1,2,3,4,5,6,7,8]
#coverting the list to a tuple
print(tuple(mylist))

Output

The list is successfully converted tuple.

Conclusion

The Python tuples are the most commonly used data structures that store the data in a sequence. The tuples are immutable and support various operations. This article explains the Python tuples with many examples.

Gry Open Source Ports of Commercial Game Engines
Open Source Ports of Commercial Game Engines
Free, open source and cross-platform game engine recreations can be used to play old as well as some of the fairly recent game titles. This article wi...
Gry Najlepsze gry wiersza poleceń dla systemu Linux
Najlepsze gry wiersza poleceń dla systemu Linux
Wiersz poleceń jest nie tylko twoim największym sprzymierzeńcem podczas korzystania z Linuksa - może być również źródłem rozrywki, ponieważ możesz go ...
Gry Najlepsze aplikacje do mapowania gamepada dla systemu Linux
Najlepsze aplikacje do mapowania gamepada dla systemu Linux
Jeśli lubisz grać w gry na Linuksie za pomocą gamepada zamiast typowego systemu wprowadzania klawiatury i myszy, jest kilka przydatnych aplikacji dla ...