Join Bill Weinman for an in-depth discussion in this video Operating on sequences with built-in methods, part of Python 3 Essential Training.
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.
- A Python 3 quick start for experienced developers
- Creating functions and objects
- Using Python's built-in objects and classes
- Repeating code with loops and iterators
- Understanding and using conditional expressions
- Creating sequences with generators
- Reusing code with objects and libraries
- Handling errors with exceptions
Skill Level Beginner
Q: The installation process for the PyDev Eclipse plug-in doesn’t work as described in the videos. What should I do?
A: The version of Eclipse used in the recording of Python 3 Essential Training was Eclipse 3.5.2 (Galileo SR2) and the version of PyDev was 1.5.7. Soon after recording, the Eclipse project was updated to version 3.6, called Helios. As of this writing, the current version of Eclipse is 3.6.1 (Helios SR1).
Around the same time as these updates were being released, the PyDev project was updated as well. As of this writing, the current version of PyDev is 1.6.2. If you are using these most recent versions, the procedure for installing Eclipse itself has not changed, but the process documented in the videos for installing PyDev may not work.
We will be releasing new versions of the videos soon, but the author has posted a document describing the new installation procedure at:
Q: How do you install the pydev-interpreter in Eclipse Indigo on Mac OS X Lion? It's significantly different than what's shown in the video "Installing Python 3 and Eclipse for Mac."
A: Since this course was publishing, there have been upgrades to Mac OS X and Eclipse. In this scenario, after installing Python and Eclipse and the Pydev interpreter, there is a different directory to go to when modifying the preferences. As instructed in the movie (at around 6:40) restart Eclipse and then go to Eclipse > Preferences and drill down to Pydev > Interpreter - Python in the sidebar. Click New and in the Select interpreter dialog that opens, click Browse.
The Open dialog box will open, but does not appear to display your hard drive. You must press and hold the Command+Shift+Period keys. This will display all hidden files on your system. Navigate to the new path ~/Library/Frameworks/Python.framework/versions/3.2/bin/python 3.2 and click Open.
You should be able to proceed normally from there.