1.11 Python Basics Part 2 - IP
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
>> Hello everyone and welcome back to Intro to Python,
00:00
you're on Cybrary on demand.
00:00
I as always, am your instructor,
00:00
Joe Perry, and you're in Lesson 7, Video 2.
00:00
In this video we're going to be discussing
00:00
the Python datatypes, lists, and dictionaries.
00:00
In our last video we covered strings and numbers.
00:00
List in Python is essentially just an array of data.
00:00
It's a storage mechanism for large amounts of data.
00:00
For example, [NOISE] we might do L1.
00:00
The easiest way to define a list
00:00
in Python is with square brackets.
00:00
You can also use the list command.
00:00
I'm personally not a huge fan of doing that
00:00
[NOISE] just because there's
00:00
not usually much of a need for it.
00:00
It's usually easier to use
00:00
the square brackets and then just
00:00
assign your values directly in it.
00:00
[NOISE] But either way is functional.
00:00
You can see here that I created this list, L1.
00:00
I can clear my screen and fix that.
00:00
We can look at what L1 contains just by
00:00
typing in on the line or by printing it.
00:00
[NOISE] Both of those are
00:00
going to work the same way in the interpreter
00:00
in a Python script.
00:00
If we were to actually write this code
00:00
down into a document,
00:00
we would have to use Print
00:00
to get it to print to the screen,
00:00
but in the interpreter, you can do either.
00:00
You can see here that this list exists and it
00:00
holds a bunch of numbers right now,
00:00
and you may remember from our for-loop video,
00:00
that we can do things like,
00:00
for i in L1,
00:00
[NOISE] print i squared,
00:00
and you can see there that that will work just fine.
00:00
Additionally, you can actually perform a lot of
00:00
other operations with lists.
00:00
You can, for example, create two lists,
00:00
[NOISE] L1 and L2,
00:00
and combine them very
00:00
easily to create a single list containing all of
00:00
those datas or all of those pieces of data.
00:00
Additionally, it's worth noting
00:00
>> here that this list here
00:00
>> can contain multiple types of data.
00:00
It can contain multiple types of variable.
00:00
It doesn't necessarily have to be variables,
00:00
doesn't necessarily have to be strings or numbers,
00:00
it can be basically anything in Python.
00:00
In fact, you can even have
00:00
[NOISE] a list containing other lists,
00:00
and you can see that demonstrated
00:00
>> when we use [NOISE] L4,
00:00
>> it is worth noting that depending on your font,
00:00
it is actually recommended not to use
00:00
the lowercase letter l as a variable name.
00:00
In general, don't use single letter
00:00
variable names, but the lowercase letter l,
00:00
as you can see here,
00:00
actually looks like a one and
00:00
can make the code confusing to read.
00:00
We're not too worried about it right now
00:00
because we're reading very simple programs,
00:00
but it is worth noticing.
00:00
Anyway, the point here that you can see is
00:00
this list now contains three lists,
00:00
each of which contains strings or numbers.
00:00
It's very easy to do.
00:00
You can see I created those with one line
00:00
commands each time, very straightforward.
00:00
List are a really powerful tool,
00:00
you can do a surprising amount of work with.
00:00
Like I said in our previous video,
00:00
we're going to spend an entire lesson
00:00
on lists in Module 2.
00:00
For now, we're going to move on to dictionaries,
00:00
the last Python datatype we're going to be
00:00
discussing in Lesson 7.
00:00
I'm mixing up my lessons now.
00:00
Dictionaries are very similar to lists.
00:00
They're created in a similar way,
00:00
[NOISE] but instead of using square brackets,
00:00
we're going to use braces or
00:00
curly brackets depending on who you're talking to.
00:00
But those are braces and calling them
00:00
curly brackets makes you sound like a 12-year-old.
00:00
I shouldn't say that on video, but I'm going to.
00:00
Moving on, Dictionary 1 that we've created here,
00:00
is an empty dictionary that has no values.
00:00
We can also choose to create it
00:00
[NOISE] with some initial values in it.
00:00
[NOISE] The way you
00:00
create values in a dictionary
00:00
is as what we call key value pairs,
00:00
much like the way you would with
00:00
a real dictionary in your office or your home,
00:00
where you look for the word
00:00
that you're trying to find the definition of.
00:00
You find that word in the list,
00:00
and it will show you the definition next to it.
00:00
Dictionaries in Python operate in exactly the same way.
00:00
We can do, for example, for our keys,
00:00
we might do the string A is the
00:00
>> key for the word "APPLE"
00:00
>> [NOISE] B, "BANANA".
00:00
[NOISE] Never know when to stop
00:00
>> writing the word banana,
00:00
>> and C we can do "CHERRY".
00:00
Here you can see we've created this dictionary with
00:00
three key value pairs,
00:00
and those key value pairs are associated
00:00
with one another by using a colon.
00:00
Each key-value pair is separated from
00:00
the other key-value pairs by commas
00:00
the same way that items are just separated in lists,
00:00
and we just break that out into
00:00
each of these individual pairs.
00:00
Then instead of using references like you might
00:00
with lists which are referenced by index,
00:00
which I actually didn't talk about,
00:00
so I'm glad that we're doing this all in one video.
00:00
You can reference specific items in
00:00
lists by using the index number.
00:00
For example, zero will give
00:00
you the first item of the list.
00:00
[NOISE] One will give you the second,
00:00
because remember we index from zero.
00:00
If you try and find
00:00
the index of an item that is not in that list,
00:00
you'll get an error saying that
00:00
your index is out of range,
00:00
but in dictionaries you don't address them by using
00:00
the index in the same way you would
00:00
with a list, instead,
00:00
you address things in dictionaries
00:00
[NOISE] by using the same square brackets
00:00
that you would if it were a list,
00:00
but using the key.
00:00
[NOISE] Here you can see by giving it the key of A,
00:00
it returned APPLE,
00:00
because as you may recall
00:00
from when we created the dictionary,
00:00
A is paired to APPLE as the key to that value.
00:00
It works exactly the same way with each of them.
00:00
[NOISE]
00:00
Very easy to reference.
00:00
Now it's worth noting that if you try and reference
00:00
a key [NOISE] that does not exist in your dictionary,
00:00
you will get an error and it's just going
00:00
to say KeyError 5.
00:00
Actually, I think it's going to be KeyError 4.
00:00
Not what I meant to do there. We're going
00:00
to clear this real fast.
00:00
[NOISE] The KeyError is
00:00
going to be whatever your input was as the key.
00:00
That's just telling you that
00:00
you're not using a valid key.
00:00
A lot of times that's going to be because of the case.
00:00
You need to check and make sure
00:00
because it is case-sensitive,
00:00
it could be really any cause,
00:00
the point is that you're not using
00:00
the right key for that particular dictionary.
00:00
That is, lists and dictionaries in Python.
00:00
Again, each of those is going to be
00:00
the subject with its own lesson.
00:00
They are incredibly powerful tools,
00:00
you can perform an astonishing array of operations,
00:00
we're going to spend a whole bunch of time on
00:00
those in Module 2,
00:00
but for now you are actually done
00:00
with Lessons 7 of Module 1,
00:00
which means that you are done with Module 1.
00:00
In our next video, we're going to
00:00
perform a summary and review.
00:00
We're going to talk about what we've learned in
00:00
this module and we're going to prepare for the midterm,
00:00
that is going to be part of your
00:00
>> supplemental materials.
00:00
>> That is the end of Module 1.
00:00
I hope that you have enjoyed learning with
00:00
me and I hope you come back for Module 2,
00:00
which should be right next on your list.
00:00
I'm very excited to start really digging into
00:00
Python code and writing some real programs.
00:00
As always, I'm your instructor Joe Perry,
00:00
and thank you for watching Intro to Python
00:00
here on Cybrary OnDemand.
Up Next
Similar Content