1
00:00:00,000 --> 00:00:03,990
As we go through the examples in this course, and as you look at scripts that you

2
00:00:04,000 --> 00:00:07,990
find from different sources, and as you write your own scripts, you are going to

3
00:00:08,000 --> 00:00:13,990
notice a few things that are constant in how these scripts are constructed.

4
00:00:14,000 --> 00:00:20,990
If we look at the syntax.py file here in our 04 Syntax folder in Exercise Files,

5
00:00:21,000 --> 00:00:22,990
you will notice a few things.

6
00:00:23,000 --> 00:00:26,990
First of all, at the very top, there is this line with a Pound sign, which is

7
00:00:27,000 --> 00:00:32,990
the Comment Indicator in Python and an exclamation point and then a path to

8
00:00:33,000 --> 00:00:34,990
the Python Interpreter.

9
00:00:35,000 --> 00:00:39,990
This is used in environments that run the script from a shell, UNIX-based

10
00:00:40,000 --> 00:00:44,990
environments like a Mac or Linux environment, and one of the beauties of a

11
00:00:45,000 --> 00:00:48,990
scripting language like Python is that your script can run in a number of

12
00:00:49,000 --> 00:00:51,990
different environments without change.

13
00:00:52,000 --> 00:00:55,990
So it's important that you leave this as the first line of the file.

14
00:00:56,000 --> 00:01:00,990
Now this path here that starts with the first slash must be the path to

15
00:01:01,000 --> 00:01:01,990
the Python Interpreter.

16
00:01:02,000 --> 00:01:06,990
Now this is a very common path: /user/bin/ python3, that will work in a lot of environments.

17
00:01:07,000 --> 00:01:09,990
If you try to run this in UNIX- based environments and you find that it

18
00:01:10,000 --> 00:01:14,990
doesn't work, one thing that you'll want to look at is this path to see if the

19
00:01:15,000 --> 00:01:15,990
path needs to change.

20
00:01:16,000 --> 00:01:19,990
Another thing that you will notice is this line down here at the end.

21
00:01:20,000 --> 00:01:24,990
What this does is it allows us to run the script with the functions in any

22
00:01:25,000 --> 00:01:26,990
order that we want.

23
00:01:27,000 --> 00:01:32,990
Without this line and without the main execution of the script being inside a

24
00:01:33,000 --> 00:01:36,990
function called main, then you wouldn't be able to run functions that are

25
00:01:37,000 --> 00:01:39,990
defined after the function is called.

26
00:01:40,000 --> 00:01:46,990
In other words, if I put a function in here, and I say def egg():

27
00:01:47,000 --> 00:01:56,990
and print("egg") now I'll need to call the egg here, egg() and save and run,

28
00:01:57,000 --> 00:01:59,990
you will notice that that runs.

29
00:02:00,000 --> 00:02:03,990
Now if I were to take out this def main

30
00:02:04,000 --> 00:02:09,990
and just make this like that without it being in any function at all, and we will

31
00:02:10,000 --> 00:02:16,990
get rid of this, so you might want to just call egg from here and then define

32
00:02:17,000 --> 00:02:18,990
egg down there, that will not work.

33
00:02:19,000 --> 00:02:23,990
That will give us a syntax error because egg is not defined at the point where we call it.

34
00:02:24,000 --> 00:02:30,990
So what this allows us to do is to define functions after they are called, and

35
00:02:31,000 --> 00:02:35,990
this is actually very useful and very common, so you will see this construct in a

36
00:02:36,000 --> 00:02:39,990
lot of the scripts that you encounter.

37
00:02:40,000 --> 00:02:43,990
If we just have it this way, it would serve the same purpose.

38
00:02:44,000 --> 00:02:48,990
The reason for this part of it here is it allows this to only be run when this

39
00:02:49,000 --> 00:02:51,990
file is called as the main module.

40
00:02:52,000 --> 00:02:56,990
Later on in the course, we will get into writing modules, and modules typically

41
00:02:57,000 --> 00:02:58,990
contain classes and functions.

42
00:02:59,000 --> 00:03:02,990
A lot of times when you write a module you are going to want to have a test

43
00:03:03,000 --> 00:03:08,990
suite at the end of the module, and this allows you to put that in a main

44
00:03:09,000 --> 00:03:12,990
function, and it will only run if the module is called as the main module.

45
00:03:13,000 --> 00:03:17,990
It won't run when the module is included in other modules.

46
00:03:18,000 --> 00:03:22,990
So you will see this pattern a lot, and that's basically what it does and what

47
00:03:23,000 --> 00:03:33,000
it's for, and it's just a good idea to always stick it in your scripts.

