Access Items in Tuple
You can access items in a tuple by referring to an index
Tuple items are indexed start from 0 ( Zero ), second index is 1....
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
print(tuple[1])
Output
banana
Negative Indexing
Negative indexing is start counting from end of the tuple
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
print(tuple[-1])
Output
apple
Range of index
you can get the specific index range of data like tuple_name [1:5]
string index is included and end index is excluded from tuple when runtime
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
print(tuple[1:5])
Output
(' banana', 'apple', 'mango' , 'graps' )
if not set the start index ,the range will start from start indexing
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
print(tuple[:5])
Output
('mango',' banana', 'apple', 'mango' , 'graps' )
if not set the end index ,the range will start from set start indexing to the end of tuple
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
print(tuple[1:]) #Return (' banana', 'apple', 'mango' , 'graps' , 'apple')
Range of negative Indexing
if you want the tuple start from end ,then applied a negative indexing
Example - 1
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
print(tuple[-5:-1])
Output
(' banana', 'apple', 'mango' , 'graps' )
Example - 2
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
print(tuple[-1:-5])
Output
( ) empty tuple