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.
So, let’s given an example tom understand python tuple access better.
Here, we access the second item in the tuple with item number 1. And the output is:
You can also watch the video of this lesson!
If we use negative numbers as index, this means that it will start to count from the last item. So, for negative numbers,
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.
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.
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.
If we use only one parameter in square brackets, we can select the remainings with colon ( : ).
With this code, we take the items starting with index 3, in other words, from the fourth item of python tuple.
If we use the colon ( : ) operator in the other side;
the output will be like below. The items up to index 3 will be in the python tuple.
We can also use negative numbers together for python tuple access.
The output of this code will be like below:
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.