Python Tuple: Explained with 9 examples

What is the tuple in Python?

The tuple is a compound data type in Python which is immutable. A tuple is a sequence just like we learned in the list chapter.

However, the difference between the two is, the tuple is immutable while lists are mutable. That means, once created, a tuple cannot b e changed while a list can be changed.

You cannot delete or add elements in a tuple, once it is created.

Besides, a tuple is enclosed in parenthesis () while lists, generally, are enclosed in square brackets [].

Another difference between the tuple and lists is, the tuple are heterogeneous data structures while lists are homogenous.

Syntax of constructing Python tuples

The general syntax of creating tuples:

tup = (5,10,15)

The tuple items are comma separated and may store mixed items as well:

mix_tup = (1,’a’,2,’b’)

An example of creating a numbered tuple

In this example, a tuple of five numbers is created and displayed by using a for loop. See the code and demo by clicking the link or image below:

The code for creating and displaying tuple:

tup_ex = (10,20,30,40,50)



for i in tup_ex:



print ("The tuple item:",i)

Output:

Python tuple

An example of creating tuple of strings

A tuple containing string items is also separated by commas as shown in the example below:

This is how the tuple of string is created and displayed by using a for loop:

# An example of constructing a tuple of strings

str_tuple = ('Tuple','is','a','compound','data type','in', 'Python')

for tup_item in str_tuple:

print ("The current tuple item:",tup_item)

Result:

tuple strings

An example of creating a tuple with mixed items

A tuple Python may contain mixed items, for example, numbers and strings. See this example where a tuple is created with seven numbers and seven strings:

The code:

# An example of constructing a mixed item tuple



mixed_tuple = (1,'List',2,'is',3,'a',4,'compound',5,'data type',6,'in', 7,'Python')



for mixed_item in mixed_tuple:



print ("The current mixed tuple item:",mixed_item)

Output:

tuple mixed

Accessing specific elements of a tuple example

The specific tuple items can be accessed by using the index of elements. The element index starts at zero. Use the square brackets to enclose an index or indices as shown in the example below:

The code:

#Demo of accessing tuple elements by index numbers



a_tuple = (1,'Tuples',2,'are',3,'heterogeneous',4,'data structures')



print (a_tuple[1], a_tuple[3], a_tuple[5], a_tuple[7])

The above code will output:

Tuples are heterogeneous data structures

Creating a tuple from other tuples

You may create a tuple from other tuples as well. To demonstrate that, two tuples are created with three elements in each.

The third tuple is created based on first two tuples as shown in the example below:

The code:

#Demo of tuples

tup1 = (1,2,3)

tup2 = (10,20,30)



tup3 = tup1 + tup2



print ("The third tuple elements:" ,tup3)

Output:

tuple from tuple

Getting length of a tuple demo

You may get the length of a tuple by using the len function. Simply, provide a tuple name in the parenthesis after len keyword, as shown in the example below:

The code for getting the length of a tuple:

# An example of a tuple



tup_ex = (10,20,30,40,50)



print ("Total number of elements in tuple = ", len(tup_ex))

Output:

tuple length

Deleting tuple items?

As mentioned earlier, the tuples are immutable; once created, it cannot be changed. So you cannot remove tuple items individually. However, by using the del method, you can remove a tuple entirely.

In that case, if you try accessing the deleted tuple, it will generate an error.

e.g. del tup_name

Creating a tuple from a list example

You may use the tuple() method to convert a list or sequence into a tuple.

In this example, a list of three strings is created. After that, the tuple method is used for creating a new tuple by converting the list elements:

The code:

# An example of a tuple



lst = ['a','b','c']



tup_ex = tuple(lst)



print ("New tuple elements after conversion = ", len(tup_ex))

Result:

tuple list

Using max function for getting maximum value

The max function can be used to get the maximum value in the given tuple. See an example below where two tuples are created; one of the numbers and other is strings. After that, max function is used to get maximum values in both tuples:

The code:

# An example of a tuple

tup_numbers = (3,-5,100,500,50,2)

tup_strings = ('The', 'Python', 'is', 'Powerful')



print ("The maximum value in numbered tuple = ", max(tup_numbers))

print ("The maximum value in string tuple = ", max(tup_strings))

Output:

tuple max

Getting minimum value

Similarly, you may use the min function for getting minimum values in the given tuples. See a demo below:

The code for getting the minimum values:

# An example of a tuple



tup_numbers = (3,-5,100,500,50,2)

tup_strings = ('The', 'Python', 'is', 'Powerful')



print ("The minimum value in numbered tuple = ", min(tup_numbers))

print ("The minimum value in string tuple = ", min(tup_strings))

Output:

tuple min

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!