Figure 3.2. Setting the PATH environment variable in Windows. For the ‘python’
command to be recognised by Windows systems the PATH environment variable must
include the location of the directory that contains the ‘python.exe’ file. An environment
variable may be set in the Windows graphical interface via Control Panel
→ System and
Security
→ System → Advanced system settings. If PATH is not already defined then the
Python executable location may be specified via New …, for example, as ‘C:\Python27’
or ‘C:\Python33’, depending on the version. If the PATH is already defined then, after
selection of this system variable in the lower table, using Edit … enables the addition of
the Python location after any existing values, after a semicolon, for example, adding
‘;C:\Python34’. Note that the PATH specification has no spaces between entries (only ‘;’)
and no trailing slash ‘\’.
The file name for Python scripts traditionally ends in ‘.py’, as illustrated in the example
below, although strictly speaking it does not have to. By running the script we send the file
containing lines of code to the Python interpreter, which reads it and acts on the contents.
Also, if required, a script can have input values called arguments passed along when it is
run, as illustrated for the Linux, UNIX or Mac command line:
> python scriptName.py argument1 argument2
This assumes that the executed ‘python’ command is on your search path, where the
operating system knows to look for commands; otherwise you have to type the full path to
its location.
The alternative to running Python from script files is to run Python alone, without a file,
in an interactive mode. This mode gives you a prompt ‘>>>’, where you can type manual
input that is passed to the interpreter one line at a time. To start the interpreter with the
Windows operating system you would click on the Python icon. To start from Mac OS X,
Linux or UNIX this means opening a command-line shell and typing ‘python’, then
pressing the Return or Enter key.
You can type commands at this prompt, pressing the Return or Enter key to issue each
command and move on to the next line. Note that, by using the ‘-i’ flag, it is also possible
to run a Python script and then go into an interactive mode immediately afterwards. When
the script is done it presents you with the prompt and awaits further instructions:
> python -i scriptName.py
[ Output of the script ]
>>>
When you are done, you quit by typing either the Ctrl-d key combination (so hold the
‘Ctrl’ key down and then tap the ‘d’ key) for Mac, Linux and UNIX systems or Ctrl-z for
Windows systems. The Python prompt is convenient for testing out simple bits of code.
More serious work is normally done with scripts, however. In this chapter you can work
either way.
In Python 2 you can print a text message to the terminal window via the print
command, for example:
print 'Hello world'
This automatically moves onto the next line because it prints a newline control
character at the end. However, if you do not want to go to the next line put a comma at the
end:
print 'Hello world',
In Python 3 the print statement changes to a function, which in simple terms means that
it requires parentheses:
print('Hello world')
This function is also available in Python 2, although it doesn’t print as nicely as in
Python 3.
2
The Python 2 syntax for print is fairly ubiquitous in existing Python code. For
new code it is probably best to use the Python 3 syntax, even when using Python 2. In this
book we will use the print function rather than the print statement.
Do'stlaringiz bilan baham: |