Placeholder Image

字幕表 動画を再生する

  • Hey, CSO.

  • So in this video, I'm gonna introduce you to lists in Python.

  • The list is a type of data, just like strings and integers that we've seen so far on its used to store a list of things, and it's similar to a raise in some other languages, like Java.

  • So let's take a look at one example here to define a list you can just write a eco's open scar bracket.

  • Three comma tang comma minus one close square brackets on this line means define a new list with the elements 3 10 on minus one on assigned this list to this viable eight.

  • And just a quick note here that you can download this simple file by going to see a snow that io slash python for.

  • On another quick reminder that you don't necessarily have to use Jupiter.

  • No book to follow this course.

  • You could use, for example, part charm, which is a good idea.

  • He for Python.

  • Now let's run the cell on.

  • Once we run the cell, A should be defined, so let's print a with print.

  • Prentice is a and we see that we get a list with the elements.

  • 3 10 on minus one.

  • What if we wanted to add an item to this list?

  • You can do that with a dot upend parentheses.

  • One.

  • This means a pen.

  • The item, the number one to the list.

  • Eight.

  • So this dot notation is a pretty common one on you.

  • See more of that later in this course.

  • So this function a pen is basically a predefined function that you can use on the list data type.

  • So again, that's a dot a pen parentheses.

  • One.

  • Let's execute this cell on.

  • Once we print A, you see that the number one has been added to this lists.

  • So one interesting thing about the Python list, which is different from some other languages like Java, is that you can mix types in a single list so you could have a list containing numbers as well as a string.

  • Let's see how that works by a pending the string Hello to the list A ways.

  • A duck, a pen, parentheses, double quotes.

  • Hello.

  • Let's print that with Prince A.

  • Execute this so and you see that this list now contains hello.

  • The string, as well as the numbers lists, could even contain another list.

  • So let's see how that works with ADA.

  • A Penn Square brackets, one comma two.

  • So this maze create a newest with the Elements one and two, and they appended to this list.

  • Let's see how that looks with print.

  • Francis is a Let's run this cell and now you see that we have a bunch of numbers and then string, which is hello on another list within this single list.

  • Okay, what if you wanted to delete on items from this list, for example, that last item to delete the last item you can do a dot pup Prentice's.

  • So we're using the dark notation again.

  • Here on pop is another function that's predefined for the lists data type.

  • And that's why we can just write a dark pop open currencies close, parent sees, and to see the result.

  • You can, of course, print a Let's run this so and you see that a now has the numbers on the string, but not the list, because that's popped or deleted.

  • Let's delete the string as well.

  • This hello strength with Ada Pup, which do It's the last item off the list, of course, And then let's print a and you see that it now has numbers on Lee.

  • Now, there are many other predefined functions other than pop.

  • Add a pen on the list.

  • There a type but a pen on pop are two of the most common ones.

  • Okay, What if you wanted to retrieve a specific item from this list?

  • For example, the first item, the second item or the third item.

  • To do that, you can just write a scar buckets zero.

  • And this means retrieved the item from the list A with the index there on in python, just like in many other languages.

  • The index starts at zero.

  • So the first item in the list has the next era on the second item has the next one.

  • The third item has the next two and so on.

  • Okay, so we want to print this item just to make sure we got the right one.

  • But actually interpret a notebook.

  • You don't have to use the print statement every time you could just write a scar buckets they're here on.

  • When you run this cell, you get the first item in the lists.

  • But to keep it consistent on also to make sure it works in any other platform.

  • For example, part charm.

  • I'm gonna use the prints they meant every time.

  • Okay.

  • What if you wanted to retrieve the fourth item?

  • This number one.

  • To do that, you can just do a score buckets.

  • Three.

  • So the fourth item has the next three.

  • And then let's prints that on, we get one.

  • And what if you wanted to change the content of this list?

  • So what if you wanted to change this number 3 to 100?

  • To do that, you can first select that item with a score.

  • Buckets Zero.

  • That's the first item in the lists.

  • And then you can assign a new number or any new value to it with a scar pockets there.

  • Eco's 100.

  • Let's run this cell on there.

  • Let's print a again with print princess, eh?

  • And you see that the first value of this list is 100 now instead of three.

  • Okay, Now I'm gonna give you a quick exercise problem to practice where you have learned so far.

  • Let's say you have a list with three strings inside.

  • So you have b equals square brackets, double quotes, banana comma, double coats, apple comma and then Microsoft.

  • Can you swap the first value off this list?

  • Who's index is off course zero with the last item off this list.

  • Before I show you my solution to this problem, I'm gonna show you a model you can use for thinking about a list in Python when you have B equals banana Apple, Microsoft.

  • The way you can think about it is that B is going to be sort of like three boxes put together or one giant box, one long box with three compartments.

  • And each of those compartments acts like a variable, so each of those refers to a value.

  • In this case, the first compartment.

  • We're first to banana, and the second element refers to Apple.

  • And the third element, of course, refers to the string Microsoft on.

  • I'm gonna show you my solution first, right Temp Eco's Be Square bug.

  • It's zero.

  • So we're defining a new viable called temp for a temporary viable, which is going to refer to what be square bracket zero refers to, which is this one.

  • And then you can write B square bracket zero eco's be scar buckets, too.

  • So we're saying the first compartment should now refer to whatever the third compartment refers to because, like I said, the index for the first item is zero on the index for the second item is one on the index for the third item is too.

  • So after this line, the first compartment or the first element will now refer to what the third compartment refers to, which is Microsoft.

  • And once you have that you can write, be score buckets to equals.

  • Temp on the third compartment now will refer to what temp refers to, which is banana.

  • As a result, after these pieces of code, we have the desired result.

  • So that's a list with the first compartment referring to the violin, Microsoft on the second element being Apple and the third element being banana.

  • The string.

  • Okay, let's make sure that method works in code.

  • First, print the current list with print be and we have Banana, Apple and Microsoft, just like we saw, will first define a temporary, viable Where's temp Eco's be scar Bucket zero and then a sign the value off B Square brackets to to be scrapper gets zero and then assign what temple refers to to be square brackets, too.

  • And then at the end, we're going to print.

  • Be okay, that's correct.

  • We have Microsoft Apple Banana now, and actually there's a shortcut for this, and that's be scrubber.

  • Could zero comma be square brackets to Eco's Be scar buckets to comma?

  • Be square buckets.

  • Zero.

  • Let's see if that works by Printing B and once we print be the first element on the last element should be swapped again.

  • So we should get the same list as the 1st 1 Banana Apple on Mike Soft on.

  • That's why we get so this is definitely a valued away to swap two viable zor two elements within the list.

  • But don't worry too much about this particular method.

  • Okay, just a quick reminder.

  • You can go to see sojo dot io slash piles on four to download the simple file on.

  • The best way to support me would be through my patron page on.

  • You can find that at sea.

  • Sojo dot io slash p a t.

  • Okay.

  • I'm working from C s soldier on.

Hey, CSO.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

Pythonでのリストの紹介 (Pythonチュートリアル #4) (Introduction To Lists In Python (Python Tutorial #4))

  • 7 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語