Slicing a Tuple


☞Tuple slices refers to part of a tuple containing some contiguous elements from the tuple.

Syntax :
<tuple> [beg index : end index : step]

Slicing a Tuple Using Slice Operator [ : : ] :

Example :
T = (10, 20, 30, 40, 50, ["A", "B", "C", "D"], (100, 200, 300, 400), 60, 70 )
print(T[2:7])
print(T[2:15])
print(T[2:-2])
print(T[-5:7])
print(T[-5:20])
print(T[-2:7:-1])
print(T[1:8:2])
Output :
(30, 40, 50, ['A', 'B', 'C', 'D'], (100, 200, 300, 400))
(30, 40, 50, ['A', 'B', 'C', 'D'], (100, 200, 300, 400), 60, 70)
(30, 40, 50, ['A', 'B', 'C', 'D'], (100, 200, 300, 400))
(50, ['A', 'B', 'C', 'D'], (100, 200, 300, 400))
(50, ['A', 'B', 'C', 'D'], (100, 200, 300, 400), 60, 70)
()
(20, 40, ['A', 'B', 'C', 'D'], 60)

Reversing a Tuple Using Slice Operator [ : : ] :

Example :
T = (10, 20, 30, 40, 50, ["A", "B", "C", "D"], (100, 200, 300, 400), 60, 70 )
print(T[8:0:-1])      #10 will not include
print(T[8::-1])
print(T[len(T)::-1])
print(T[::-1])
print(T[20::-1])
Output :
(70, 60, (100, 200, 300, 400), ['A', 'B', 'C', 'D'], 50, 40, 30, 20)
(70, 60, (100, 200, 300, 400), ['A', 'B', 'C', 'D'], 50, 40, 30, 20, 10)
(70, 60, (100, 200, 300, 400), ['A', 'B', 'C', 'D'], 50, 40, 30, 20, 10)
(70, 60, (100, 200, 300, 400), ['A', 'B', 'C', 'D'], 50, 40, 30, 20, 10)
(70, 60, (100, 200, 300, 400), ['A', 'B', 'C', 'D'], 50, 40, 30, 20, 10)