Math and Python Home Page Skylight Publishing



Mathematics for the Digital Age
and
Programming in Python

>>> Second Edition:
              with Python 3

Appendix A: Getting Started with Python


 

We are grateful to Jim Gettys for contributing the Mac and Linux sections.


A.1   Introduction

Python is distributed under the open source license that makes it free to use, even for commercial products.

To obtain Python go to www.python.org and click Download.  Download the current production version for your operating system and CPU, for example, Python 3.6.5 Windows x86-64 executable installer (python-3.6.5.exe) for a 64-bit Windows system. 

The Python distribution at python.org includes the Python interpreter, a very simple development environment, called IDLE, libraries, tools, and documentation (a tutorial, a reference manual, etc.).

Python is preinstalled on Linux and Mac systems, but it may be an old version, and a convenient interactive development environment may not be present by default.  Windows does not come with Python installed.  The configuration of IDLE and file path syntax differ slightly between operating systems, though Mac OS X and Linux will often be very similar, due to their UNIX heritage.


A.2   Installing and Configuring under Windows

Download the appropriate Python installer file and save it in a folder of your choice or to Desktop.

Double-click on the saved .exe file to run the installer.  Accept the defaults, only consider installing Python in C:\Program Files\Python36 rather than in the suggested C:\python36.  The installation includes the Python interpreter, IDLE — a very simple development environment with a graphical user interface (GUI), libraries, tools, and documentation (a tutorial, a reference manual, etc.).

It is possible to run Python in a Command Prompt window, but for most purposes it is more convenient to use IDLE, rather than the “raw” command-line user interface.  To run IDLE, on Windows 10 type "idle" in the search box and click on "IDLE desktop app" to run.

It is better to create a shortcut to IDLE on the desktop.  To do that, type "idle" in the search box, righ click on "IDLE desktop app" and choose "Open file location." Drag "IDLE" shortcut to the desktop while holding the Ctrl key down to create a copy (you should see a small "+" symbol next to the icon). 

As other programs, IDLE uses the concept of the working directory — the folder where the interpreter looks for Python programs and data files.  By default, the working directory is set to the same folder where Python is installed.  It is not a good idea, though, to place your files into the installation folder.  Instead, reconfigure the shortcut you created to specify your own working directory.  To do that, right-click on the shortcut and choose Properties.  In the "Start In:" line, enter your work folder, for example C:\mywork.  Click OK.

Figure 1

(If your working directory name has spaces in it, put it in double quotes.)

Check your configuration:

  1. Double-click on the shortcut icon to start the interpreter
  2. At the prompt, enter: >>> from os.path import abspath >>> abspath('.') Python responds: 'C:\\mywork'
Here \\ stands for one backslash.  abspath('.') returns the absolute path of the current directory.

It is possible to change the working directory from the interpreter itself.  For example:

>>> from os import chdir >>> chdir("C:/myOtherWork")

By default, Windows operating systems hide file extensions.  When you are developing programs, it is more convenient to see the extensions.  To enable extensions in Windows 10, click View on any folder and check the "File name extensions" box.


A.3   Installing and Configuring under Mac OS X

A Python 2 interpreter is installed on all Mac systems, but historically it has been several years out of date, and IDLE is not included.  You should download and install the current release.  Download and install the latest version of Python and IDLE (for example, 3.6.5 as of June 2018), which will create a Python application in your Application folder.  You can drag an icon out of this folder to your desktop for convenient access.  If you have more than one version of Python and IDLE installed, make sure that you use Python 3 when working with this book. 

There are many demos of Python code provided with Python in the installation.  You can find them in Applications/Python 3.6/Extras/Demo.

IDLE uses an old monospace font by default.  You may want to use a more modern monospace font.  The "Andale Mono" font distinguishes the numeral zero from the letter "O", and the numeral one from the letter "l"; it is designed specifically for coding.  The IDLE Options‑>Fonts/Tabs dialog wll allow you to set it as IDLE's default font.

If you use IDLE on several operating systems at the same time, you may find it very confusing if the key bindings differ between those systems.  IDLE allows you to select whether these bindings follow Mac, UNIX, Windows or other historical key bindings.  Select the key bindings you prefer in the IDLE Options‑>Keys dialog.


A.4   Installing and Configuring under Linux

Python is an essential component of Linux systems and it is kept up to date and very well supported.  Any current Linux distribution will have available several versions of Python and corresponding IDLE versions.  Python 3.6 (or later) may or may not be available prepackaged in your distribution at this date (June 2018).  You should use your Linux distribution binary packages of Python 3.6 or later, if available.  If not, you should download and install the latest version from python.org (Python 3.6.5 as of June 2018).  The details of installing these software packages depend on the Linux distribution and desktop environment.

If you install a particular version of IDLE, Linux software installation package systems will automatically ensure that the version of Python it depends on is also installed.  More than one Python version can be installed at the same time.  They won't conflict with each other if they have different names (e.g., /usr/bin/python2.7 and /usr/bin/python3.6).  The command python in a shell will resolve to some default version of Python, which might very well be (in fact, is likely to be) Python 2, rather than Python 3. 

The examples and documentation shipped with Python from python.org will be packaged separately (in packages named something like python3.6-examples and python3.6-doc).  They should be installed separately, and would likely be found in the /usr/share/doc/python3.6 directory.

If installed via your Linux distribution packages (the recommended way), IDLE will appear in the Applications‑>Programming‑>IDLE menu automatically; if not, you can add a menu definition yourself.  You will likely want to create a shortcut to IDLE on the desktop, or panel.  To do that, go to Applications/Programming and drag IDLE (using Python 3.6) to the desktop or panel, while holding the left button down.  You can also create a shortcut to the Python documentation: start IDLE, press F1 (or select the HELP menu entry), and drag the URL from your web browser to where you want the documentation icon.

You can run Python in a terminal window, but, for most purposes, it is more convenient to use IDLE, rather than the “raw” command-line user interface.  To run IDLE, go to Applications‑>Programming and choose IDLE (using Python 3.6).

By default, Linux's IDLE uses an old monospace font.  You may want to configure the font to a more modern monospace font designed for programming.  "DejaVu Sans Mono" or "Liberation Mono" both distinguish the numeral zero from the letter "O" and the numeral one from the letter "l".  The Options‑>Configure Idle‑>Fonts dialog wll allow you to select one of these fonts.

If you use IDLE on several operating systems at the same time, you may find it very confusing if the key bindings differ between those systems.  IDLE allows you to select whether these bindings follow Mac, UNIX, Windows or other historical key bindings.  Select the key bindings you prefer in the Opions‑>Configure Idle‑>Keys dialog.

As other programs, IDLE uses the concept of the working directory — the folder where the interpreter looks for Python programs and data files.  By default, the working directory is set to your home directory (e.g. /home/yourname). IDLE keeps track of which files you have recently browsed; so if you want to reopen a file in a particular location, you can use File‑>Recent Files to work in the same location.


A.5   Running Python Programs

A short program can be typed directly into the interpreter: >>> print('Hello, World!') Hello, World! Or: >>> n = 1 >>> sum1toN = 0 >>> while n <= 5: sum1toN += n print("{0:3d}: {1:5d}".format(n, sum1toN)) n += 1 1: 1 2: 3 3: 6 4: 10 5: 15 This is not very practical, though, because you have to reenter every statement to run the program again or to make a small change to it.  In IDLE, you can copy one of the previous statements: click on it or move the cursor up to the appropriate line and press <Enter>.  You can then edit the statement.  Still reentering every statement of a program would be too tedious.

It is more practical to save the program statements in a file and execute the program from the file.  A file that contains the text of a program is called a source file.  Python source files usually have the extension .py

You could create a source file using any text editor, for example Notepad.  You could even use a word processor — just make sure you save your file as a "text only" file, and that you replace the default .txt extension in the file name with .py.  However, the easiest way to write a short Python program is by using IDLE's own built-in editor.

To open a new editor window in IDLE, choose New File from the File menu (or press Ctrl-N).  Type in the text of your program.  For example:

Figure 2

The IDLE editor automatically increases indentation for statements that expect it: def, while, if, and so on.  Press Backspace to decrease the indentation level.  Choose Save As... from the File menu or press Ctrl+Shift+S to save the program in a file.  Use the .py extension with the file name.  Save the file in a folder of your choice, for example, C:\mywork.

While an IDLE editor window is open and active, you can test your program by choosing Run Module from the Run menu or simply by pressing F5.

If your program has syntax errors, Python will alert you to that and highlight the first error.  For example:

Figure 3

(a semicolon instead of a colon).

You can have several editor windows open at once and cut and paste text within the same window or from one window to another.  Highlight the text you want to copy, press Ctrl-C to copy the text, position the cursor at the insertion point, press Ctrl-V to paste the text.


A.6   Where to Go from Here

The IDLE environment is adequate for working on programs in our Math and Python book. Most of the programs are just a few lines of code, only one or two exceed half a page.  There are many professional development tools for Python programmers, of course.  See http://wiki.python.org/moin/DevelopmentTools.




Copyright © 2010-2018 by Skylight Publishing
support@skylit.com