Now let's talk about two new data types, the list as well as the dictionary.
And to begin, let's first focus on the list.
Think about it like this, when we happen to say, for example,
assign a string to a variable, all we're doing is kind of storing a singular
thing,
you know, like an email address or a name or perhaps we were using
and end of your data type to store a singular number.
When we're talking about utilizing something known as a list,
what this is doing, it's giving us a way to store multiple values,
so let me show you what I mean. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
so let me show you what I mean.
Now the way we actually do this in Python is we make the use of these square
brackets.
And for every single element in the list, we will separate them out,
utilizing commas.
Now right now, this might look a little bit strange.
So the best thing to do is just to get hands on and see it in action.
So if that said, let's just get to it them.
So I'm just creating new example called example 2.py and open this up.
Okay. So again, let's maybe say we want to have
multiple items stored as a variable. Okay. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
multiple items stored as a variable. Okay.
So let's begin with a variable. Let's just call this myFoods.
Okay, this is going to be something which is plural, obviously.
So as opposed to just saying, you know, pizza as so,
this would be a singular value as you print out.
If you want to have multiple values, let's just wrap it within square brackets.
Okay. Now right now, this is actually a list,
even though we only have one value within it.
Let me just prove it to you.
If I just print the type of myFoods and save this,
and let's just run example 2.py, we can see this is now a list object. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and let's just run example 2.py, we can see this is now a list object.
Now as it stands right now, it's a list with only one value in it.
So maybe not quite so useful, but it is still a list.
However, if I want to add in more values like I say,
I just have a comma and then I can separate the values.
So that's maybe adding ice cream as well as chocolate.
You can see here this is obviously a well-balanced diet.
So let's go and we'll try to print out the MyFoods variable right here.
Let's just hit Enter.
As we can see here, we're now able to store these three different items WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
As we can see here, we're now able to store these three different items
within this one variable value right here, MyFoods.
Now one thing to note about the list is that the elements within
happen to be ordered, meaning that when we happen to write in the values,
they are going to always be accessed in the same position,
unless we happen to modify the list that is.
So what do I mean?
I mean that when I print out this list,
pizza is always going to be first, then the second item will always be ice
cream,
then the third will always be chocolate.
So no matter how often I run this script and keep printing this out,
it's going to print out things in the exact same order. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
it's going to print out things in the exact same order.
This is important, okay?
So because things are always in the same order,
we can actually access them depending upon their position within the list.
Now, there will be actually access elements within the list,
is utilizing an index number.
Now, the index number starts from the index zero,
all the way up to the end of the list.
So that means that right here,
the first element is in list index position zero.
The next element is in list index one,
and the last element in this case here is in list index two. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and the last element in this case here is in list index two.
And if we want to target particular pieces of data within that list,
we just specify the index number.
So say, for example, I want to get the first element within the list.
I can just say MyFoods and then list position zero.
If I run this, look at this right here.
We're able to access pizza within this data type.
Then if I change the number to one,
what do you think we're going to print out?
We're going to get ice cream.
Then the number two, as we can see,
we're going to get chocolate.
And if I go above the value,
we're going to get an error.
As we can see right here, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
As we can see right here,
the list index is out of range.
We do not actually have any element in list position three.
Now, we actually can do things like adding to our list.
I can say MyFoods and then append to the list,
which is going to add on new value.
So I get append in, let's go with.
I don't know, let's say, stake.
And if I then print out MyFoods,
we have created the list,
added on a new element,
which is going to take the last place.
And then suddenly, if I print out the value now,
we can see here our list is now got a new element within it. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we can see here our list is now got a new element within it.
List position three, the value of stake,
which means that if we now try to grab index three
and I run the script,
we are able to access the value stake.
Now one thing to note here with respect to our list,
we can actually mix and match data types.
So say, for example, the second data type
was going to be a number.
Let's just go with 45,
not that it would make much sense in this context here,
but nevertheless, let's just prove it is true.
If I just print out the entirety of MyFoods
and run the script again,
I enter, look at this right here.
We actually have an integer in amongst all WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We actually have an integer in amongst all
of these other string values.
And we can actually see here,
the integer is not wrapped within quotation marks
like the other ones are, okay?
So a list is going to allow us to group together
multiple pieces of data,
even data of different data types.
The elements within are going to follow a particular order
and can be accessed via their position within the list.
And we can modify the list as we just saw right here, okay?
Now the next data type is going to be a dictionary.
Now dictionaries are so so important
in the world of network automation WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
in the world of network automation
because they are so commonly used.
Pretty much this allows us to store data
as key value pairs.
So we assign a particular key
and then return an associated value.
Let me show you how this actually works.
So what I will do is I'll create a dictionary called myinfo, okay?
Now again, we'll use our equal signs
and this time as opposed to utilizing square brackets,
I'm going to use my curly braces.
And what I will now do is,
with quotation marks, I'm going to specify a key.
So just give it a key, let's just say name.
Then I'll have a colon WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Then I'll have a colon
and then I'll have a corresponding value.
So let's just say the corresponding value
is a string which is John McGovern.
And then I can have a comma.
So this is the first key value pair, okay?
And then after the comma, we can specify the next key value pair.
So let's say the next key is going to be occupation
and the corresponding value will be trainer.
Then I'll have another comma.
And what I can actually do here
to make this a little bit more readable
is to just do this over multiple lines as so.
Then I can say hobbies.
And what I could do is say I like to play golf. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So similar to a list object,
we're able to store multiple pieces of data
but as opposed to accessing the individual components
via an index position as we see in the list,
this time we access the values
utilizing their corresponding key.
So what I can do this time is I can say print
the myinfo dictionary, try that again.
There we go.
So if I now print it myinfo and run this,
we can still see the list is being printed
because we are doing that right here as we see.
But below notice we're also printing out
the contents of this dictionary. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the contents of this dictionary.
So look at the start difference.
If I want to get a safe example,
the pizza value from my foods
I would use the list position zero.
Whereas if I want to get the value of John McGuffern here,
I don't use list position zero.
Instead I use the name of the actual corresponding key,
which is going to be my case here.
If I do within square brackets and just say the name key.
If I now run this, look at this right here.
We're able to access the elements in the list
via the index.
And this one here we're able to access the element
utilizing the key. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
utilizing the key.
And again, if I just change this to let's maybe say hobbies
and save here, run the script again.
As we can see here we're now getting a new value return.
So this is our introduction to lists
and dictionary.
These are very, very popular concepts within Python.
You're going to see a lot of it within network automation.
With this now done, let's now turn our attention
to the idea of conditional logic.