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