2.4.5. Python: Workflow and learning strategy

In this section we discuss how to work with Python, trying to motivate a particular strategy for learning it.

  1. Start out using Jupyter notebook. Selected lectures and homework assignments will have associated notebooks, with worked-out examples showing you exactly what goes in to Python and what comes out. Follow the examples closely, copy them, and try to insert new code to do homework problems or to generate examples of your own.

  2. Gradually wean yourself away from the notebook, working in text files to produce scripts that can be executed independently. One way to start doing this is take some ideas you have developed in a notebook and turn them into an independent program.

We discuss these two stages below, beginning with some hints on how to start up and interact with IPython notebook and the recommended workflow, and then moving on to how to write independent programs.

2.4.5.1. Stage one: Using IPython or Jupyter notebook

As detailed above, the recommended way to start out with Python is to use Jupyter notebook. The most common way to do that in this course will be to click on a notebook link on the course outline. This will take you to a strange-looking text file with curly braces, colons, and strings of English text. This is what a Jupyter notebook really looks like, but the problem is that you are looking at the notebook, you are not running it. To run it, you need to save the file to your own computer, using the “Save As”Command in your browser’s “File” Menu. When you save it your local computer, don’t change the file name. For example, the first notebook assignment file in this course is called running_python.ipynb. Save it in some directory/folder where you keep normal coursework files.

You are now ready to run the notebook file.

To do that you do:

gawron$ jupyter notebook  --pylab

Both ipython and jupyter notebook have excellent online tutorials introducing you to the basics.

  1. Ipython help

  2. Notebook tutorial/help

  3. Markdown language help (for adding text and comments to your notebooks, which you’ll be doing on assignments)

The notebook in particular has a very intuitive user interface that is very easy to pick up, with a well-designed help menu for those features which are a little harder to remember (like keyboard shortcuts).

Very well. The notebook is easy to use. But just because something is easy to use doesn’t mean you should use it. Why use the notebook?

Jupyter notebook is a tool we use to help get you get started writing Python. It allows you to think about Python one line at a time, then a few lines at a time, and to go through the process of getting something to woork on one example, then generalizing into reusable code.

Later on, Jupyter notebook might be a tool you use for working out pieces of a program, or for sharing some ideas, or for producing slides, if it comes to that. But your scripting skills advance, you will find it necessary to produce scripts that can live on their own as executable programs. This is when you need stage two.

2.4.5.2. Stage two: Text editor or IDE?

This section assumes you have installed Python and that you know how to start up Python. We discuss other ways of interacting with Python.

The next thing you will want to do is to write a simple program of your own, either using ideas you have developed in some Jupyter notebook sessions, or on a piece of paper. Often this entails something fairly scarey, starting out with a blank screen that you are now supposed to fill with code.

There are two ways of writing a program and running it. The first way is to use an Integrated Development Environment (IDE), which is an interactive program that lets you write, edit, and run your code. When you run your code, you typically see the results immediately in a new window, and typically you can interact with that new Window just as would with Python, gathering information to help you improve or debug your code.

The second way is to use a text editor that lets you save your files as text files (which is what Python program files are), without inserting any funny characters or removing line breaks or doing anything that interferes with the intended meaning of your program. Then you can run the program in Python in a window separate from the editor window, using either simple Python or your favorite interactive Python shell, gather information, and edit again. Typically the editor for this purpose will not be Microsoft Word (although it is possible), but something designed for this much simpler purpose.

The distinction between IDE and editor is actually sort of a continuum, since many IDEs offer a rich assortment of editing aids, and since many editors offer the option of creating a new window in which you can interact with Python.

We discuss the options in more detail below.

2.4.5.3. Using a text editor

Let us say you have chosen to write your first program in a text editor.

Here are some popular choices.

  1. Spyder (Comes with Anaconda).

  2. PythonWin (Windows only). Available either as part of ActiveState’s ActivePython distribution (which is not open source) or as part of the Win32All extensions from Mark Hammond’s pages (which is open source). Note: the Win32 pages were running in Test mode after a Wiki attack in January (as of August 12, 2013). You just click on the Front Page link on the left to see the usual content.

  3. TextWrangler (Macintosh only)

  4. Emacs. Very general editor, used for both general text editing and code editing, with good Python facilities. The downside is that it is both a big program and not particularly easy to learn.

  5. Komodo Edit Open source editor, very much targeted toward Python.

2.4.5.4. Using an IDE

Let us say you have chosen to write your first program using an IDE.

Here are some popular choices.

  1. IPython. This also comes with the Anaconda distribution with a whole set of desirable features and is a very serviceable option. Available at ipython.org if you don’t have the Anaconda distribution. This is the recommended option for this course.

  2. Idle (comes with all standard Python installations). Just type idle to a command window to try it out.

  3. Komodo IDE. Komodo is an award winning Python IDE from ActiveState. Fully-integrated Python Python 2.x and Python 3 support featuring code intelligence with autocomplete and calltips, Python debugger (includes remote debugging), interactive shell, remote file support, macros, templating, emacs command support and great help documentation.

There really are a number of options. The following list was posted on StackOverflow.com.

 1
 2                                       Rapid Application Development -.
 3                                           Integrated DB Support -+   |
 4                                                GUI Designer  -+  |   |
 5                                             Unit Testing -+   |  |   |
 6                                        Code Templates -.  |   |  |   |
 7                                       Code Folding -+  |  |   |  |   |
 8                           UML Editing / Viewing -+  |  |  |   |  |   |
 9                              Line Numbering -+   |  |  |  |   |  |   |
10                         Bracket Matching -+  |   |  |  |  |   |  |   |
11                          Smart Indent -+  |  |   |  |  |  |   |  |   |
12         Source Control Integration -+  |  |  |   |  |  |  |   |  |   |
13                   Error Markup  -+  |  |  |  |   |  |  |  |   |  |   |
14  Integrated Python Debugging -+  |  |  |  |  |   |  |  |  |   |  |   |
15    Multi-Language Support -+  |  |  |  |  |  |   |  |  |  |   |  |   |
16  Auto Code Completion -+   |  |  |  |  |  |  |   |  |  |  |   |  |   |
17 Commercial / Free --+  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
18 Cross Platform -+   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
19                _|___|__|___|__|__|__|__|__|__|___|__|__|__|___|__|___|_
20                |CP|C/F|AC|MLS|PD|EM|SC|SI|BM|LN|UML|CF|CT|UT|UID|DB|RAD|comments
21                +--+---+--+---+--+--+--+--+--+--+---+--+--+--+---+--+---+
22 BlackAdder     |Y | C |  |   |  |  |  |Y |  |  |   |Y |  |  |   |  |   |
23 BlueFish       |L |   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
24 Boa Constructor|Y | F |Y |   |Y |Y |  |Y |Y |Y | Y |Y |Y |  |   |  |   |
25 Canopy         |Y | F |Y |   |Y |  |  |Y |Y |Y |   |  |  |  |   |  |   |
26 ConTEXT        |W | C |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
27 DABO           |Y |   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
28 DreamPie       |  | F |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
29 Dr.Python      |  | F |  |   |  |Y |  |  |  |  |   |  |  |  |   |  |   |
30 Editra         |Y | F |Y | Y |  |  |Y |Y |Y |Y |   |Y |  |  |   |  |   |
31 Emacs          |Y | F |Y | Y |Y |Y |Y |Y |Y |Y | Y |Y |Y |Y |   |  |   |
32 Eric Ide       |Y | F |Y |   |Y |Y |  |Y |  |Y |   |Y |  |Y |   |  |   |
33 E-Texteditor   |W |   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
34 Geany          |Y | F |Y*| Y |  |  |  |Y |Y |Y |   |Y |  |  |   |  |   |* very limited
35 Gedit          |Y | F |Y¹| Y |  |  |  |Y |Y |Y |   |  |Y²|  |   |  |   |¹ with plugin ² sort of
36 Idle           |Y | F |Y |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
37 JEdit          |Y | F |  | Y |  |  |  |  |Y |Y |   |Y |  |  |   |  |   |
38 KDevelop       |Y | F |  | Y |  |  |Y |Y |Y |Y |   |Y |  |  |   |  |   |
39 Komodo         |Y |C/F|Y | Y |Y |Y |Y |Y |Y |Y |   |Y |Y |Y |   |Y |   |
40 NetBeans       |Y | F |Y | Y |Y |  |Y |Y |Y |Y | Y |Y |Y |Y |   |  | Y |
41 NotePad++      |W | F |  | Y |  |  |  |  |  |Y |   |  |  |  |   |  |   |
42 Pfaide         |W | C |Y | Y |  |  |  |Y |Y |Y |   |Y |Y |  |   |  |   |
43 PIDA           |LW| F |Y | Y |  |  |  |Y |Y |Y |   |Y |  |  |   |  |   |VIM based
44 PTVS           |W | F |Y | Y |Y |Y |Y |Y |Y |Y |   |Y |  |  |Y* |  | Y |*WPF bsed
45 PyCharm        |Y | C |Y | Y*|Y |  |Y |Y |Y |Y |   |Y |  |Y |   |  |   |* javascript
46 PyDev(Eclipse) |Y | F |Y | Y |Y |Y |Y |Y |Y |Y | Y |Y |Y |Y |   |  |   |
47 Pyscripter     |W | F |Y |   |Y |Y |  |Y |  |Y |   |  |Y |Y |   |  |   |
48 PythonWin      |W | F |Y |   |Y |  |  |Y |Y |  |   |Y |  |  |   |  |   |
49 SciTE          |Y | F |  | Y |  |Y |  |  |Y |Y |   |Y |Y |  |   |  |   |
50 ScriptDev      |W | C |Y | Y |Y |Y |  |Y |Y |Y |   |Y |Y |  |   |  |   |
51 SPE            |  | F |Y |   |  |  |  |  |  |  | Y |  |  |  |   |  |   |
52 Spyder         |Y | F |Y |   |Y |Y |  |Y |Y |Y |   |  |  |  |   |  |   |
53 Sublime Text   |Y | C |Y | Y |  |  |  |Y |Y |Y |   |  |Y |  |   |  |   |extensible w/python
54 TextMate       |M |   |  | Y |  |  |  |Y |Y |Y |   |Y |Y |  |   |  |   |
55 UliPad         |Y | F |Y | Y |Y |  |  |Y |Y |  |   |  |Y |Y |   |  |   |
56 Vim            |Y | F |Y | Y |Y |Y |Y |Y |Y |Y |   |Y |Y |Y |   |  |   |
57 WingIde        |Y | C |Y | Y*|Y |Y |Y |Y |Y |Y |   |Y |Y |Y |   |  |   |* support for C
58 Zeus           |W | C |  |   |  |  |Y |Y |Y |Y |   |Y |Y |  |   |  |   |
59                +--+---+--+---+--+--+--+--+--+--+---+--+--+--+---+--+---+
60                |CP|C/F|AC|MLS|PD|EM|SC|SI|BM|LN|UML|CF|CT|UT|UID|DB|RAD|
61                |__|___|__|___|__|__|__|__|__|__|___|__|__|__|___|__|___|

Source Stack Overflow

If you really want another up-to-date list of some of the options has been posted on this page Python Editors page for a list of editors and IDEs. Most of the options offered are free. This is a good thing, not a bad thing, as we explain below.