Tuple Indexing
Indexing in the tuple works in the same way as in the List. Each Item is assigned the index position that starts with 0. We can access each item of tuple by passing the index position in the square brackets.
Example:
emp_details = (‘Brad’,’Lee’,’Sales’,380,250000,True,’New York’,’USA’,’Developer’,’Python’)
Few Examples:
emp_details = ('Brad','Lee','Sales',380,250000,True)
Check what is at position 0.
print(emp_details[0])
Position 4.
print(emp_details[4])
Tuple Slicing
When we need to extract more than one item from the tuple, then slicing is performed. While performing slicing, we can pass three optional parameters in the square brackets([]) separated by a colon(:):
Tuple[StartPosition
:EndPosition
:Jump
]
StartPosition | Item at which we want to start. Default is the first character. |
---|---|
EndPosition | Up to which item we want to go(Note: this is up to, the item at this specific location will not be included). Default is the last last. |
Jump | The number of items we want to jump. Default is 1 means it will not jump anything. |
When we don’t use a colon(:) then the specified argument is considered as the position of an item.
Let us understand index positions with some examples:
Only using start to end.
emp_details = ('Brad','Lee','Sales',380,250000,True,'New York','USA','Developer','Python')
print(emp_details[0:])
Start with 3 to end.
print(emp_details[3:])
Go up to 3. Note: Item at position 3 will not be included.
print(emp_details[:3])
Include all jumping a character.
print(emp_details[::2])
Start at 2 up to 8.
print(emp_details[2:8])
Start with 1 up to 8 with jump of 3.
print(emp_details[1:8:3])
Negative Indexing
We can also use negative indexing. The last item is assigned with the index position of -1. It keeps on increasing in the negative order till the first item. If we want to traverse backward, we can use jump as a negative number. Example -1.
Negative Index Positions:
Example:
emp_details = (‘Brad’,’Lee’,’Sales’,380,250000,True,’New York’,’USA’,’Developer’,’Python’)
Let us understand with some examples:
Item at -2 position.
emp_details = ('Brad','Lee','Sales',380,250000,True,'New York','USA','Developer','Python')
print(emp_details[-2])
This will not work because the default jump size is 1 which is positive but to traverse backward we need a negative jump size.
print(emp_details[-2:-6])
Traversing from -2 uptill -6 with jump of -1.
print(emp_details[-2:-6:-1])
Traversing from -5 to -2 with a positive jump i.e. default 1.
print(emp_details[-5:-2])
We can use this approach to reverse the tuple.
print(emp_details[::-1])
Check if an item exists in Tuple
To check whether an item exists inside the tuple or not, we use the membership operator in.
emp_details = ('Brad','Lee','Sales',380,250000,True)
if ("Brad" in emp_details):
print("Welcome, Brad!")
As Steve is not present, so nothing gets printed.
emp_details = ('Brad','Lee','Sales',380,250000,True)
if ("Steve" in emp_details):
print("Welcome, Steve!")
Index out of range error
If we try to access any index position where no item is present, then we get ‘index out of range’ error.
If we try to access item at index 2, we get index out of range error.
emp_details = ['Brad','Lee']
print(emp_details[2])