1 00:00:01,190 --> 00:00:08,119 In the previous video, we used a Python script to create a VLANs on the switch. 2 00:00:08,600 --> 00:00:14,870 So we wrote this code to create a VLANs 2 to 8 on the Cisco switch. 3 00:00:15,350 --> 00:00:20,150 Now, even though that works, it's not a great way to program networks. 4 00:00:20,360 --> 00:00:27,890 You can make mistakes by copying and pasting code like this, and it's not very easy to edit and maintain. 5 00:00:28,370 --> 00:00:35,240 What happens if you want to create 50 VLANs or 100 VLANs or 200 VLANs? 6 00:00:35,570 --> 00:00:40,580 This is not a very efficient way of creating VLANs on the switch. 7 00:00:41,090 --> 00:00:49,250 So what I'm going to do is copy the Python 32. p script to a Python 33. p script. 8 00:00:49,880 --> 00:00:55,820 Let's edit that script and improve the script by using loops. 9 00:00:56,300 --> 00:01:02,690 So rather than creating a vlans in this way, let's see if we can simplify the code. 10 00:01:04,269 --> 00:01:08,560 And use a loop to create the VLANs on the switch. 11 00:01:09,940 --> 00:01:11,620 Now before I do that. 12 00:01:12,530 --> 00:01:14,480 Let me show you an example of a loop. 13 00:01:14,480 --> 00:01:18,160 So I'll create a Python script called Python Range dot P. 14 00:01:18,590 --> 00:01:21,260 You don't have to use the word Python in your scripts. 15 00:01:21,560 --> 00:01:24,320 I'm only doing that to make it easy for myself. 16 00:01:24,620 --> 00:01:28,760 Now, in this test file, let's use the range option. 17 00:01:29,510 --> 00:01:36,530 I'm going to say four NW in range and I'll specify a range of 2 to 10. 18 00:01:36,860 --> 00:01:39,080 I'm going to add four spaces here. 19 00:01:40,520 --> 00:01:44,660 That's good practice in Python and then I'm going to say print n. 20 00:01:45,470 --> 00:01:52,280 So in the range 2 to 10 print n where n is the number in the range. 21 00:01:52,640 --> 00:01:54,590 Let's see what that script does. 22 00:01:55,790 --> 00:01:58,940 So Python three python range. 23 00:01:59,210 --> 00:02:00,020 P y. 24 00:02:00,410 --> 00:02:02,240 Now notice the error. 25 00:02:02,510 --> 00:02:04,700 This is what's nice about Python. 26 00:02:04,730 --> 00:02:10,340 It's showing us that we've made a mistake at this point in our script. 27 00:02:10,580 --> 00:02:13,790 We told that this is invalid syntax. 28 00:02:14,510 --> 00:02:19,460 So what I'll do is editor that script and fix it by adding a colon here. 29 00:02:19,760 --> 00:02:22,310 This is an example of what's called a loop. 30 00:02:22,880 --> 00:02:30,440 We're going to get a computer to do multiple things for us rather than us doing it manually. 31 00:02:30,950 --> 00:02:34,160 So notice the script has printed out 2 to 9. 32 00:02:35,050 --> 00:02:37,930 We created a range 2 to 10. 33 00:02:38,720 --> 00:02:44,450 So if we wanted to print out 2 to 10, we would simply change this value to 11. 34 00:02:46,830 --> 00:02:51,780 Run the script again and notice it prints values in the range 2 to 10. 35 00:02:52,110 --> 00:02:57,120 If I wanted to make that bigger, I could simply say 101. 36 00:02:58,910 --> 00:03:00,440 So run the script again. 37 00:03:00,560 --> 00:03:01,460 Python three. 38 00:03:01,460 --> 00:03:03,080 Python arranged a pi. 39 00:03:04,480 --> 00:03:09,040 Notice its printed numbers in the range to. 40 00:03:10,510 --> 00:03:11,470 To 100. 41 00:03:13,350 --> 00:03:18,180 Now think about how we can use that when we create a VLANs. 42 00:03:18,540 --> 00:03:25,500 We could create a script that creates ten VLANs or a hundred VLANs by using a loop. 43 00:03:27,270 --> 00:03:28,950 Now before we do that? 44 00:03:29,190 --> 00:03:31,440 Well, let me explain a little bit about loops. 45 00:03:31,710 --> 00:03:34,500 Spacing is very important in Python. 46 00:03:35,650 --> 00:03:42,850 Use false spaces to indicate that this code belongs under this code. 47 00:03:44,510 --> 00:03:49,040 The logic is very similar to Cisco iOS configurations. 48 00:03:49,860 --> 00:03:55,740 When looking at the running configuration, we know that this IP address. 49 00:03:56,770 --> 00:04:01,450 Belongs to this interface because of the indentation here. 50 00:04:01,600 --> 00:04:03,910 One space is used in Python. 51 00:04:03,910 --> 00:04:07,060 We use four spaces for indentation. 52 00:04:07,540 --> 00:04:11,740 We know that this IP address belongs to this interface. 53 00:04:11,890 --> 00:04:20,110 Because of this indentation, we know that this network command belongs under the OSPF process. 54 00:04:20,740 --> 00:04:22,720 Because of this indentation. 55 00:04:23,670 --> 00:04:28,200 And again, these commands belong under the Orcs port. 56 00:04:28,770 --> 00:04:32,820 These commands belong under console and so forth and so on. 57 00:04:33,000 --> 00:04:38,430 If I try to type a network, 0000 here. 58 00:04:40,930 --> 00:04:47,890 That wouldn't be accepted on the roster because I need to be in the right mode or right context and 59 00:04:47,890 --> 00:04:49,630 then I can type the command. 60 00:04:50,170 --> 00:04:53,800 So in the running configuration of a router. 61 00:04:54,610 --> 00:05:02,260 The router knows that this command is part of the OSPF configuration rather than part of, say, the 62 00:05:02,260 --> 00:05:04,090 IGP configuration. 63 00:05:04,750 --> 00:05:08,440 So command like that is applied differently. 64 00:05:08,470 --> 00:05:11,560 It hasn't been applied to OSPF. 65 00:05:12,370 --> 00:05:16,870 It's going to be applied to IGP because of this indentation. 66 00:05:17,290 --> 00:05:21,550 We know these commands belong under this interface and so forth and so on. 67 00:05:21,850 --> 00:05:25,570 So if I made a mistake here and removed the indentation. 68 00:05:25,720 --> 00:05:27,100 Notice what happens. 69 00:05:31,650 --> 00:05:33,000 Notice the output. 70 00:05:33,210 --> 00:05:34,620 There's an error here. 71 00:05:34,860 --> 00:05:37,830 Expected an indented block. 72 00:05:38,640 --> 00:05:40,050 Python is complaining. 73 00:05:41,190 --> 00:05:46,410 So let's edit to the script again and I'll simply say print. 74 00:05:48,150 --> 00:05:48,840 Hello. 75 00:05:49,890 --> 00:05:51,870 What's the script going to do now? 76 00:05:52,880 --> 00:06:00,380 If this print statement is here and this print statement is here with indentations, how is it going 77 00:06:00,380 --> 00:06:01,760 to react now? 78 00:06:01,850 --> 00:06:04,730 So python three python range to p y. 79 00:06:07,590 --> 00:06:08,100 Notice. 80 00:06:08,100 --> 00:06:13,560 Hello is printed multiple times, but we only see 100 once. 81 00:06:16,170 --> 00:06:25,380 If I ed that script and add four indentations here to tell Python that this command is part of the loop. 82 00:06:25,830 --> 00:06:33,060 Notice the difference now when we run the script, we're getting hollow, followed by a number. 83 00:06:33,150 --> 00:06:34,320 In this case. 84 00:06:35,170 --> 00:06:37,930 2 to 100. 85 00:06:38,950 --> 00:06:43,000 Notice how the indentations change the behavior of the script. 86 00:06:43,150 --> 00:06:47,140 So if I moved that hello to here. 87 00:06:47,920 --> 00:06:49,780 And removed the spaces. 88 00:06:50,500 --> 00:06:51,430 Notice we see. 89 00:06:51,430 --> 00:06:52,210 Hello. 90 00:06:52,210 --> 00:06:53,950 Printed only once. 91 00:06:55,970 --> 00:06:58,010 So be careful with your spacing. 92 00:06:58,520 --> 00:07:01,610 Spacing is very important in Python. 93 00:07:02,740 --> 00:07:06,640 You need to get your spacing right when creating scripts. 94 00:07:07,510 --> 00:07:11,350 So in this case we've got numbers followed by Hello? 95 00:07:13,880 --> 00:07:16,040 Because we've got four spaces here. 96 00:07:16,520 --> 00:07:18,920 Be consistent with your spacing. 97 00:07:20,530 --> 00:07:23,680 My recommendation is that you use four spaces. 98 00:07:25,340 --> 00:07:28,280 And that's per the Python best practices.