Computer science
-
Introduction to Programs Data Types and Variables
-
Binary Numbers
-
Python Lists
-
For Loops in Python
-
While Loops in Python
-
Fun with Strings
-
Writing a Simple Factorial Program. (Python 2)
-
Stepping Through the Factorial Program
-
Flowchart for the Factorial Program
-
Python 3 Not Backwards Compatible with Python 2
-
Defining a Factorial Function
-
Diagramming What Happens with a Function Call
-
Recursive Factorial Function
-
Comparing Iterative and Recursive Factorial Functions
-
Exercise - Write a Fibonacci Function
-
Iterative Fibonacci Function Example
-
Stepping Through Iterative Fibonacci Function
-
Recursive Fibonacci Example
-
Stepping Through Recursive Fibonacci Function
-
Exercise - Write a Sorting Function
-
Insertion Sort Algorithm
-
Insertion Sort in Python
-
Stepping Through Insertion Sort Function
-
Simpler Insertion Sort Function
Python Lists Understanding the basics of lists in Python
⇐ Use this menu to view and help create subtitles for this video in many different languages.
You'll probably want to hide YouTube's captions if using these subtitles.
- In this video, I want to introduce you to the idea of a list,
- of a list in python.
- It is one of the most powerful data structures in Python,
- and it really is just a sequence of a bunch of other stuff.
- So a list in Python would look something like this:
- So this would be a list of integers,
- Maybe 1 , -7, 0, 0, 5 and 10
- And we can have some variable refer to it,
- so that we can refer back to it later on in the program,
- so maybe we say a is equal to all of this.
- And if we run this in the interpreter,
- or if we run this in a program,
- now we can refer to the elements of a,
- and you do that by, so let's say I have a
- (let me write in the same pink color)
- You do that by referring to the index of the element inside of the list.
- So, if I say a and then in brackets, I put a zero here.
- This says the 0th element in a,
- 0th elment...
- and the general convention in Python,
- and this is true of most programming languages,
- is what some people would consider the first item in an array, or inside of a list
- you would call it the 0th item, or the index of this item right here, is 0.
- So, if you were to type this in an interpreter,
- or if you were to type it in a program,
- it would evaluate as this item right over here.
- It would be this item, right over there.
- And let me do another example:
- If I were say a of..
- I shouldn't say a of...
- If I were to say the second element...
- in the second element in a
- this would be 0th element, first element, second element.. It would be
- this zero, right over there.
- So, that's how you refer to elements in a list.
- you can maybe already imagine doing interesting things with that.
- But, what is really cool with lists ...
- is that they don't have to all be the same data type.
- They could be all integers,
- or all floats...
- or all strings...
- but you could mix it up.
- So you could... you could...
- Actually, You could take a right over here,
- and you could redefine one of the elements
- so you could say...
- you could say a.....
- you could say let's reset......
- (do it in that same pink color)
- Let's reset the.......the...
- (I don't know...)
- let's say the fourth element..
- Let's reset the fourth element in a
- to be equal to...
- to be equal to.."this text"
- So literally "this text".
- Literally "this text"
- and so If you did that..
- if you set the fourth element be "this text"
- and then you and your program say print..
- print a..print a
- then a... would look like this.
- The 0th element wouldn't change.
- Still be 1
- The first element will stll be -7
- The...the...so this is the 0 first..this...this...this..
- This two index element would still be 0
- This will be the third.
- So this is 0th index.
- First, second, third, fourth.
- to replace the fourth element.
- So the fourth element will now be "this text"
- It will now be...this....will now be "this text"
- ...the second...we wanna try out with Python intrepreter
- and the last element will still be.....
- The last element will still be 10.
- and this will completely valid to do in Python.
- You can mix data type.
- In fact, you could even...if you..after..after you did this,
- you could even say..that..a
- Let's redefine the 0th element of a..
- to be another list.
- To be the list -1, -2.
- and if you did this, then this 0th element over here,
- will in the list, the 0th element will now refer to this thing.
- So the list would now look like this.
- So the first element or the 0th element I should say will be this thing.
- Negative 1, negative 2.
- And then you'd have you're negative seven, zero, zero, this text
- and then you'll have your 10, just like that.
- So you define it this way and literally replaces or the 0th element will now refer to this data structure
- over here and you could maybe already imagine useful ways to use this in programs
- and if you can't don't worry about it. We'll see useful ways.
- Now there is one thing I want to make very clear here
- is that, when I... Let's say that I have... Let me define some new...
- Let's say that I have some list that I have the variable b referred to
- and let's say I make this list: 7, 13, 15.
- So that's my list.
- And let's say that I... the next line in my program or maybe using the python interpetrator.
- I say that c is equal to b.
- And sometimes you might be tempted to do this is, hey I want to have a copy of b.
- And what I wanna make clear is that this point, c and b are actually pointing at the same thing.
- They are referring to the same thing, so if you think about what's happening.
- There are some entity in memory that looks like this: 7, 13, 15.
- And when you execute this, when you execute this first line over here that says
- that look, b is going to definetly refer to that.
- If the variable b refers to this entity and when you say c is equal to b,
- this is actually saying that c is going to refer to the same thing that b is referring to.
- So c is also going to refer to this exact same thing.
- C is not a copy of it, we didn't make a copy of it and make c refer to that.
- And what's relevant here is if we now change c we are also going to change or if we change
- what c is refering to we'll also change what b is referring to.
- And I'll show you that in a second with the interpetator, but let me show you what I'm talking about.
- So this point, I say that c...If I... the next line I say that c, the 0th element in c needs to be equal
- to 1.
- And if I were to tell you then print c you would get.
- You would get 1, 13 and 15.
- But if you were to say print b you would also get 1, 13 and 15.
- And that's because this call right over here or this statement right over here
- it changes what the 0th element in this list refers to.
- So it won't refer to 7 anymore it will refer to 1.
- So now if you say b of 0, this will be equal to 1 and not 7.
- So you might say:" Well Sal okay, I understand that, so if set a variable equal to another variable
- that refers to a list they are actually refering to the same list and if I change one of the items in
- the list that this is referring to is actually changing what this is referring to.
- So how could I copy lists?"
- How could I actually copy it.
- And there you use the notion that can actually and I'll write right over here if you actually want to
- copy things you would do this.
- You would write c is equal to b and this might look a little funny for you
- but you put this colon sign over here and what this does is it tells you copy everything in b
- from the beginning of b all the way to the end of the b.
- So you could write c is equal to b and then you could say 0 all the way up to but not including
- the second element.
- So this right over here, this would create a new list that is the 0th and only the first element.
- It won't include the second element.
- So in this case, what you do... So this one right over here would create a new list that looks like this.
- That creates... That looks like... If we assume we haven't changed b and if we assume this was
- our definition of b, then it will create a new list that looks like this and c will now refer to this
- new list.
- So c will now refer to this new list.
- So it created a copy of the first two elements, 0th element and first, not the seconth element.
- You would have to put 3 here if you wanted the second element,
- so up to but not including the second element.
- If you wanted all of the elements to copy you would do this.
- So this is simply saying from the beginning all the way to the end.
- So it would create... It would create a separate list...
- It would create a separate list that looks just like b, but it is a separate one.
- And then c could refer to that and now if you change c you won't change b.
- So enough of just talking about it, let's actually play with it on our interpetrator.
- Cuz this is the fun stuff right over here.
- So let's say that we have... Let's say that... Let me get rid some of this text that I have here
- So you don't get confused.
- So let's say I define some list.
- A is equal to 1, 2 -- I'll write this way -- 1, 2, negative 7, 9, 11.
- So I've defined and could even verify that.
- Print a, you can see it right there.
- We can change one of the elements in a.
- We can say... Let's change the first element and remember, when we say we have an index 1
- we are not talking about this one here this is the 0th element.
- So we are changing this one right over here.
- So let's change that.
- Let's just for fun... Let's change that to a string.
- So "Sal's String*.
- And now let's see what a looks like. Print a.
- So notice, it took this, this is the index one, this is the first, 0th element, 1st element.
- It changed the first element to "Sal's String" and we see it right there.
- Now let's do something interesting.
- Let's set b equal to a.
- And let's print... Let's print b. So it looks like a.
- And let me do something else.
- Let me... Let c equal to a and then do what I talked about, so let me make c equal to a copy of a.
- So let's print c. Print c.
- So b and c looked the same, but they are two different things.
- C is a copy of a or c is a copy of the thing that a is referring to.
- B is the thing that a is referring to and to see that, let's change the first element
- of both b and c.
- so if you change the first element of b.
- So b of... the 0th element in b. Let's just make that, I don't know. Let's make that equal to...
- Let's make it equal to 0.
- Now if we print b you see the first element is 0.
- And what you'll also see is that if you print a, the first element will be 0 now.
- Cuz they were referring to the same thing.
- So it said, whatever b is referring to make it, the very 0th element, make that equal to 0.
- Well a was referring to the same thing so it also became 0.
- But c is referring to a copy so c should not have changed.
- C did not change. The 0th element here did not change a bit.
- So I'll leave you there. Lists are super powerful, there's a ton of functions you can do
- with lists, In fact, I'll expose you to a few right here.
- So that is a, you can add elements to a.
- So you could say a dot append and you can add something to the end of a.
- So you could add "new element" and if you do that it should add it to the end of a.
- So let's look at a.
- It added that element.
- Well b is referring to the same thing so let's see what b looks like.
- It has a new element.
- C is referring to a copy of the old a, so let's see what that looks like.
- Didn't add it there, cuz it didn't add it to the copy.
- It added only to what a was referring to which is the same thing b is referring to
- and there is a ton of useful functions that we'll explore them as we do more and more
- programs, more more example problems,
- but there is a bunch of useful functions or methods that lists have to be able to manipulate them
Be specific, and indicate a time in the video:
At 5:31, how is the moon large enough to block the sun? Isn't the sun way larger?
|
Have something that's not a question about this content? |
This discussion area is not meant for answering homework questions.
Discuss the site
For general discussions about Khan Academy, visit our Reddit discussion page.
Flag inappropriate posts
Here are posts to avoid making. If you do encounter them, flag them for attention from our Guardians.
abuse
- disrespectful or offensive
- an advertisement
not helpful
- low quality
- not about the video topic
- soliciting votes or seeking badges
- a homework question
- a duplicate answer
- repeatedly making the same post
wrong category
- a tip or feedback in Questions
- a question in Tips & Feedback
- an answer that should be its own question
about the site
Share a tip
Suggest a fix
Have something that's not a tip or feedback about this content?
This discussion area is not meant for answering homework questions.