- Do you know what's more fun than Python lists?
Nothing. Not even "Elden Ring," maybe.
But seriously, in this video,
we're gonna level up those Python list skills.
We've already seen how we can build a list
in the previous episode,
but now we're gonna add things to it.
And it's this crazy fun thing called methods.
Hmm, what is that?
You're gonna find out.
So get your coffee ready
or refill your coffee
'cause I've already had like four cups
and let's get started.
(host sipping coffee)
(upbeat music)
Okay, quick sip of coffee
before we launch our lab. (sipping coffee)
If you haven't already,
check that link of the description
I've got a free Python lab
that you can use right here in your browser, right now.
So go ahead and launch it, right now.
It's free. And while that lab is launching
can I show you something really cool
I've been working on?
Check this out.
I've been watching the Python For Security course
on IT Pro TV and check this out.
I learned how to build my own port scanner in Python.
Sure, you can use pre-built port scanners
but building your own,
that's just cool.
If you wanna try it, check it out,
link below,
itpro.tv/NetworkChuch
which by the way IT Pro TV is a sponsor
of this entire series
and the reason it can be free here on YouTube
and not to mention,
they are my go-to IT training provider.
I use them to learn stuff.
And literally whatever you wanna learn in IT,
you can learn it at IT Pro TV
from just getting your start with foundational skills
to more advanced things like Linux or the CCNA,
or the DevNet Associate, the CCMP,
and of course, tons of Python.
And you'll also able to stay awake during this training.
That's one of the features.
I'm glad they baked that in.
And of course they have virtual labs
so you can practice all that stuff you're learning
and practice tests,
so you can get ready for any exam you might be taking.
So if you wanna learn IT like me,
check it out, link below.
itpro.tv/NetworkChuck.
If you use code "NetworkChuck"
you'll get 30% off
Forever.
So, so here is our gorgeous Python script
because hey, we're going camping.
You're coming, right?
Yes you are. In order to go camping, you need stuff,
supplies to bring with you.
And we did that in Python.
We put together an amazing Python list,
full of the stuff that we are bringing.
But you forgot something.
And I can't believe you forgot this.
Come on, get your head on straight.
But seriously, we forgot toilet paper.
Can you imagine what a disaster that would be?
And actually I'm going to bring a bidet
because I'm fancy.
So that that'll be for me,
you can use the toilet paper.
So now our task in this video
is to somehow, with our list already created,
we gotta add toilet paper to our list
and also a bidet for me,
'cause I'm I'm bougie.
And this is just one of the many great things
about Python list because we can change these.
We can add things if we forget stuff,
'cause I forget things all the time.
And apparently you do too, thanks a lot.
I'm just kidding, by the way.
Don't get scared or upset.
I'm sorry. Anyways, here we go.
So our task of the day
is to add toilet paper and a bidet to our list.
Now I know what you're thinking.
You're thinking, "Well, Chuck can't we just go over here,
"in our list and edit the list manually."
Like just go over here and go,
"Toilet paper,
"they're done."
No, that's lazy.
We have to do it programmatically, using Python.
Don't do that.
So we're not gonna do that.
We're gonna use a powerful Python concept.
In fact, new concept time.
It's a thing called methods.
Professor Bernard, go ahead and tell us what methods are.
- [Bernard] A method in Python
is somewhat similar to a function
except it is associated with object/classes.
Methods in Python are very...
- Okay, Bernard, you're done.
You're putting people to sleep.
I'm sorry guys.
Anyways, let's stop talking about methods
and let's actually use one
to add toilet paper and a bidet to our list.
Let's do it.
It's it's really cool, actually.
This is actually a groundbreaking concept.
You're gonna use a ton in Python not with just lists.
So as you learn this,
you're getting a head start to everything else,
which is pretty cool.
Anyways, here we go. So down here, type this with me.
Our list up there,
supplies, go and type it out.
Supplies
Here comes the method.
I'm gonna do a dot,
and then type in a pen.
Notice, ooh, pretty different color.
When colors change in Python
we know somethings happening.
Something's cooking and brewing
and that's what's happening here.
So just after append, I'm gonna do a parenthesis and
inside those parentheses, I'm going to add our toilet paper.
Let's go ahead and do it. We'll do a string
and we'll type in toilet paper and that's it.
So let's go and just test the script
out before I explain what happened here.
So down here let's go and print supplies once more
just like this and let's run our code, run.
Look at that.
Okay. So, some magic just happened.
Our list now at the very end has toilet paper on it.
Thank goodness for you.
I'm using a bidet.
So let's analyze what happened here.
This "append" thing is called a method.
A method is a built in Python tool or
or function and specifically for list.
It allows us to change things up with our list.
So with our append method, obviously we're appending
or adding an item to our list at the very end.
And you probably assumed and you assumed correctly, sir
ma'am, that the, there are other methods
and we can do different things to our list
which we will explore.
But real quick let's have you try it yourself.
So here we add a toilet paper.
You, I need you to carry it for me, please.
Can you please add a bidet to our list just underneath that
pause the video, unpause, coffee break.
So just under adding toilet paper
I'm gonna type in supplies
dot append, once more using our pen method parentheses
and then I'll put in bidet, let's run the code.
Yes. Just like that.
Not only do we have a toilet paper
but now we have a bidet for "Network Chuck"
Which by the way, I'm not even kidding about that.
I've got a bidet in every bathroom in my house.
If you're not using a bidet what are you even doing?
Okay. Off my soapbox.
Now some of you may have tried this
and it wasn't a bad thought.
You're probably thinking, Hey, we don't need an extra line.
I'll just add two in one append method.
Right? Makes sense to me.
But if you run our code
it doesn't work. Using the append method,
you can only add one item
one piece of data at a time, which honestly yeah.
Is kind of annoying.
So is there a way around that?
Can we do something else?
And the answer is yes
there is one other way we can do this.
And this one is very handy.
So me back this
up really quick I'll type in supplies once more.
And it's another method, who would've saw that coming.
Right? And this one is called extend E-X-T-E-N-D.
Did I say that? E-X-T-E-N-D.
There we go. And then of course our parentheses. Now here
we're not adding one piece of data.
We're adding multiple pieces of data
which will come in the form of a list.
We're actually gonna add another list to our list.
Let's try it out.
And actually I want you to do it real quick.
So you know, our task bidet and toilet paper, add that list
Using the extend method.
It's kind of intuitive, so try it out.
Pause the video unpause, coffee break.
Here's how you would do it here, inside our parentheses.
I'm gonna create another list just inside there.
And I'm not actually creating a list.
I'm just telling it to add this list.
If that makes sense.
Okay. So brackets then a string.
I'll say toilet paper and then comma
one more piece of data, my bidet.
Can't forget that.
That should do it.
Now. Let's run our code, run, that did yes.
So with our extended method, we extended our list
by adding two items at the very tail end.
Whew, Amazing.
Now can I show you one more fun way to do this.
Watch this, instead of using extend
we're gonna use something that does the exact same thing.
We're gonna add a list to a list, but we'll do this.
We'll take supplies
and we'll say supplies equals supplies, which sounds weird.
Plus, and then we'll do our list of things you want to add.
So toilet paper and a bidet, and if we run our code
it should be the exact same thing.
Fingers crossed, totally is, awesome.
What's happening here.
First notice that we're not really using a method anymore.
We're simply using a plus sign addition
things we've already covered in this course.
When we try to add two strings
to each other or two integers
and here we're adding two list, which is kind of crazy.
So if we look at our equation, we're saying, Hey
we got this list, name, supplies.
We want this now to equal the list supplies.
Plus this other list we're just pulling out
of thin air, and, it did it. Adding those items
to the end of the list supplies.
So to sum it all up, sorry. When you want to
add one piece of data, which will happen often
especially when we do something called four loops.
Oh, I that's gonna be fun coming up soon by the way.
If you wanna add one piece
of data at a time, we'll use the append method.
So the list, name dot append, and then whatever you want to
add on the side.
To add multiple pieces of data or add a list to a list.
We'll either use the extend method with a list inside
or we can simply add a list to a list.
But wait, there's more.
So what if you don't want to add an item
to the very end of your list?
What if you want it to go
in the beginning or in the middle or second to last
there is a way there is a method, a new method.
So let's say that the bidet is probably the most
important item we have to bring on a trip.
So I want that at the very beginning of our list.
So we don't forget it.
So I, the bidet added at the, well, the beginning.
How do we do that?
Let's try it out.
Here we go type in supplies
our list and then, dot, in our new method, insert.
Ooh. And let's try it out parentheses.
And if I want that data to go with the beginning
here's what I'll do.
I'm gonna type in zero, comma,
and then my piece of data, "bidet".
So what's happening here because with this method, it seems
things are a bit different, right?
We have two arguments that come into play.
The first one is doing something.
We'll talk about him in a bit.
And the second one is our data, which we already know that.
But this first thing that is actually something
we've already talked about
in the first episode of this talking about list.
That's our index.
You may have picked up on that.
You may recall that our list are ordered.
They're sequenced.
0, 1, 2, 3.
You got it. They're in order.
And you can reference that order when you're
trying to pull data out of it.
And now when you're trying to insert data into your list
so I'm telling this list, Hey buddy, bidets are important.
And I want that bidet to be at position zero index zero.
When I add the sucker in there. Now toilet paper
is pretty important, but it's for you.
And I don't really care about that so much.
So just add toilet paper
to the second to last position of our list.
Can you do that for me real quick? and challenge for you.
Challenge, use a negative index.
Can you do it?
Try it out.
Pause the video.
Unpause, coffee break.
This is a fun one. I love when we combine multiple things.
Okay? So here's how you would do it.
I'm gonna do another insert method.
So supplies dot, insert parentheses.
Now I position if I want it to be second
from last will be negative, one, comma
And then, my data, toilet paper.
Let's run our code to see if it works and boom, there it is.
Toilet paper. Second to last in our list.
Now this might have been a bit confusing
because in our previous video
if we wanted to print out the last item in our list
we would do this: print supplies, use our brackets
and reference the negative one index run our code.
Negative one absolutely means printing out the last item
'cause there it is right there.
Marshmallows. They're delicious.
However, it's a bit different when we're inserting data
because normally when we're just adding data
to our list and we're not using the insert method
it'll always go down here at the end.
But with insert, If we wanna have it go,
just one before the end,
we're gonna use negative one and so on and so on
So if we want it to be third from the last, well
I mean you probably got that, right?
It'll be negative too.
Let's run the code. There it is. Third from last.
Woo. Okay.
Coffee break. That was a lot.
But I told you right? Way more fun
than Elden ring or whatever video game you're into.
This is real life, camping
toilet paper, bidets, Python, man! Love it.
Now right here, you learn the concepts
but you need to concrete that into your brain.
Solidify it practicing.
So check out network, Chuck academy, link below.
We're gonna have extra practice
extra quizzes to cement these things into your brain.
In the next episode
we're going to destroy some things from our list.
Obliterate, blow it up with some new methods.
It's gonna be fun.
And by the way, have you hacked the YouTube algorithm today?
Let's make sure you do hit that like button.
Subscribe, notification bell, comment
You gotta hack YouTube today.
Ethically of course.
Yeah. That's all I have.
I'll catch you guys next time.