image
image
image

Slicing

image

The slicing operator, the full colon is used to access a range of items in a tuple.

Example

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

tuple_mine=[‘t’,’r’,’o’,’g’,’r’,’a’,’m’]

print(tuple_mine [2:5])  #Output: ‘o’,’g’,’r’,’a’

print(tuple_mine[:-4])   #’g’,’r’,’a’,’m’

Point to Note

Tuple elements are immutable meaning they cannot be changed.  However, we can combine elements in a tuple using +(concatenation operator). We can also repeat elements in a tuple using the * operator, just like lists.

Example

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

print((7, 45, 13) + (17, 25, 76))

print(("Several",) * 4)

Point to Note

Since we cannot change elements in tuple, we cannot delete the elements too. However removing the full tuple can be attained using the kwyword del.

Example

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

t_mine=[‘t’,’k’,’q’,’v’,’y’,’c’,’d’]

del t_mine