Python - not just the name of CMU's Common Lisp compiler engine
Provided you use an advanced editor such as Emacs, Python is a pleasure to use. The flip-side is that without auto-indent, it can be difficult, introducing subtle errors as often seen by groups that uses a mix of several different editors.
The huge upside, however, is that everything you need is in one place. (If only the Lisp crowd would learn by this wonderful example...!)
- www.python.org
-
...that's all you really need to know for getting started with Python!
They make it so easy, regardless of whether you're a long-time seasoned professional programmer or have never seen a programming language before.
Everything else on this page is redundant because they've covered it all on Python.org !!!
Learning Python
New to the language or even new to programming?
Start here: http://python.org/topics/learn/
If you're familiar with at least one programming language such as Perl, Java, C++, SmallTalk, Lisp, etc the only printed book you need to purchase is the O'Reilly pocket reference series Python book.
(ISBN 0-596-00189-4; $12)
Just make sure it covers Python version 2! (that is, avoid used editions, as they're probably for Python 1.5)
There are few caveats about the language that can be addressed for those transitioning from another language: Keep reading.
Laugh
It's named Python because of MONTY PYTHON, so expect silly examples to play upon "spam & eggs" and "comfy chair" jokes or even ones from "confuse a cat."
Dead parrot references, however, are usually avoided.
(Refer to Monty Python's Flying Circus if you were deprived of a proper education: www.MontyPython.com)
Use a programming editor with auto-indent!
Python is like Java or C++ but without curly braces { } and semicolons ;
How, then, does it determine scope?-- Via indentation and white space.
Emacs
Emacs is highly recommended for syntax highlighting, auto-indent and more.
- BSD and Linux distributions have Emacs within your packages collections.
- Aquamacs: Emacs for MacOS X
- Carbon Emacs for MacOS X
- Emacs for windows
Some handy keystrokes:
First, M-
... denotes meta which is handled by the ALT key on
Windows. So, M-t
means ALT-t
. (All Emacs
documentation uses meta or the M- notation.)
Likewise, S-
... is for Shift, and C-
... is for
Control.
These may be used in combination: e.g., M-S-s
is ALT plus Shift
plus "s" key to search using a Regular Expression pattern (whereas
C-s
starts plain text searching).
-
M-/
completes the word, so if you previously typed TestSafe once, typing Tes followed byM-/
, you'll get the rest of the word completed for you. (If the wrong word appears, tapM-/
again to see the next choice.) -
Tab
auto-indents the current line based upon program scope. If the previous line isif chair == comfy:
hitting the Tab key places the cursor indented an additional four spaces. -
M-x indent-region
will automatically re-indent your code based upon syntax within the area of text selected.
There's too much to explain here, such as 800 levels of UnDo.
vi & vim
Users familiar with Unix are probably comfortable with vi
(or
"vim
" for Cygwin on Windows).
Enable auto-indent and syntax highlighting modes by typing:
:set autoindent :syntax on
Or add this to your ~/.virc
(or ~/.vimrc
) file:
set autoindent syntax on
You'll still have to add the customary four spaces to begin a new block, as
vi
lacks any native syntax recognition.
DrPython
For a straight-forward tool more like Wordpad but with syntax highlighting, get the DrPython programming editor for Windows: http://sourceforge.net/projects/drpython/
Unpack the Zip file contents under C:/Python24/ using its default folder name ("drpython-2.2.9").
Create a Windows Shortcut with target:
C:\Python24\pythonw.exe "C:\Python24\drpython-2.2.9\drpython.py"
DrPython requires wxPython libraries: http://wxPython.org/download.php#binaries
Install wxPython in its default location, C:\Python24\...
Note, however, that the default is to use Tab characters for auto-indented code rather than the customary four (4) spaces. Please change this immediately to avoid breaking other people's code.
Preserving syntax highlighting
If you must kill trees by printing your source code or simply wish to
maintain a nice visual representation of your code, consider using
py2html
or HTMLize
within Emacs.
Both are available by searching www.python.org.
The home page for py2html is http://www.peck.org.uk/p/python
Tutorials