|
|
||
Script ModeIn interactive mode, Python displays the results of expressions. In script mode, however, Python doesn't automatically display results. In order to see output from a Python script, we'll introduce the print statement. This statement takes a list of values and prints their string representation on the standard output file. The standard output is typically directed to the Terminal window. We'll revisit this in depth in The print Statement. print "PI = ", 355.0/113.0 We can have the Python interpreter execute our script files.
Application program scripts can be of any size or complexity. For the
following examples, we'll create a simple, two-line script, called
Example 3.1. example1.py print 65, "F" print ( 65 - 32 ) * 5 / 9, "C" There are several ways we can start the Python interpreter and have it evaluate our script file.
Running Python scripts from the command-line applies to all operating systems. It is the core of delivering final applications. We may add an icon for launching the application, but under the hood, an application program is essentially a command-line start of the Python interpreter. Explicit Command LineThe simplest way to execute a script is to provide the script file name as a parameter to the python interpreter. In this style, we explicitly name both the interpreter and the input script. Here's an example. Example 3.2. Command Line Execution python example1.py This will provide the Implicit Command-Line ExecutionWe can streamline the command that starts our application. For
POSIX-standard operating systems (GNU/Linux, UNIX and MacOS), we make
the script file itself executable and directing the shell to locate the
Python interpreter for us. For Windows users, we associate our script
file with the
Now you can run a script in most GNU/Linux environments by saying: ./example1.py
Windows Setup. Windows users will need to be sure that
For Windows programmers, the windows command interpreter uses the
last letters of the file name to associate a file with an interpreter.
You can have Windows run the
POSIX Setup. We have to be sure that the Python interpreter is in value of
the The #! technique depends on the way all of the POSIX shells handle scripting languages. When you enter a command that is the name of a file, the shell must first check the file for the "x" (execute) mode; this was the mode you added with chmod +x . When execute mode is true, the shell must then check the first few bytes to see what kind of file it is. The first few bytes are termed the magic number; deep in the bowels of GNU/Linux there is a database that shows what the magic number means, and how to work with the various kinds of files. Some files are binary executables, and the operating system handles these directly. Other files, beginning with "#!", are script files. The rest of this first line names the program that will interpret the script. In this case, we asked the env program to find the python interpreter. The shell finds the named program and runs it automatically, passing the name of script file as the last argument to the interpreter it found. The very cool part of this trick is that "#!" is a comment to Python. This first line is, in effect, directed at the shell, and ignored by Python. The shell glances at it to see what the language is, and Python studiously ignores it, since it was intended for the shell. Another Script ExampleThroughout the rest of this book, we're going to use this script processing mode as the standard way to run Python programs. Many of the examples will be shown as though a file was sent to the interpreter. For debugging and testing, it is sometimes useful to import the program definitions, and do some manipulations interactively. We'll touch on this in the section called "Hacking Mode". Here's a second example. We'll create a new file and write another
small Python program. We'll call it
Example 3.3. example2.py #!/usr/bin/env python """Compute the odds of spinning red (or black) six times in a row on an American roulette wheel. """ print (18.0/38.0)**6 This is a one-line Python program with a two line module document string. That's a good ratio to strive for. After we finish editing, we mark this as executable using
When we run this, we see the following.
Which says that spinning six reds in a row is about a one in eighty-nine probability. |
||