Account
Categories

Python Tuple Slicing


Definition:

It means taking some part of a tuple. It gets some data from the tuple by using a range of index numbers.

Now you will clearly understand the operation through the flowchart .

Tuple →  t = (10, 20, 30, 40, 50)

Index :    0        1        2        3        4  
           ↓        ↓        ↓        ↓        ↓
Value :   10       20       30       40       50  

Slicing →  t[1 : 4]

Index Range →  [ 1 ----------- 4 )
                 ↑        ↑
            include     exclude

Selected Part →   203040

Syntax:

tuple_name[start:end]

Example:

t = (10, 20, 30, 40, 50)
print(t[1:4])

Output:

Exercise

Write Python code to print elements from index 2 to 4 using slicing.