How can I do Python Tuple Slicing?

PythonServer Side ProgrammingProgramming

Slicing operator can be used with any sequence data type, including Tuple. Slicing means separating a part of a sequence, here a tuple. The symbol used for slicing is ‘:’. The operator requires two operands. First operand is the index of starting element of slice, and second is index of last element in slice+1. Resultant slice is also a tuple.

>>> T1=(10,50,20,9,40,25,60,30,1,56)
>>> T1[2:4]
(20, 9)

Both operands are optional. If first operand is missing, slice starts from beginning. If second operand is missing, slice goes upto end.

>>> T1=(10,50,20,9,40,25,60,30,1,56)
>>> T1[6:]
(60, 30, 1, 56)
>>> T1[:4]
(10, 50, 20, 9)

Published on 15-Jan-2018 00:28:24