Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,160 --> 00:00:01,520
Now.
2
00:00:01,520 --> 00:00:04,190
So far we built a good enough program.
3
00:00:04,220 --> 00:00:11,510
It can take user input and based on it, it changes the Mac address of the interface that the user specifies
4
00:00:11,510 --> 00:00:13,460
to the MAC address that they choose.
5
00:00:13,940 --> 00:00:20,240
What I don't like about it right now, though, is it looks a little bit messy and I think it can really
6
00:00:20,240 --> 00:00:21,740
use some refactoring.
7
00:00:22,100 --> 00:00:28,100
Therefore, I think this is a great opportunity to introduce you to functions.
8
00:00:28,970 --> 00:00:35,180
Now functions are used everywhere in programming and we've actually used them in previous lectures,
9
00:00:35,180 --> 00:00:36,800
even without you thinking.
10
00:00:36,980 --> 00:00:44,150
So for example, when we used to use the input or zero input, that was a function that displays on
11
00:00:44,150 --> 00:00:49,430
screen whatever we put in here and returns, whatever the user inputs.
12
00:00:49,430 --> 00:00:52,580
And then we use to capture that and store it in a variable.
13
00:00:54,090 --> 00:01:01,590
So what we mean by a function is it's a set of instructions that carry out a certain task.
14
00:01:02,320 --> 00:01:04,970
Like we've seen before, it can take an input.
15
00:01:04,989 --> 00:01:12,520
So for example, again here, the input or the RO input takes a string as input and displays it on screen
16
00:01:12,850 --> 00:01:15,010
and it can return a value.
17
00:01:15,010 --> 00:01:20,980
And again, we see that this returns the value of whatever the person enters using their keyboard.
18
00:01:21,880 --> 00:01:28,600
Now the reason why we use functions is because, first of all, they make our code look pretty and we'll
19
00:01:28,600 --> 00:01:30,460
see that now once we use it.
20
00:01:30,550 --> 00:01:33,550
It also makes our code reusable.
21
00:01:33,550 --> 00:01:40,600
So we've seen before that we use the input function twice in our code to ask for the interface and for
22
00:01:40,600 --> 00:01:41,650
the Mac address.
23
00:01:42,220 --> 00:01:48,100
Without that function, we would have had to write the code for the input function twice.
24
00:01:49,180 --> 00:01:56,080
It also makes our code or makes programming more abstract because when we use the input, for example,
25
00:01:56,080 --> 00:01:58,150
we don't even know how it works.
26
00:01:58,150 --> 00:02:01,030
We don't know the code used in this function.
27
00:02:01,060 --> 00:02:03,880
All we know is I need to give it a string.
28
00:02:03,910 --> 00:02:10,120
The string that I give will be displayed on screen and then the function will work and ask the user
29
00:02:10,120 --> 00:02:11,260
to enter something.
30
00:02:11,260 --> 00:02:15,310
Whenever the user enters something, the function will give it back to me.
31
00:02:15,370 --> 00:02:19,690
I don't know the code behind the input function itself.
32
00:02:19,720 --> 00:02:23,950
Whoever wrote it, wrote it very well and now all of us are using it.
33
00:02:24,580 --> 00:02:28,240
So you need to use the same idea when you program.
34
00:02:28,240 --> 00:02:35,710
So whenever you have a block of code in your program, for example, right here we have a block of code
35
00:02:35,710 --> 00:02:37,600
that changed the MAC address.
36
00:02:38,050 --> 00:02:44,770
Now we know that this code works very well, and if we bundle this inside the function, then we can
37
00:02:44,770 --> 00:02:48,520
reuse this function any number of times, even in the future.
38
00:02:48,520 --> 00:02:54,700
If we write bigger programs and need to be able to change the MAC address from within that program,
39
00:02:54,700 --> 00:02:59,470
then we can just call this function instead of having to type its code.
40
00:02:59,470 --> 00:03:06,490
Again, not only that, but we can also bundle this in a separate file and then we can just import this
41
00:03:06,490 --> 00:03:13,210
the same way that we import sub process right here and call a mac changer function so we won't even
42
00:03:13,210 --> 00:03:16,480
have to worry about the code written in this function.
43
00:03:16,480 --> 00:03:22,090
Maybe in a year or two year or five year time from now, you want to be able to use a code that changes
44
00:03:22,090 --> 00:03:23,140
the Mac address.
45
00:03:23,170 --> 00:03:24,790
You won't have to rewrite it.
46
00:03:24,790 --> 00:03:28,090
You won't even have to remember how to change the Mac address.
47
00:03:28,210 --> 00:03:32,260
All you have to do is just call the function that you created before.
48
00:03:33,660 --> 00:03:39,900
So let's try and bundle all of this inside the function and use it from our code.
49
00:03:40,320 --> 00:03:43,650
Now, first of all, you can define a function like so.
50
00:03:45,230 --> 00:03:47,030
You just have to type death.
51
00:03:47,860 --> 00:03:53,830
Followed by the function name, and it's a good idea to use meaningful function names.
52
00:03:53,830 --> 00:03:57,340
So we'll call this function change mark.
53
00:03:58,960 --> 00:03:59,550
Okay.
54
00:03:59,980 --> 00:04:04,240
Now, followed by this, you need to put two brackets.
55
00:04:04,240 --> 00:04:10,150
And between these two brackets, we're going to specify the input that this function takes.
56
00:04:10,390 --> 00:04:17,649
Again, going back to the input function, this function used to take a string as an input and then
57
00:04:17,649 --> 00:04:20,560
do stuff with it and display it on screen as we know.
58
00:04:21,220 --> 00:04:28,390
So when we're defining our function here, we need to specify what inputs it takes.
59
00:04:28,720 --> 00:04:35,140
So we know in order to change the Mac address, we need an interface and we need a mac address.
60
00:04:35,350 --> 00:04:38,020
So we're going to call these interface.
61
00:04:38,960 --> 00:04:42,350
And new mark again.
62
00:04:42,350 --> 00:04:44,490
The names here are completely up to you.
63
00:04:44,510 --> 00:04:46,880
You can use any name that you find suitable.
64
00:04:46,880 --> 00:04:51,980
You can use X and Y if you want it, as long as they are valid variable names.
65
00:04:52,580 --> 00:04:59,570
Once you define the function name and the inputs that it takes, all you have to do is just put call
66
00:04:59,570 --> 00:05:03,440
on here at the end of the line and then hit enter.
67
00:05:03,950 --> 00:05:05,840
Now you'll see automatically.
68
00:05:05,870 --> 00:05:11,090
PY charm has indented this and put a tab for our cursor.
69
00:05:11,240 --> 00:05:17,340
This is to show that everything that we're going to write now is going to be part of the change Mac
70
00:05:17,360 --> 00:05:21,050
function and it's going to be part of this block of code.
71
00:05:21,410 --> 00:05:26,900
Now, the goal of this function to change the Mac address and we already know the instructions to change
72
00:05:26,900 --> 00:05:29,690
the MAC address, it's these instructions right here.
73
00:05:30,260 --> 00:05:35,870
So I'm just going to cut them, control X and paste them here.
74
00:05:36,780 --> 00:05:45,090
What's very important to note is all of the code that is part of this function need to be indented,
75
00:05:45,090 --> 00:05:46,980
the same indentation in here.
76
00:05:47,040 --> 00:05:54,030
So all of the code that started here, this is part of the normal Python program or script.
77
00:05:54,690 --> 00:05:59,910
Everything that's indented in here is part of the change mac function.
78
00:06:00,180 --> 00:06:05,490
This is how Python separates blocks of code based on indentation.
79
00:06:06,300 --> 00:06:06,970
That's it.
80
00:06:06,990 --> 00:06:08,340
Now our function is done.
81
00:06:08,340 --> 00:06:10,440
We have a function called change, Mark.
82
00:06:10,470 --> 00:06:12,840
It takes an interface and a new mark.
83
00:06:12,840 --> 00:06:18,030
And whenever we call this function, it will execute all the instructions in here.
84
00:06:18,480 --> 00:06:22,020
So if we don't call this, nothing is going to happen.
85
00:06:22,320 --> 00:06:23,850
Let me show you what I mean.
86
00:06:24,510 --> 00:06:31,080
If I go down on my Python program and run the same command that we used to run before, and let's change
87
00:06:31,080 --> 00:06:32,760
that to two, two at the end.
88
00:06:33,570 --> 00:06:36,120
You can see that we didn't get a print statement.
89
00:06:36,180 --> 00:06:43,590
And if I do, if config 88 zero, you'll see that the MAC address did not change.
90
00:06:43,920 --> 00:06:48,960
This is because we define the function here, but we never used it in here.
91
00:06:49,230 --> 00:06:53,760
So it's kind of similar to when we import a module but never use it.
92
00:06:54,450 --> 00:07:01,200
So now what's happening is whenever you run, Python is going to read this, read this, read that this
93
00:07:01,200 --> 00:07:04,290
is a function but not execute any of that.
94
00:07:04,320 --> 00:07:08,940
And then it will go back in here and start executing from the parser.
95
00:07:09,090 --> 00:07:12,120
It will read this stuff that the person enters.
96
00:07:12,150 --> 00:07:13,270
It will pass them.
97
00:07:13,290 --> 00:07:15,450
It will get the interface and all of that.
98
00:07:15,450 --> 00:07:18,240
But it will never call change Mac.
99
00:07:18,870 --> 00:07:22,650
So all we have to do in here is call change Mac.
100
00:07:23,850 --> 00:07:29,760
Now, as you can see, pie chart automatically knows what I need to give to this based on the definition
101
00:07:29,760 --> 00:07:30,420
in here.
102
00:07:30,630 --> 00:07:36,330
So it's telling me that I need to give it an interface and we know that the value for the interface
103
00:07:36,330 --> 00:07:39,060
is stored in options dot interface.
104
00:07:39,180 --> 00:07:45,360
So I'm actually going to use this directly and instead of using the interface variable in here.
105
00:07:45,480 --> 00:07:49,440
So I'm just going to do options that interface.
106
00:07:50,910 --> 00:07:57,990
And for the Mac we're going to use options dot new Mac and I'm going to delete these two lines because
107
00:07:57,990 --> 00:07:58,980
we don't need them.
108
00:07:58,980 --> 00:08:05,670
They just store the value of options dot interface and interface and options dot new Mac any new Mac.
109
00:08:05,670 --> 00:08:07,590
But now I'm using it directly.
110
00:08:07,590 --> 00:08:13,410
I'm using options dot interface and options dot new Mac in the change mac function.
111
00:08:13,830 --> 00:08:18,780
So now when Python reaches this line, it's going to read, okay, this is a call for change.
112
00:08:18,780 --> 00:08:25,440
Mac, I'm going to use the interface as the first input here and use New Mac as the second input in
113
00:08:25,440 --> 00:08:26,010
here.
114
00:08:26,040 --> 00:08:27,510
Call this function.
115
00:08:27,510 --> 00:08:31,800
Execute all of this code which will change the MAC address for me.
116
00:08:33,000 --> 00:08:34,230
Let's test it out.
117
00:08:34,710 --> 00:08:35,840
Going down.
118
00:08:35,850 --> 00:08:38,610
Run the same exact command that we did before.
119
00:08:38,610 --> 00:08:41,400
But it didn't work right now.
120
00:08:41,400 --> 00:08:47,070
It printed the statement for me, which means that it actually did get into this block of code.
121
00:08:47,460 --> 00:08:55,740
And then if we do, if config 88 zero, you'll see that the MAC address did change to the MAC address
122
00:08:55,740 --> 00:08:56,610
that I want.
123
00:08:57,730 --> 00:09:00,820
Now, as you can see, the code already looks neater.
124
00:09:00,880 --> 00:09:07,480
And now every time I need to change the Mac address, I won't have to use these four lines anymore.
125
00:09:07,510 --> 00:09:12,160
All I have to do is just call change Mac anywhere I want in my code.
126
00:09:12,190 --> 00:09:16,240
Give it the interface, give it the new Mac and it will change it for me.
127
00:09:16,630 --> 00:09:22,780
Not only that, but in the future you'll learn how to put this in a special file and be able to call
128
00:09:22,780 --> 00:09:25,240
it from any Python program that you want.
129
00:09:25,630 --> 00:09:31,630
This will allow you to change the Mac address within any program that you write without having to worry
130
00:09:31,630 --> 00:09:34,300
about the code used in this function.
131
00:09:34,420 --> 00:09:39,820
Something similar to the way that we use input, print row input and so on.
13133
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.