For Python slicing for a sequence[start:stop:step], have derived these rules:
- start:stop = start:stop:1
- start:stop:(+ or -) step - It means when traversing skip N items in the sequence. However, (-) indicates backward traversal
- Remember, position of last item in sequence is -1, and the one before than is -2, and so on..
# start:stop: +step Rules
- Always traverse in forward
- Always start from beginning of sequence as its a positive step ( forward )
- Start at requested position, stop at requested position but exclude the item stop position
- Default start: If start is not provided, start at 0
- Default stop: if stop is not provided, it means until the end of the sequence including the last value
- If item at stop position is not reachable (item is beyond the end of sequence traversal), slice does not return anything
# start:stop:-step Rules
- Always traverse in reverse
- If start position is provided, start from there, but traverse in reverse ( its a step back )
- If stop is provided, stop traversing there but exclude this
- Default start: If start position is not provided, start position is the last position of the sequence ( since negative traversal)
- Default stop: If stop is not provided, it is the beginning of the list ( position 0)
- If item at stop position is not reachable (item is beyond the end of sequence traversal), slice does not return anything