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
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

                                       Rapid Application Development -.
                                           Integrated DB Support -+   |
                                                GUI Designer  -+  |   |
                                             Unit Testing -+   |  |   |
                                        Code Templates -.  |   |  |   |
                                       Code Folding -+  |  |   |  |   |
                           UML Editing / Viewing -+  |  |  |   |  |   |
                              Line Numbering -+   |  |  |  |   |  |   |
                         Bracket Matching -+  |   |  |  |  |   |  |   |
                          Smart Indent -+  |  |   |  |  |  |   |  |   |
         Source Control Integration -+  |  |  |   |  |  |  |   |  |   |
                   Error Markup  -+  |  |  |  |   |  |  |  |   |  |   |
  Integrated Python Debugging -+  |  |  |  |  |   |  |  |  |   |  |   |
    Multi-Language Support -+  |  |  |  |  |  |   |  |  |  |   |  |   |
  Auto Code Completion -+   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 Commercial / Free --+  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 Cross Platform -+   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
                _|___|__|___|__|__|__|__|__|__|___|__|__|__|___|__|___|_
                |CP|C/F|AC|MLS|PD|EM|SC|SI|BM|LN|UML|CF|CT|UT|UID|DB|RAD|comments
                +--+---+--+---+--+--+--+--+--+--+---+--+--+--+---+--+---+
 BlackAdder     |Y | C |  |   |  |  |  |Y |  |  |   |Y |  |  |   |  |   |
 BlueFish       |L |   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 Boa Constructor|Y | F |Y |   |Y |Y |  |Y |Y |Y | Y |Y |Y |  |   |  |   |
 Canopy         |Y | F |Y |   |Y |  |  |Y |Y |Y |   |  |  |  |   |  |   |
 ConTEXT        |W | C |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 DABO           |Y |   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 DreamPie       |  | F |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 Dr.Python      |  | F |  |   |  |Y |  |  |  |  |   |  |  |  |   |  |   |
 Editra         |Y | F |Y | Y |  |  |Y |Y |Y |Y |   |Y |  |  |   |  |   |
 Emacs          |Y | F |Y | Y |Y |Y |Y |Y |Y |Y | Y |Y |Y |Y |   |  |   |
 Eric Ide       |Y | F |Y |   |Y |Y |  |Y |  |Y |   |Y |  |Y |   |  |   |
 E-Texteditor   |W |   |  |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 Geany          |Y | F |Y*| Y |  |  |  |Y |Y |Y |   |Y |  |  |   |  |   |* very limited
 Gedit          |Y | F |Y¹| Y |  |  |  |Y |Y |Y |   |  |Y²|  |   |  |   |¹ with plugin ² sort of
 Idle           |Y | F |Y |   |  |  |  |  |  |  |   |  |  |  |   |  |   |
 JEdit          |Y | F |  | Y |  |  |  |  |Y |Y |   |Y |  |  |   |  |   |
 KDevelop       |Y | F |  | Y |  |  |Y |Y |Y |Y |   |Y |  |  |   |  |   |
 Komodo         |Y |C/F|Y | Y |Y |Y |Y |Y |Y |Y |   |Y |Y |Y |   |Y |   |
 NetBeans       |Y | F |Y | Y |Y |  |Y |Y |Y |Y | Y |Y |Y |Y |   |  | Y |
 NotePad++      |W | F |  | Y |  |  |  |  |  |Y |   |  |  |  |   |  |   |
 Pfaide         |W | C |Y | Y |  |  |  |Y |Y |Y |   |Y |Y |  |   |  |   |
 PIDA           |LW| F |Y | Y |  |  |  |Y |Y |Y |   |Y |  |  |   |  |   |VIM based
 PTVS           |W | F |Y | Y |Y |Y |Y |Y |Y |Y |   |Y |  |  |Y* |  | Y |*WPF bsed
 PyCharm        |Y | C |Y | Y*|Y |  |Y |Y |Y |Y |   |Y |  |Y |   |  |   |* javascript
 PyDev(Eclipse) |Y | F |Y | Y |Y |Y |Y |Y |Y |Y | Y |Y |Y |Y |   |  |   |
 Pyscripter     |W | F |Y |   |Y |Y |  |Y |  |Y |   |  |Y |Y |   |  |   |
 PythonWin      |W | F |Y |   |Y |  |  |Y |Y |  |   |Y |  |  |   |  |   |
 SciTE          |Y | F |  | Y |  |Y |  |  |Y |Y |   |Y |Y |  |   |  |   |
 ScriptDev      |W | C |Y | Y |Y |Y |  |Y |Y |Y |   |Y |Y |  |   |  |   |
 SPE            |  | F |Y |   |  |  |  |  |  |  | Y |  |  |  |   |  |   |
 Spyder         |Y | F |Y |   |Y |Y |  |Y |Y |Y |   |  |  |  |   |  |   |
 Sublime Text   |Y | C |Y | Y |  |  |  |Y |Y |Y |   |  |Y |  |   |  |   |extensible w/python
 TextMate       |M |   |  | Y |  |  |  |Y |Y |Y |   |Y |Y |  |   |  |   |
 UliPad         |Y | F |Y | Y |Y |  |  |Y |Y |  |   |  |Y |Y |   |  |   |
 Vim            |Y | F |Y | Y |Y |Y |Y |Y |Y |Y |   |Y |Y |Y |   |  |   |
 WingIde        |Y | C |Y | Y*|Y |Y |Y |Y |Y |Y |   |Y |Y |Y |   |  |   |* support for C
 Zeus           |W | C |  |   |  |  |Y |Y |Y |Y |   |Y |Y |  |   |  |   |
                +--+---+--+---+--+--+--+--+--+--+---+--+--+--+---+--+---+
                |CP|C/F|AC|MLS|PD|EM|SC|SI|BM|LN|UML|CF|CT|UT|UID|DB|RAD|
                |__|___|__|___|__|__|__|__|__|__|___|__|__|__|___|__|___|

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.