We have the 'Tuple' with three values, 5, 'John' and 'Python' . And we want to access the second element i.e. 'John' .
x = (5, "John", "Python") print(x[1])
So , in the above code we have created a 'Tuple' and initialised to the variable 'x' .
Now , let us see, how the values are positioned in the Tuple
So , as we can see the elements are positioned as '0' , '1' and '2' . And if we want to access the second element, we can refer to the position '1' using the square brackets (i.e. x[1]) .
And the print statement prints the value of the second element of the 'Tuple' (i.e. 'John' ).
So , we have seen how to access the 'Tuple' elements using positive Indexes (i.e. '0', '1', '2')
Well ! Elements in the 'Tuple' can also be accessed using negative indexes.
We have the 'Tuple' with three values, 5, 'John' and 'Python' . And we want to access the second element i.e. 'John' .
x = (5, "John", "Python") print(x[-2])
So , in the above code we have created a 'Tuple' and initialised to the variable 'x' .
Now , let us see, how the values are positioned in the Tuple
So , as we can see the elements are positioned as '-1' , '-2' and '-3' from backward direction.
And if we want to access the second element using negative index, we can refer to the position '-2' using the square brackets (i.e. x[-2]) .
And the print statement prints the value of the second element of the 'Tuple' (i.e. 'John' ).
We have the 'Tuple' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim' . And we want to access the second, third and fourth element i.e. 'John', 'Paul' and 'Kriti' .
x = ("Mohan", "John", "Paul", "Kriti", "Salim") print(x[1:4])
So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.
Now, let us see, how the values are positioned in the Tuple
So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.
Now, if we want to access the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').We can specify the range using ':'.
i.e. x[1:4]
'x[1:4]' actually tells, to pick the elements from index/position '1' to the index/position '4-1' i.e. 3.
And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').
Say, we have the 'Tuple' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'.
And we want to access the first three elements i.e. 'Mohan', 'John' and 'Paul'.
x = ("Mohan", "John", "Paul", "Kriti", "Salim") print(x[:3])
So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.
Now, let us see, how the values are positioned in the Tuple
So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.
Now, if we want to access the first three elements (i.e. 'Mohan', 'John' and 'Paul').We can specify the range using ':'.
Just that we won't be mentioning the starting point. And only put the endpoint.
i.e. x[:3]
'x[:3]' actually tells, to pick the elements from the start till the index/position '3-1' i.e. 2.
And the print statement prints the first three elements (i.e. 'Mohan', 'John' and 'Paul').
print(x[:3])
Just like the previous example, we have the 'Tuple' with five values, 'Mohan', 'John', 'Paul','Kriti' and 'Salim'.
And we want to access the last three elements i.e. 'Paul', 'Kriti' and 'Salim'.
x = ("Mohan", "John", "Paul", "Kriti", "Salim") print(x[2:])
So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.
In the below way the values are positioned in the Tuple,
So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.
Now, if we want to access the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').We can specify the range using ':'.
Just that we won't be mentioning the ending point this time. And only put the starting point.
i.e. x[2:]
'x[2:]' actually tells, to pick the elements from the index/position '2' till the last element.
And the print statement prints the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').
We have the 'Tuple' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'. And we want to access the second, third and fourth element i.e. 'John', 'Paul' and 'Kriti'.
x = ("Mohan", "John", "Paul", "Kriti", "Salim") print(x[-4:-1])
So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.
Now, let us see, how the values are positioned in the Tuple
So, as we can see the elements are positioned as '-5', '-4', '-3', '-2' and '-1'.
Now, if we want to access the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').We can specify the range using ':'.
i.e. x[-4:-1]
'x[-4:-1]' actually tells, to pick the elements from index/position '-4' to the index/position '-1-1' i.e. -2.
And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').
Say, we have the 'Tuple' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'.
And we want to access the first three elements i.e. 'Mohan', 'John' and 'Paul'.
x = ("Mohan", "John", "Paul", "Kriti", "Salim") print(x[:-2])
So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.
Now, let us see, how the values are positioned in the Tuple.
So, as we can see the elements are positioned as '-5', '-4', '-3', '-2' and '-1'.
Now, if we want to access the first three elements (i.e. 'Mohan', 'John' and 'Paul').We can specify the range using ':'.
Just that we won't be mentioning the starting point. And only put the endpoint.
i.e. x[:-2]
'x[:-2]' actually tells, to pick the elements from the start till the index/position '-2-1' i.e. -3.
And the print statement prints the first three elements (i.e. 'Mohan', 'John' and 'Paul').
Just like the previous example, we have the 'Tuple' with five values, 'Mohan', 'John', 'Paul','Kriti' and 'Salim'.
And we want to access the last three elements i.e. 'Paul', 'Kriti' and 'Salim'.
x = ("Mohan", "John", "Paul", "Kriti", "Salim") print(x[-3:])
So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.
In the below way the values are positioned in the Tuple,
So, as we can see the elements are positioned as '-5', '-4', '-3', '-2' and '-1'.
Now, if we want to access the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').We can specify the range using ':'.
Just that we won't be mentioning the ending point this time. And only put the starting point.
i.e. x[-3:]
'x[-3:]' actually tells, to pick the elements from the index/position '-3' till the last element.
And the print statement prints the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').