Operate on sequences with built-in methods Python 3
Operating on sequences with built-in methods
Tuples and lists are very powerful in Python and there are a lot of operations that you can perform on them. Let's go ahead and set up a couple of tuples and lists. We'll have a tuple we'll call t and we'll initialize it with a range. So, there is our tuple and we can see type(t) is class tuple. So, I can check and I can say is there 10 in there. I can say 10 in t and I'll get it True. If I say 50 in t I get a false.
Of course, you can also use not in. I can say 50 not in t and that will be True. I can look at a particular entry. I can say well what is that index 10. I can say t10 like that. I see because a range was 0 base that at position 10 is the number 10. Of course, I can look at the length of it and a tuple is iterable. I can say for i in t and I can print(i).
Likewise, you can do all of these things with lists. x= list(range(20). I am missing a parenthesis there. So x is now that list and I can 10 in x and I can get true. I can 20 in x and will get false because that range was not inclusive and I can iterate it. I can say for i in x: print(i) and that's iterable as well. Of course, one of the major differences is that I can assign things to my list and cannot assign things to my tuple.
My tuple is immutable. So I can't say t sub 10 = 25. tuple object does not support item assignment. A tuple is immutable. But I can say x sub 10 = 25 and when I look at x now I've got that 25 in the middle of it. Likewise, I can do any operation on a tuple that does not involve modifying it. I can use the count method to count how many 5s there are in the tuple and there is of course one of them.
I can find the index of the 5. That will find that index of the first occurrence of that value and it's index number 5. If we look at t here we see 0, 1, 2, 4, 5. So that one there is indeed a 5. But I cannot append. If I say t.append(100), 'tuple' object has no attribute 'append'. On the other hand, if I look at my x object, which is a list, and I say x.append(100), it will put 100 at the end of it.
The length of my x object is 21 instead of 20. I've actually extended it because I've added something to the end of it. If I want to add a number of things, I can't do that with append but I can do it with extend. This will allow me to extend it with any iterable object, so if I say (range(20)) now I'll get another 20 objects at the end of it. So, that range has been appended to the end using the extend method.
If I want to insert something, I can say x.insert and I can insert at the beginning if I want to and I can put a 25 there and now we look at x. We've again extended the length of the array and there is a 25 at the beginning. But I can insert any place I want to. I can say x.insert(12, 100) and we'll see that I've got 100 there in the middle of the list.
Likewise, I can remove it. x.remove(12) and that will remove the element with the value 12. It actually looks for the first value of 12 and removes that. If I want to delete the item at index 12 I use the delete operator and say x12 and that will delete that element at index 12. So, we see that 100 that we added in and we inserted at 12 is now gone.
Finally, just like I can insert things at the beginning or at the end I can pop to remove things. If I just say x.pop, it will give me the value from the very end of the list and it removes it. So, you see that 19 is now gone. I got it returned from the pop method and now it's gone. If I want to, I can pop from the beginning I can say x.pop(0) and that will pop from the beginning.
Now, I got that 25 from the beginning and the list is shorter by 1 and that's been removed from the list. Basically, we can do anything we want to with these lists, as long as we are using the mutable list object. So, all the power is there. You've got the equivalents of push and pop and insert and remove. You can remove things based on their value or based on their index, you can search for things, you can count the occurrence of things. All of this power is here for both the tuple and the list, as long as you are not using the things on the tuple that would modify it. Because you have to remember the tuple is immutable and the list is mutable.
Up and Running with Python1,102,871 Views
Perl 5 Essential Training51,856 Views
JavaFX GUI Development22,465 Views
Python GUI Development with Tkinter223,863 Views
Overview of IDEs for Java13,301 Views
Foundations of Programming: Fundamentals7,748,469 Views
PHP with MySQL Essential Training4,274,773 Views
Git Essential Training2,854,511 Views