Lists
- 04:05
Understand how lists are used in Python.
Downloads
No associated resources to download.
Transcript
During this course, we're going to teach a lot of concepts through Excel. And the reason for that is because you already know a lot of things that are taught when somebody is studying computer science. Even if you don't have a computer science background, the fact that you're familiar with Excel means that you already have a body of knowledge that we can build on so that you can learn Python coding even faster with a lot less effort. Let's start by looking at lists as we would build them in Excel. So a list is an object containing a group of other objects that you can arrange in a specific order. In Excel the equivalent of a list might look like what you have here, it can organize in this case, we have a list of integer that are in a specific order, and we have a list of strings. These letters that we have arranged in a specific order. In Python lists are formatted in a very specific way. Let's go ahead and look at how we do that.
So here we have both of the lists that we just saw in Excel in Python format, and what's important is we have that unique name first, and then the equal sign, which tells Python we're about to define something. And then the objects in our list are organized in order within square brackets. So here we have these integers one through five and strings A through E. What's super important is that these lists are contained within square brackets. If you don't use that symbol, it's not a list. And you can think of it like the left bracket is a capital letter L. That's a trick that I like to use and that's gonna help you remember to use square brackets whenever you're defining a new list. So if I execute this code cell, now list one and list two are defined. So if I wanna print that out, Those lists have been defined and I can now print them out as an output. A list can contain multiple types of objects. You're not restricted to a single object type. So let's say that I wanted to create a variable called pi, and then I wanted to create a list. Let's call it list three.
So I'm gonna open that with square brackets in that list, I'm gonna have a integer. Let's make a float. I'll include my variable pi. I will compare pi to 0, so that's a Boolean.
And let's add a string as well.
So there's my list, open and close with square brackets. Let's go ahead and print it out. And there it is. So a list can contain multiple types of objects, including other lists. Let's go back to Microsoft Excel and take a look at what a list of lists would look like.
Okay, so in Excel, a combined list would look something like this, and all we've done is we've created a matrix with five rows and two columns. This should seem very basic and very easy to you. Let's look at how it looks in Python.
In Python, that same combined list is going to look like this. We have one list enclosed in square brackets, and then within that there are five smaller lists, each with two objects. Now that's going to represent our five rows or these five lists. And the two columns are the two objects within each of those five lists. If I execute that and then print it out, it looks like this.