Write a Python program to slice a tuple


The program defines a tuple a containing 10 integer elements. The print() function is used to output sub-tuples (slices) of the original tuple a. The slicing of a tuple a is done using the syntax a[start:stop], where start is the starting index (inclusive) and stop is the stopping index (exclusive) of the slice.

  • The first print() statement outputs a slice of the tuple a starting from index 3 (inclusive) up to index 8 (exclusive), resulting in the values (40, 50, 60, 70, 80).
  • The second print() statement outputs a slice of the tuple a starting from index 5 (inclusive) up to index 9 (exclusive), resulting in the values (60, 70, 80, 90).
  • The third print() statement outputs a slice of the tuple a starting from index 0 (inclusive) up to index 3 (exclusive), resulting in the values (10, 20, 30).
  • As a result, the output will show three slices of the original tuple a, each slice representing a sub-tuple of the original tuple.

Source Code

a = (10,20,30,40,50,60,70,80,90,100)
print(a[3:8])
print(a[5:9])
print(a[0:3])

Output

(40, 50, 60, 70, 80)
(60, 70, 80, 90)
(10, 20, 30)


Example Programs