Python | Tutorial 9 | Tuple

Posted on Posted in Software

In the previous tutorial, we covered the part 2 of Exception and discussed finally statement, raise exception and assertion. In this tutorial, we will be covering tuple. Let’s get started.

SUBSCRIBE to our channel if you haven’t. 🙂

As you know, List is an object that is used to store an indexed list of items. A Tuple is an inbuilt function in Python that is very similar to a list except that they are immutable i.e. they cannot be changed. Lists are created using square brackets but tuples are created using parentheses. Similar to a list, you can access the value in the tuple with their index. However, if you try to reassign a value, it will give TypeError as it is a tuple and not a list.

Tuple1=(‘Electronics’, ‘Python’, ‘Arduino’, ‘Raspberry Pi’)
print Tuple1[1]
Tuple1[1]=”DIY” <- // Reassigning value to the index 1 element of Tuple1

Output:

Python
Traceback (most recent call last):
tuple1[1]=’DIY’
TypeError: ‘tuple’ object does not support item assignment

Tuples can be created without the parentheses, by just separating the values with commas. Now in this tuple, you can see we have assigned numbers as values. With this, you can get max and min.

Tuple2=23,12,-5,-16,54
print max(Tuple2)
print min(Tuple2)

Output:

54
-16

You can also obtain the length of the tuple just like you did in the list by using the inbuilt function len. You can also add two separate tuples like this.

Tuple1=(‘Electronics’, ‘Python’, ‘Arduino’, ‘Raspberry Pi’)
Tuple2=23,12,-5,-16,54
print len(Tuple1)
print len(Tuple2)
print Tuple1+Tuple2

Output:

4
5
(‘Electronics’, ‘Python’, ‘Arduino’, ‘Raspberry Pi’,23,12,-5,-16,54)

There is a way to update your tuple. First, you need to convert it to list using inbuilt function list like this. Then perform the required update or modification, like appending a value since it is now a list. If you want to change it completely, then don’t forget to use 3rd bracket because it is now a list. And then reconvert it back to tuple using the inbuilt function tuple like this. Pretty neat, isn’t it!

Tuple2=23,12,-5,-16,54
newTuple2 = list(Tuple2)
print newTuple2
newTuple2.append(63)
print newTuple2
Tuple2 = tuple(newTuple2)
print Tuple2

Output:

[23,12,-5,-16,54]
[23,12,-5,-16,54,63]
(23,12,-5,-16,54,63)

If you want to check whether a particular value is present in a tuple, then you can use the inbuilt function in. Suppose if I write DIY is present in Tuple1, followed by a comma and write DIY in Tuple1, then you can see it printing false which is obvious. Likewise, since 24 is present in Tuple2 hence it will print true.

Tuple1=(‘Electronics’, ‘Python’, ‘Arduino’, ‘Raspberry Pi’)
Tuple2=23,12,-5,-16,54
print (“DIY is present in Tuple1: “,”DIY” in Tuple1)
print (“24 is present in Tuple2: “,24 in Tuple2)

Output:

DIY is present in Tuple1: False
24 is present in Tuple2: True

A bad thing about tuple is that you cannot remove a particular value from it. It removes the tuple completely. So you need to convert it to list, do that deletion and again reconvert it back.

So that’s it for today and I hope you have enjoyed this tutorial. If yes, then leave a thumbs up in our video and don’t forget to subscribe if you are new. Click the bell icon so that you receive the notification for the next video. Follow us on social media for more updates. Peace. 🙂

Spread it!

Leave a Reply