Receive a quick introduction to the major parts of a Python script—comments, import statements, and the body of your script.
- [Instructor] A Python script is simple, but it's somewhat different from other scripting languages. Let's take a look at the different parts of a Python script. Here in Komodo, I've opened a working copy of hello-version.py from Chapter Two of the exercise files, and up here at the top, you'll see the first two lines are comments. So comments are introduced by the hash mark or the pound sign, and the first comment is a special case of a comment. It's called a shebang line or sometimes hashbang.
The shebang line is a common pattern for Unix based systems. It allows a script to be invoked from the command line. There are two parts to the shebang line. The hash mark and the exclamation sign must be the first two characters on the first line of the file. These marks together form the shebang. An exclamation mark is sometimes called a bang, so hash plus bang is where the word shebang comes from. There must be no lines before this line, and no spaces before these first two characters.
Essentially, these two characters must be the first two bytes in the file. After the shebang, is the path to the executable that will run your script, along with any other optional arguments. In this case, we're using the Unix env command to find the path to the Python interpreter. This is a common usage. Alternately, we could use the absolute path to the Python interpreter. I didn't use this method because these example scripts must operate on different installations, and I cannot predict the path of the Python interpreter on different systems.
For our purposes, I use the env command in the examples for this course. If you're on one of those systems that is not based on Unix, like perhaps Windows, this line, because it starts with the comment character, will be completely ignored. On Macs, it is actually functional because Macs are based on BSD Unix. Following the comments, the first line in the script is an import statement. This tells the Python interpreter to import a module from the library.
The module may contain any combination of classes, functions, or other Python objects. In this case, it's importing the platform module, which is used to display the Python version when we run the script. There are standard library modules or you may define your own. A Python script may have multiple import statements or you may specify multiple modules in one import statement. After the import statements, you find the rest of the script. In this case, it's this one line which is a call to the print function.
In versions of Python before version Three, print was a statement. In Python Three, print is now a function, and it always requires parentheses. Inside the parentheses is a string, and you notice that because everything is an object in Python, we're actually calling a function on this string object, the format function, and that format's the text inside these curly braces. We'll get into more detail on the print function later in this course, and also on string objects. So when I run this, and I'm pressing shift + command + r here on my Mac or control + shift + r on a PC, and there we have the output, and you notice it says, "This is Python version", and it gives the version number which we're getting from the platform module.
So there's a common variation that I'd like to show you. I'm going to switch to this other tab here. Here I have a working copy of hello main.py from Chapter Two of the exercise files. This is functionally the same as the other script. It calls this print function with the format and the Python version. You notice that the print function is actually called from inside a message function, and message is called from main, and then we call main down here at the bottom in this conditional statement.
This is actually a very common pattern in Python. By having this conditional statement at the bottom that calls main, it actually forces the interpreter to read the entire script before it executes any of the code. This allows a more procedural style of programming, and it's because Python requires that a function is defined before it's called. Again, we'll get into a lot more detail about this later, but you'll notice that when I run this, it gives us exactly the same output. So I've described the general anatomy of a Python script.
It's pretty simple, and we'll get into more detail about each of these parts later in this course.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api