In the previous video, we used a Python script to create a VLANs on the switch.
So we wrote this code to create a VLANs 2 to 8 on the Cisco switch.
Now, even though that works, it's not a great way to program networks.
You can make mistakes by copying and pasting code like this, and it's not very easy to edit and maintain.
What happens if you want to create 50 VLANs or 100 VLANs or 200 VLANs?
This is not a very efficient way of creating VLANs on the switch.
So what I'm going to do is copy the Python 32. p script to a Python 33. p script.
Let's edit that script and improve the script by using loops.
So rather than creating a vlans in this way, let's see if we can simplify the code.
And use a loop to create the VLANs on the switch.
Now before I do that.
Let me show you an example of a loop.
So I'll create a Python script called Python Range dot P.
You don't have to use the word Python in your scripts.
I'm only doing that to make it easy for myself.
Now, in this test file, let's use the range option.
I'm going to say four NW in range and I'll specify a range of 2 to 10.
I'm going to add four spaces here.
That's good practice in Python and then I'm going to say print n.
So in the range 2 to 10 print n where n is the number in the range.
Let's see what that script does.
So Python three python range.
P y.
Now notice the error.
This is what's nice about Python.
It's showing us that we've made a mistake at this point in our script.
We told that this is invalid syntax.
So what I'll do is editor that script and fix it by adding a colon here.
This is an example of what's called a loop.
We're going to get a computer to do multiple things for us rather than us doing it manually.
So notice the script has printed out 2 to 9.
We created a range 2 to 10.
So if we wanted to print out 2 to 10, we would simply change this value to 11.
Run the script again and notice it prints values in the range 2 to 10.
If I wanted to make that bigger, I could simply say 101.
So run the script again.
Python three.
Python arranged a pi.
Notice its printed numbers in the range to.
To 100.
Now think about how we can use that when we create a VLANs.
We could create a script that creates ten VLANs or a hundred VLANs by using a loop.
Now before we do that?
Well, let me explain a little bit about loops.
Spacing is very important in Python.
Use false spaces to indicate that this code belongs under this code.
The logic is very similar to Cisco iOS configurations.
When looking at the running configuration, we know that this IP address.
Belongs to this interface because of the indentation here.
One space is used in Python.
We use four spaces for indentation.
We know that this IP address belongs to this interface.
Because of this indentation, we know that this network command belongs under the OSPF process.
Because of this indentation.
And again, these commands belong under the Orcs port.
These commands belong under console and so forth and so on.
If I try to type a network, 0000 here.
That wouldn't be accepted on the roster because I need to be in the right mode or right context and
then I can type the command.
So in the running configuration of a router.
The router knows that this command is part of the OSPF configuration rather than part of, say, the
IGP configuration.
So command like that is applied differently.
It hasn't been applied to OSPF.
It's going to be applied to IGP because of this indentation.
We know these commands belong under this interface and so forth and so on.
So if I made a mistake here and removed the indentation.
Notice what happens.
Notice the output.
There's an error here.
Expected an indented block.
Python is complaining.
So let's edit to the script again and I'll simply say print.
Hello.
What's the script going to do now?
If this print statement is here and this print statement is here with indentations, how is it going
to react now?
So python three python range to p y.
Notice.
Hello is printed multiple times, but we only see 100 once.
If I ed that script and add four indentations here to tell Python that this command is part of the loop.
Notice the difference now when we run the script, we're getting hollow, followed by a number.
In this case.
2 to 100.
Notice how the indentations change the behavior of the script.
So if I moved that hello to here.
And removed the spaces.
Notice we see.
Hello.
Printed only once.
So be careful with your spacing.
Spacing is very important in Python.
You need to get your spacing right when creating scripts.
So in this case we've got numbers followed by Hello?
Because we've got four spaces here.
Be consistent with your spacing.
My recommendation is that you use four spaces.
And that's per the Python best practices.