Python Tuple Access

python-tuple-access-ipcisco-1

Accessing Items In Python Tuples With Index

 

Pyton Tuples has items in it. For python tuple access or in other words, to access the items in a python tuple, we can use index numbers. The usge of this index number is inside the square brackets.

 

The index numbers starts with 0 and goes on. If you would like to access an item in the python tuple, you should give its index number.

 

  • The first item in the tuple has index 0,
  • The second item in the tuple has index 1,
  • The third item in the tuple has index 2 and so on.

 

So, let’s given an example tom understand python tuple access better.

 

cars = ("BMW", "Audi", "Mercedes")
print(cars[1])

 

Here, we access the second item in the tuple with item number 1. And the output is:

 

Audi

 

You can also watch the video of this lesson!

 

python-tuple-access-ipcisco-1

 

If we use negative numbers as index, this means that it will start to count from the last item. So, for negative numbers,

 

  • index -1 shows the last item of the python tuple,
  • index -2 shows the second last item of the python tuple and so on.

 

 

If we would like to access a range of the items in a tuple, then we use two parameters as starting item and the last item. And between them, we use colon ( : ) operator. LEt’s show this with an example.

 

cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")
print(cars[2:5])

 

Here, in the output we will take the third item, fourth item and the fifth item. The first one, index 2 shows third item and the last one index five shows the sixht item. And the sixth item is not included.

 

('Mercedes', 'Alfa Romeo', 'Aston Martin')

 

python-tuple-access-ipcisco-2

 

If we use a third operator, the step operator, then, we can also jump on some items. In the below example, we will jump one while returning with the tuple items.

 

cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")
print(cars[2:5:2])

 

('Mercedes', 'Aston Martin')

 

python-tuple-access-ipcisco-3

 

If we use only one parameter in square brackets, we can select the remainings with colon ( : ).

 

cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")
print(cars[3:])

 

With this code, we take the items starting with index 3, in other words, from the fourth item of python tuple.

 

("Alfa Romeo", "Aston Martin", "Porche")

 

python-tuple-index-ipcisco-1

 

If we use the colon ( : ) operator in the other side;

 

cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")
print(cars[:3])

 

the output will be like below. The items up to index 3 will be in the python tuple.

 

("BMW", "Audi", "Mercedes")

 

python-tuple-index-ipcisco-2

 

We can also use negative numbers together for python tuple access.

 

cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")
print(cars[-5:-2])

 

The output of this code will be like below:

 

('Audi', 'Mercedes', 'Alfa Romeo')

 

python-tuple-index-ipcisco-3

 

Here, the negative indexes starts at the end of the tuple as we have mentined before.

 

And do not forget that, when we use indexes with colon ( : ) operator for python tuple access, we start with the index that is given as the first parameter and we continue to take until the second parameter. But the parameter is not included.

 

Back to: Python Programming Course > Python Tuples

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k