Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As we go through the examples in this course, and as you look at scripts that you find from different sources, and as you write your own scripts, you are going to notice a few things that are constant in how these scripts are constructed. If we look at the syntax.py file here in our 04 Syntax folder in Exercise Files, you will notice a few things. First of all, at the very top, there is this line with a Pound sign, which is the Comment Indicator in Python and an exclamation point and then a path to the Python Interpreter.
This is used in environments that run the script from a shell, UNIX-based environments like a Mac or Linux environment, and one of the beauties of a scripting language like Python is that your script can run in a number of different environments without change. So it's important that you leave this as the first line of the file. Now this path here that starts with the first slash must be the path to the Python Interpreter. Now this is a very common path: /user/bin/ python3, that will work in a lot of environments.
If you try to run this in UNIX- based environments and you find that it doesn't work, one thing that you'll want to look at is this path to see if the path needs to change. Another thing that you will notice is this line down here at the end. What this does is it allows us to run the script with the functions in any order that we want. Without this line and without the main execution of the script being inside a function called main, then you wouldn't be able to run functions that are defined after the function is called.
In other words, if I put a function in here, and I say def egg(): and print("egg") now I'll need to call the egg here, egg() and save and run, you will notice that that runs. Now if I were to take out this def main and just make this like that without it being in any function at all, and we will get rid of this, so you might want to just call egg from here and then define egg down there, that will not work.
That will give us a syntax error because egg is not defined at the point where we call it. So what this allows us to do is to define functions after they are called, and this is actually very useful and very common, so you will see this construct in a lot of the scripts that you encounter. If we just have it this way, it would serve the same purpose. The reason for this part of it here is it allows this to only be run when this file is called as the main module.
Later on in the course, we will get into writing modules, and modules typically contain classes and functions. A lot of times when you write a module you are going to want to have a test suite at the end of the module, and this allows you to put that in a main function, and it will only run if the module is called as the main module. It won't run when the module is included in other modules. So you will see this pattern a lot, and that's basically what it does and what it's for, and it's just a good idea to always stick it in your scripts.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97410 Viewers
61 Video lessons · 84638 Viewers
71 Video lessons · 68850 Viewers
56 Video lessons · 101308 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.