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:

Python tuple

See online demo and code

The code for creating and displaying tuple:

 

An example of creating tuple of strings

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

tuple strings

See online demo and code

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

 

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:

tuple mixed

See online demo and code

The code:

 

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:

tuple index

See online demo and code

The code:

 

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:

tuple from tuple

See online demo and code

The code:

 

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:

tuple length

See online demo and code

The code for getting the length of a tuple:

 

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:

tuple list

See online demo and code

The code:

 

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:

tuple max

See online demo and code

The code:

 

Getting minimum value

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

tuple min

See online demo and code

The code for getting the minimum values: