Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - ACCESSING TUPLES


Accessing the elements of the 'Tuple' using positive Indexes


We have the 'Tuple' with three values, 5, 'John' and 'Python' . And we want to access the second element i.e. 'John' .


Example :


x = (5, "John", "Python")
print(x[1])   


Output :



  John

So , in the above code we have created a 'Tuple' and initialised to the variable 'x' .


x = (5, "John", "Python")

Now , let us see, how the values are positioned in the Tuple


java_Collections

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' ).


print(x[1])

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.


Accessing the elements of the 'Tuple' 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' .


Example :


x = (5, "John", "Python")
print(x[-2])  


Output :



  John

So , in the above code we have created a 'Tuple' and initialised to the variable 'x' .


x = (5, "John", "Python")

Now , let us see, how the values are positioned in the Tuple


java_Collections

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' ).


print(x[-2])

Accessing a chunk of elements of the 'Tuple' using range of Indexes


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' .


Example :


x = ("Mohan", "John", "Paul", "Kriti", "Salim")
print(x[1:4])  


Output :



  ('John', 'Paul', 'Kriti')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "John", "Paul", "Kriti", "Salim")

Now, let us see, how the values are positioned in the Tuple


java_Collections

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.


java_Collections

And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').


print(x[1:4])


Output :



  ('John', 'Paul', 'Kriti')

Accessing first few elements of the 'Tuple' using range of Indexes


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'.


Example :


x = ("Mohan", "John", "Paul", "Kriti", "Salim")
print(x[:3]) 


Output :



  ('Mohan', 'John', 'Paul')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "John", "Paul", "Kriti", "Salim")

Now, let us see, how the values are positioned in the Tuple


java_Collections

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.


java_Collections

And the print statement prints the first three elements (i.e. 'Mohan', 'John' and 'Paul').


print(x[:3])



Output :



  ('Mohan', 'John', 'Paul')

Accessing last few elements of the 'Tuple' using range of Indexes


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'.


Example :


x = ("Mohan", "John", "Paul", "Kriti", "Salim")
print(x[2:]) 


Output :



  ('Paul', 'Kriti', 'Salim')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "John", "Paul", "Kriti", "Salim")

In the below way the values are positioned in the Tuple,


java_Collections

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.


java_Collections

And the print statement prints the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').


print(x[2:])


Output :



  ('Paul', 'Kriti', 'Salim')

Accessing a chunk of elements of the 'Tuple' using range of negative Indexes


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'.


Example :


x = ("Mohan", "John", "Paul", "Kriti", "Salim")
print(x[-4:-1]) 


Output :



  ('John', 'Paul', 'Kriti')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "John", "Paul", "Kriti", "Salim")

Now, let us see, how the values are positioned in the Tuple


java_Collections

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.


java_Collections

And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').


print(x[-4:-1])


Output :



  ('John', 'Paul', 'Kriti')

Accessing first few elements of the 'Tuple' using range of negative Indexes


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'.


Example :


x = ("Mohan", "John", "Paul", "Kriti", "Salim")
print(x[:-2]) 


Output :



  ('Mohan', 'John', 'Paul')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "John", "Paul", "Kriti", "Salim")

Now, let us see, how the values are positioned in the Tuple.


java_Collections

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.


java_Collections

And the print statement prints the first three elements (i.e. 'Mohan', 'John' and 'Paul').


print(x[:-2])

Output :



  ('Mohan', 'John', 'Paul')

Accessing last few elements of the 'Tuple' using range of negative Indexes


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'.


Example :


x = ("Mohan", "John", "Paul", "Kriti", "Salim")
print(x[-3:]) 


Output :



  ('Paul', 'Kriti', 'Salim')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "John", "Paul", "Kriti", "Salim")

In the below way the values are positioned in the Tuple,


java_Collections

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.


java_Collections

And the print statement prints the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').


print(x[-3:])

Output :



  ('Paul', 'Kriti', 'Salim')