- This is a Python list,
and they are ridiculously powerful
and pretty fun to play with.
So in this episode,
get you coffee ready because we're covering
the what and why of this incredible data structure,
and how it will open up a whole new world
of Python goodness.
You ready? (coffee slurping)
Let's dive in.
(soft groovy music)
Okay, here we go.
One more sip of coffee. Let's get started.
So we covered a variable, we create it,
assign it a simple piece of data,
and we can use it everywhere in our Python script.
It's awesome. But let me show you when it's not awesome.
Because you and I, we're going on a camping trip.
What, you didn't know? Yeah, come on.
But first we need our Python lab up and running,
let's get that running real quick.
So check that link below in the description
to get access to a free Python lab right now.
But while your lab is loading up,
let me tell you about the sponsor of this entire series
and why it can be free here on YouTube;
ITProTV.
They are by far my favorite IT training provider.
They are what I use to keep myself up to date
on all the latest things in IT.
From automation,
which dad gum look at all these automation things,
to all things Cisco.
Holy crap, look at all the Cisco stuff.
ENCOR, DevNet, CCNA, and, of course, Python.
They have everything you need
to go crazy in Python right now
if you just cannot wait
and you wanna go further, this is it.
Seriously, check it out, link below.
But that's not all, a fuckin' infomercial, but seriously,
they do have more than training.
They have, hey look, practice test.
To get you ready for those crazy exams.
Or how about a lab to actually practice what you're doing?
Look at all these labs.
So, if you wanna learn IT like me,
check it out, link below.
Use code NETWORKCHUCK and get 30% off forever.
Seriously, they are awesome.
So yeah, your lab should be up and loaded. Let's keep going.
So, we're going on a camping trip.
And like all camping trips, we need a ton of stuff.
Tent, sleeping bags, raspberry pis, flash drives,
ethernet cables, marshmallows, just things like that, right?
The essentials.
Now here's what I need you to do.
I need you to create a variable. Let's name it camp_stuff.
And I want that to equal all the supplies we have to bring.
Go ahead and do it right now.
In fact, we'll do it together right now. Let's do it.
So using the knowledge we learned so far in this series,
we might do this.
Camping stuff. Variable created.
Let's have it equal something. Equals.
And, you know what, we'll just start typing in the stuff.
Tent, sleeping bags,
jump forward in time and have it already there.
And if we print the variable camping stuff
and we run our code, go and click run at the top there.
That works, but it doesn't.
Because right now this is one big fat gargantuous string.
It's bloated. It's ugly. I hate it.
It's really hard to use, and honestly kind of stupid.
We can do better.
Instead of storing all of our data into one big fat string,
we could try out something really cool,
something amazing, something fun, something life changing.
something called a Python list.
(Chuck chuckling)
Let's do this.
Which honestly makes total sense,
'cause we're trying to make a list of things we gotta bring.
Hey, Python list. It just all adds up.
It's all comin' together here.
So I'm gonna create a new variable called camping_list.
Just like before, but here comes the craziness.
Don't worry. I'll walk you through it.
Brackets. That's the game changer right there.
Buckle your seatbelt.
Coffee break. (coffee slurping)
And time to build the list.
You can step forward in time. Just go ahead and jump.
Jump forward in time. Woo. Done.
That was exhausting.
Need a coffee break. (coffee slurping)
Okay, and also I want you to do the same thing.
So I know it's a lot of typing, whatever.
Just do it. It's not too hard.
This right here is a Python list.
And it's so awesome for so many reasons,
way better than this idiot over here.
And I will go into why,
but first let's get a really nerdy definition
on what a Python list is.
Go ahead Bernard.
- [Bernard] A Python list is an ordered
and changeable collection of data objects.
Unlike an array, which can contain objects of a single type,
a list can contain a mixture of objects.
- Now first before we talk about how awesome this is,
I want you to print the type of data
this camping list variable is.
Just go ahead and print it real quick. The type.
Do you remember how? Here's how we do it.
Print using our print function.
And then in the print function we use our type function
and that'll just print the type of the camping list.
'Cause I don't wanna lie to you, I gotta prove to you
that this is a data structure called list.
Let's go ahead and run the code. Run.
And there it is suckers, I told you.
A data class, a data structure called list. Oh man.
I'm tellin' you, this is gonna open up so many doors
to Python coding.
We're gonna spend a lot of time actually playing with this.
Okay. Anyways. I'm excited for you. Let's keep going.
I'm gonna delete this stuff real quick
and we'll talk about what the junk is going on
with this stuff up here.
Now you can recognize a list
because it'll always have opening and closing brackets.
Bam, and bam.
It has to have those. That's what a list needs.
And then inside that list is where we have our data.
And notice how the data is separate by a comma.
There they all are.
Now real quick, let's bask in the awesomeness of a list.
So up here, right? Is a big ugly strings.
One big fat string.
Down here, we got a bunch of individual strings.
Like how many we have here. Count for me.
I'll do it.
We have 10 individual pieces of data
inside this one variable.
So much better, right?
You're probably thinking, well, why is it so much better?
We'll get to it. We'll get to it.
I'll show you a use case here.
But can I tell you something else cool about a list?
It can have more than just string data types inside of it.
Let's try it out, actually.
So, let's create a new list.
And I wanna name this camp_site.
And actually, I want you to do this.
And here's the data I want you to put in there.
It'll be campsite information.
First, the name of our camp will be, or the campsite,
will be Crystal Lake.
Sounds perfectly safe.
And then of course we'll need our site number,
which will be 404.
And then they expect a temperature of that day.
It's gonna be a balmy 89.3 degrees.
And then one more piece of data.
Is this place dangerous? True or false?
It's gonna be true. It is dangerous.
So, make our campsite list.
Go for it now. Pause.
Unpause. coffee break. (coffee slurping)
Let's go ahead and do it.
To make our list we'll do an opening bracket
and our amazing Python replit here will make it for us.
First data, Crystal Lake, is a string.
Cool. We got that.
Comma, space. Space is optional, but it's best practice.
Do a space just 'cause it looks prettier.
And we're all about pretty.
Next piece of data. 404.
That's gonna be a number, an integer, right?
Done. Just like that, no quotes.
Space, or comma space.
Temperature. 89.3 degrees.
Fahrenheit people. I'm in America. Get over it.
And then finally, is it dangerous?
Yeah. True. Cool.
So (Chuck chuckles) how amazing is this?
Here in this list
we have four different types of data, right?
We got a string, we have an integer, a float, and a boolean.
I don't know why I say it like that. I'm sorry.
So now I think you're startin'
to see how amazing the list are.
They can hold multiple pieces of data
and different types of data.
Doesn't that make you just a little bit excited?
If it doesn't, man, sip some more coffee
and then come back to me, okay?
Rewind. Now here comes the killer.
(Chuck chuckles) killer.
The killer part of list. I talked about this.
If you remember what Professor Bernard said,
they are ordered.
What does that mean?
I'll explain it by showing you two amazing things.
You ready for this? Okay. Here we go.
Let's say that out of our camping list I need
to assign who's going to bring what on the camping trip.
So for example, me, I'm going to bring coffee, duh.
Right? You saw that coming.
And you. You're coming, right?
Yeah. You're coming.
You're gonna bring marshmallows because they're delicious.
Here's how I can assign that data with a list.
Oh, it's so powerful. Watch this.
This is gonna blow your mind.
I'll create the variable me and have that equal, watch this,
the list camping list, and then do some brackets.
And then in those brackets I'm gonna put the number four.
What just happened? What am I doing?
Watch. I'm going to print me, the variable me.
Let's run it. Run. Did you just see that?
(Chuck laughing)
Sorry.
I know it's it seems like I'm over exaggerating,
but I'm really this excited about it.
I'm not kidding.
Right here. It printed just coffee.
Just that one string, that one piece of data in our list.
Now go ahead and do the same thing for you.
Do you remember what you're bringing?
Go ahead and do it real quick.
Pause the video, do the same thing for you,
and then print out that one item.
Go. Unpause. Okay.
How would you do that?
Create the variable you, have that equal camping list
and then brackets, let's see if you could figure this out.
You were bringing marshmallows,
which is the last in our list,
which means marshmallows will be nine.
Did you get that right?
I'll explain why it's nine here in a second,
but let's go ahead and print it out.
I'll put it below me.
Printing you now. And run.
Coffee and marshmallows. Yes.
So when I say the list is ordered,
or you might see people talking about it being
in a sequence, or sequenced,
when you put your stuff, that data, into your list,
the order you put it in there, it stays the same.
Which is great because we can do things
like reference the individual items in that list.
So when it says ordered, when it says sequenced,
it just means, hey, that stuff you put in there,
it's in that same order all the time unless we change it,
which we'll talk about later.
But then real quick, hold on. Why was coffee four?
Because if we're looking at our list here,
we got one, two, three, and then four.
Raspberry pi is four.
Coffee should have been five, right?
No, we're dealin' with computers
and computers have to be difficult sometimes.
You might realize and remember and know
that computers always start counting with zero.
That's what they do.
So test would be zero.
Then one, two, three, bringin' us at four with coffee.
Which is why marshmallows, instead of being 10,
would actually be 9.
So in a list with 10 items,
your last number will always end up being 9.
Just keep that in mind.
That always messes me up whenever I'm dealing with list,
so I just have to manually, consciously remember it.
It's fine though.
But this right here, what we did here, is super powerful
and it'll become more apparent how powerful that is
when we do something later.
I can't tell you yet.
I wanna tell you, but you have to wait.
But diving deeper into that,
you'll also see list referred to as indexed.
That's kinda just another way of saying ordered.
Ordered, sequenced, indexed.
And when I say indexed, it just means
that each item in our list is assigned a number.
It's just the order it's in.
And when we reference a list like this,
we're calling out the index of that item.
We're going, hey, ah, here's our camping list.
Item number four. Pull it out.
That's what we're doing. How cool is that?
And you probably already noticed that when we are trying
to reference a item in a list, when we index it,
we use brackets.
Now, quick coffee break.
I wanna cover one more thing that might break your brain,
but can we just do it real quick?
It's gonna be fun. It's really fun, I promise.
Coffee break. (coffee slurping)
It has to do with indexing. We're gonna index one more time.
And actually I'm gonna change you real quick.
I'm gonna change you. Your variable you.
You're still bringing marshmallows,
but I'm gonna change the way I find marshmallows
with that list.
Watch this.
I'm gonna change that nine to, this is weird, negative one.
Now what do you think is gonna happen when we print this?
Let's try it out. Let's run our code.
Hey look, it stayed the dang same. (Chuck chuckling) Why?
Well, that's called a negative index.
It does come in handy.
But essentially what you're doing
is you're counting in reverse.
So instead of starting up here,
we're starting all the way back here.
So the very last item in our list would be negative one.
And you're probably thinkin',
well, Chuck, nuh uh, should be negative zero.
It's not. Just isn't.
You can't have negative zero. It's impossible.
Anyways, sorry. I didn't mean to get little angry with you.
I'm just kidding. I wasn't.
But that's a handy way to access the last item in your list.
You're probably thinking, well, that's kind of stupid.
Why would we ever do that?
Well, sometimes you may not know how big a list is.
And other times there might be a very good reason
to access the last item in a list.
You'll find out that later.
But let's try it one more.
What happens if we do, go ahead and change it
to negative two.
Negative two. Let's try it out. Run our code.
Bam. Beard oil.
It's now the second to last item in the list.
And real quick, I can't believe we haven't done this yet.
Shame on me.
Let's go ahead and print the entire list
at the very end here.
Just print camping list. Run. Cool.
So when we print the camping list, guess what?
It looks just like our list that we have there.
Let me scroll up so you can see it.
The only difference
is that it uses single quotes instead of double quotes.
It breaks our standard.
It's okay though. We'll forgive it.
Now, I told you didn't I? I wasn't lying.
Python lists are really fun and really powerful.
Now, you probably understand the power they have,
even just now, but you have no idea what's coming up.
What you can do with these is gonna, you know,
it's just gonna be mind blowing.
So, I know you can't wait,
go ahead and move forward to the next episode.
Now, if you're watching on YouTube it might be like,
(Chuck groaning) it's not here.
I know.
I create them in batches and I release them week by week,
sometimes month by month, and maybe year by year, hush.
No, I do release them frequently,
but they do have them in advance on Network Chuck Academy,
which you can check out in the link below.
Also in Network Chuck Academy is extra practice
on the stuff we're covering.
So if you're like, hey, I get this, I understand it,
but I wanna play with it a bit more.
First, play with it yourself. Just do more of it.
But also, I'm gonna have some exercises,
some labs, and some quiz questions
along with all of this at Network Chuck Academy,
so check it out, link below.
Oh and also, hey, I'm gotta ask you a question.
Have you hacked the YouTube algorithm today?
Let's make sure you do.
Hit that like button, notification bell, comment, subscribe.
Did I hit everything? Yes.
You gotta hack YouTube today.
Ethically, of course.
Yeah. That's all I got.
And also, again, a huge shout out
to the sponsor of this entire series; ITProTV.
Again, if you're trying to get into IT,
if you wanna learn anything in IT,
get certification or just learn a skill,
dude, they're the place to go.
Again, if you use my code NETWORKCHUCK
you get 30% off forever.
Don't miss that deal.
It's not goin' away 'cause I said forever,
but you still, just don't waste time.
Don't don't wait till tomorrow, do it right now.
You can start learning right now.
You got a career in a month. Just do it.
I'll catch you guys later.
I'm done talking. Coffee break.
(soft groovy music)