Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
Tuples and lists are the basic array types in Python. tuples are immutable and lists are mutable. tuples are created with the comma operator. Say t = 1, 2, 3, 4, 5 like that, and I get a tuple with those five elements in it. So, element number 0 is the beginning of the list and element 4 is the end of the list. Of course, you can always get the end of the list with -1, like that.
I can take the length of the list, I can get the smallest element, I can get the largest element and so I have a tuple. There is some confusion about how you create a tuple in Python. Sometimes it looks like you use the parenthesis to create a tuple, because more often than not you'll see it done like this to create the tuple, but in fact, it's the comma operator. This is the important distinction because if you want to create a tuple with one element, that won't do it.
That creates an integer.
The comma is required. The parentheses are for binding, and when you have a lot
of elements separated by commas, the parenthesis are convenient for that and
that's why you'll often see it that way.
But in fact, if you want a tuple with one element, you create it like this.
And now, we have a tuple. type(t) is
Lists work very much like tuples in that I can subscript it in the same way or I can look at the last one and I can check its length and I can get the largest element and the smallest element. The difference is, is that I can change things. If I take my tuple and let's put something bigger in the tuple. Now I have a tuple with 25 elements in it and that is a tuple.
Let's say that I try to change one of those elements.
I get this error, 'tuple' object does not support item assignment.
Tuples are immutable, so I can't change things in them.
On the other hand if I create a list, I am using the list constructor here to
convert this range into a list and there is my list and type(x) is
Now, we see that 42 right there in the list. So, a list is mutable and a tuple is not mutable. So, why would you want to use a tuple when you can use a list and the list is more powerful and has more functionality? Tuples are there because more often than not, when you them you don't need to be changing them. It's a smaller class. It's simpler to implement, they are faster for some things, but most importantly they don't allow you to change stuff.
So, if you have a circumstance, and most circumstances are like this, where you need an object that's always going to stay the same size, it's always going to have the same data in it, you want to use a tuple so that you can't accidentally it, and if you do, you'll get an exception and you'll be able to find your problem. So, use your tuples where you can, and where you absolutely need to be able to change your array, that's where you use a list.
Find answers to the most frequently asked questions about Python 3 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.