In this article we will focus on a complete walk through of a Python tuple data structure.

## What is a Python tuple

A Python tuple is a sequence data type that allows us to combine several items in one comma-separated data instance.

A tuple can contain elements of the same or different data types and it is immutable, which means that we can’t alter the size and content of a tuple object in Python after it is created (without converting it to different data structures and creating new tuples).

## How to create a Python tuple

Creating a tuple in Python is very simple. You need to put all the items separated by commas into parentheses.

Keep in mind that you can put different data types together, so integers, floats, booleans and even other tuples.

Here are a few examples:

### Tuple with string elements

my_tuple = ('Apple', 'Banana', 'Orange', 'Pineapple')
print(my_tuple)

Output:

('Apple', 'Banana', 'Orange', 'Pineapple')


### Tuple with integer elements

my_tuple = (1, 2, 3, 4)
print(my_tuple)

Output:

(1, 2, 3, 4)


### Tuple with mixed type elements

my_tuple = ('Apple', 1, True, 2.4)
print(my_tuple)

Output:

('Apple', 1, True, 2.4)


### Tuple with nested elements

In the examples above each item in a tuple only contained one value.

What if we want to store two values per item? Python tuples allow us to do that too!

For example, you would like to make a tuple of products and their prices. Your data is the following: price of an apple is $1 and price of a banana is$0.70.

In order to add two or more values per item in a tuple, we need to put them in parentheses and separate with commas.

It will look like having a tuple inside of a tuple (nested):

my_tuple = (('Apple', 1), ('Banana', 0.70))
print(my_tuple)

Output:

(('Apple', 1), ('Banana', 0.7))


## How to access an element from a Python tuple

An important and very useful property of a Python tuple is that it’s an indexed sequence, meaning for a tuple with n elements, the first element will have an index = 0, second element index = 1, and all the way to n-1.

### Access element from a tuple using index

The index can also be reversed meaning that the fist element will have an index = –n, second element index = –n+1, and all the way to -1.

To make it easier to showcase, take a look at the visualization below:

$$\begin{matrix} \begin{array}{c|cccc} \hline \text{Index} & 0 & 1 & 2 & 3 \\ \text{Tuple} & Apple & Banana & Orange & Pineapple \\ \text{Reversed Index} & -4 & -3 & -2 & -1 \\ \hline \end{array} \end{matrix}$$
$$\text{Length = n = 4}$$

We can see that ‘Apple’ element in the tuple has two indexes: 0 and -4.

Let’s recreate this tuple in Python:

my_tuple = ('Apple', 'Banana', 'Orange', 'Pineapple')


Now, we would like to print out the first element from the tuple.

We know from before, that it has two indexes: 0 and -4, so we can try both and see if the output will be the same.

print(my_tuple[0])
print(my_tuple[-4])

Output:

Apple
Apple


### Find element in a tuple

Let’s assume we have the following tuple with string elements:

my_tuple = ('Apple', 'Banana', 'Orange', 'Pineapple')
print(my_tuple)

Output:

('Apple', 'Banana', 'Orange', 'Pineapple')


And you would like to fine the index of “Banana” element.

This can be simply found by using .index() method that takes some value as a parameter and finds its index in a Python tuple:

element = 'Banana'
element_index = my_tuple.index(element)

print(element_index)

Output:

1


We quickly found that the “Banana” element is located at index 1 of our tuple.

## How to slice a Python tuple

In the section before we showed how you can access an element from a Python tuple using its exact index.

In this section we will show what to do when you want to access a range of elements, for example, first two or last two.

Recall that for retrieving elements from a tuple using an index, we placed it in square brackets [].

Slicing is using the same approach, but instead of passing a single index value, we will pass a range.

A range in Python is passed using the following syntax [from : to].

### Slicing without specifying from and to

If you don’t put any index into from and to, by default Python will take the entire tuple.

The following two lines of code produce an identical output:

print(my_tuple)
print(my_tuple[:])

Output:

('Apple', 'Banana', 'Orange', 'Pineapple')
('Apple', 'Banana', 'Orange', 'Pineapple')


### Slicing with specifying from

You can slice a tuple with just specifying from and what it will take is the elements from the index you specified until the end of the tuple.

For example, you would like to print the last two elements in the tuple. Remember that you can use both index and reverse index.

print(my_tuple[2:])
print(my_tuple[-2:])

Output:

('Orange', 'Pineapple')
('Orange', 'Pineapple')


### Slicing with specifying to

You can slice a tuple with just specifying to and what it will take is the elements from the beginning on the tuple until the element at the index you specified -1.

For example, you would like to print the first two elements of the tuple. Remember that you can use both index and reverse index.

The following code will produce the desired output:

print(my_tuple[:2])

Output:

('Apple', 'Banana')


Notice here, that we specified our to index = 2, what’s important to keep in mind is that index = 3 is actually the position of the third element. The way tuple slicing work in Python is that it goes through elements until the specified to index (in our case 2) and includes all elements up to that index but not including the element under the to index.

## Conclusion

This article is an introductory walkthrough for Python tuple data structure and its functionality which is important to learn as they are used in many areas of programming and in machine learning.

The post Everything About Python Tuple Data Structure: Beginner’s Guide appeared first on PyShark.