Python Programming - Common Tuple operations

You can learn about Tuples in Python Programs with Outputs helped you to understand the language better.

Python Programming – Common Tuple operations

Python provides you with a number of ways to manipulate tuples.

Tuple Slicing

If you want a part(slice) of a tuple, use the slicing operator [ ]. The first value in a tuple is at index 0. Similar to Python lists, you can use the index values in combination with square brackets [] to access items in a tuple. When using positive indices, you traverse the list from the left. For example,
numbers = (0,1,2,3,4,5,6)
numbers[0]
0
numbers[3]
3
You can also use negative indexing with tuples:
numbers[-1]
6
Indexing is used to obtain individual items, whereas slicing allows you to obtain a subset of items. When you enter a range that you want to extract, it is called range slicing. The general format of range slicing is:
[Start index (included):Stop index (excluded) :Increment]
Here, Increment is an optional parameter, and by default, the increment is 1. For example,
# Item at index 5 is excluded numbers[1:5]
( 1 , 2 , 3 , 4 )
# This provides all the items in the tuple
numbers[: ]
( 0 , 1 , 2 , 3 , 4 , 5 , 6 )
# Increment = 2
numbers[::2]
( 0 , 2 , 4 , 6 )
You can also use the negative increment value to reverse the tuple, numbers[: :-1]
( 6 , 5 , 4 , 3 , 2 , 1 , 0 )

Let’s Try

Type the following Python codes and write the out-put:

>>> grade_point= ( 10 , 9 , 8 , 7 , 6 , 5 , 4 , 0 )
>>> grade_point[ 2 : 4 ]
>>> grade_point=( 10 , 9 , 8 , 7 , 6 , 5 , 4 , 0 )
>>> grade_point[ : 4 ]
>>> grade_point[ 4 : ]
>>> grade_point[ 2 : 2 ]
>>> grade_point=(10,9,8,7,6,5,4,0)
>>> grade_point[ : -4 ]
>>> grade_point[ -4 : ]
>>> grade_point=( 10 , 9 , 8 , 7 , 6 , 5 , 4 , 0 )
>>> grade_point[ 4 : -4 ]
>>> grade_point[ -4 : 4 ]

Tuple Addition

You can combine tuples to form a new tuple. The addition operation simply performs a concatenation with tuples. For example,
x = ( 1 , 2 , 3 , 4 )
y = ( 5 , 6 , 7 , 8 )
# Combining two tuples to form a new tuple
z = x + y
print ( z )

>>>
( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 )
>>>

Let’s Try

Type the following Python code and write the out-put:

>>> t = ( 1 , 2 , 3 , 4 , 5 )
>>> t = t + ( 6 , )
>>> print ( t )

Hence, you can add any kind of element to a tuple, using the + operator.

Tuple Multiplication

The multiplication operation simply leads to repetition of the tuple by the given number of times. For example,
x = (1,2,3,4)
z = x*2
print(z)
>>>
( 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 )
>>>

Let’s Try

Type the following python codes and write the output:

a = ( 10 , 20 , 30 , 40 , 50 )
b = ( 60 , 70 )
c = a + b
print ( c )
a = ( 10 , 20 , 30 )
b = ( 60 , 70 )
print ( a * 2 )
print ( b *3 )

Deleting a Tuple

Since tuples are immutable, it is not possible to delete individual elements of a tuple, however, you can delete the whole tuple itself. In order to explicitly remove an entire tuple, the del keyword is used with the tuple name.
For example,
tuple1 = ( 1 , 2 , 3 , 4 , 5 , 6 )
print(tuple1)
( 1 , 2 , 3 , 4 , 5 , 6 )

del tuple1 [0] # not possible it will
raise error
print(tuple1)
Traceback (most recent call last):
File “C:/Users/shash/AppData/Local/Pro grams/Python/Python37-32/tup4.py” , line 5, in <module>
del tuplel [0]
TypeError: ‘tuple’ object doesn’t support item deletion

>>> del tuple1 # tuple1 will be deleted from the memory
>>> print(tuple1) # NameError because tuplel is deleted
Traceback (most recent call last) :
File “<pyshe11#2>”, line 1, in <module>
print(tuple1)
NameError: name ‘tuple1’ is not defined
>>>

Note an exception raised, this is because after using del tuple1 tuple does not exist anymore.

Leave a Reply

Your email address will not be published. Required fields are marked *