Posted on

How to use lists in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright, so let’s discuss lists and what they are in Python. So, let’s open up terminal again and launch a Python 3 console. So, if you do have experience with programming in other languages, let’s say PHP, you know how to create an array and what an array is. For anybody else it’s a way to to keep data organized and within one construct. So we’re going to go ahead and define an array and how we do that Python is just open up and close square brackets, and then in them we’re going to have some items and those items are going to each have their own index number within the array. So, let’s say we’re making a list of things that I like. I’m going to type “[“Movies”, “Games”, “Python”]” Now what this becomes is a list that has three indexes and we saw earlier when we split a string apart that created one of these and so you guys already know how to access items within this array, however if you forgot we’re going to go ahead and do that. So, basically I can put an index of 0 “[0]” and it’s going to print out movies. Now if we put that in a print statement, we’re going to concatenate it, “print(“I Like” +” Alright, we’re gonna get there so…let’s go ahead and print this out it says “I like movies” if we change that index number [1] it’s going to say “I like games” and if I change that to [2], oh not 3, so “I like Python” as well. Now what happens if one of these are a number? Let’s say I replace this with the number 16, let’s say “I like 16” go ahead and change this index to “[1]” then hit enter. It can’t convert the integer to string. So what we would do is if we were trying to print, and this specifically printing out an array and then choosing an index that time of recreation or list, that’s impractical and we’re never going to do things that way. So, what we just saw this error message doesn’t really matter. Basically again if you’re trying to concatenate strings with an integer it’s gonna freak out because it doesn’t know how to do that. So that is what a list is, it’s just a way to to create a collection under one variable name and when we discuss variables we will revisit this so you guys can see what I mean. In the next video we’re going to be talking about dictionaries, and basically these are like in JavaScript you can create JSON objects and that’s basically what they are. So, let’s get into that.