In this video we are going to discuss lists and what they are in Python. So, if you have experience with programming in other languages, let's say PHP, you know to create an array, and what an array is. For those who do not have experience programming, an array is a way of keeping data organized and within a single construct. For single-dimensional arrays, we can implement it as a list in Python. To create a list in Python we use square brackets:
["Movies", "Games", "Python"]
This becomes a list that has 3 indexes. To call the first item in the list you write the following:
["Movies", "Games", "Python"][0]
Remember, when programming the first ID of the item in a list or an array will be 0. We can also concatenate a list item with a string.
If we were to change the index to number to 1 it would print out "I Like Games". So, that is what a list is in Python. It's just a way to create a collection under one variable. In the next video we will cover dictionaries.
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'ss 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.