We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
The next important operation on tuples is deleting a tuple, okay?
And that's very, very easy
to do. So we can delete a tuple using simply the del keyword here, okay? When you say delete t, it deletes the whole tuple. We cannot change individual items or elements in a tuple. We cannot remove an individual element because remember, tuples are immutable. Because tuples are immutable, which means you cannot edit or you cannot remove a specific item from a tuple. You can delete the whole tuple if you choose to, but you can't remove a single item. That's not possible. Okay, so the next important operation is count. This is exactly like the count operation in list that we saw. Imagine if I have a tuple like this, 1231-3341 if I say t dot count one, it counts how many times one exists. One, two, three. So it prints three, okay? So count basically gets the frequency or the number of times an element occurs in a tuple. Okay? So the next thing is, there is a function called index. Suppose if I have a tuple like this. If I say t dot index three, it prints, or it returns the first element, the index. So this is index zero, index one, index 234567. Here. If you notice, there is three here, there is three here, and there is three here. But Tuple, dot index returns the index of the first time this element occurs, which is two. So it prints two here. Okay? So it only returns the index of the first element the first time. This is equal to this. Now, what about tuple membership? Again, we have the keyword in, right, that we learned when we were learning about lists. So if I say one in t, it returns me a boolean saying whether one exists in t or not, right? So if I say print one in t, since one exists in metupol, it returns true. If I say seven in t, seven is not there in metupple, so it will return false. And you can print. And we just printed false here, right? So in and not in and not in can be used just the way we used it in lists tuples. Also, we can use the same functions, same operations or same keywords. So there is also tuple length, just like a list length. There are a bunch of built in functions for tuples, like length, length basically prints how many items are there, the length of the list, the length of the tuple. Here we have the same sorted. We can use sorted here to sort. Of course you can't edit. Remember, lists are immutable. That's very, very important. So if you have a tuple like this, not list. Sorry. Tuples are immutable. Keep getting confused between these two. I'm extremely sorry. So here is a tuple. If I say sorted t, it creates a new tuple with all the elements in sorted order, in ascending order, actually. Okay. But you cannot modify a list because. Sorry. You cannot modify a tuple because tuples are immutable to start with. Okay. You can also get the smallest and largest elements by using simple operations like this. You can say, if I have a tuple like this, if I can say max of t, it returns me the largest element. Min of t will return me the smallest element. Sum of t will sum up all the elements in the tuple and return the return the sum. These are all simple functions. Remember, there is a lot of similarity between what we learned in tuples and lists, except for the most important difference, that we cannot change the elements in a tuple, while we can change the elements in a list. Okay? So that's the fundamental difference, except that most other things almost very, very similar.