Preparing to learn python guide

From Bio.scipy.org

Jump to: navigation, search

By Brandon W. King

Created: May 9th, 2007

With contributions by:

<insert name>: <description of changes>

Contents

Introduction to this guide

There already exists tutorials and books for getting started with Python, but I have noticed they leave out a little upfront information that can make your life better when using Python. Little tips and tricks that I only learned through interaction with more experienced Python programmers. My goal for this guide is to provide those tips & tricks as well as document a method I found useful for learning Python. Where there exists documentation (that I can find), I will try to reference it rather then rewriting the Python tutorials.

Getting prepared to learn python

Here is where I will mention a little that might make learning Python work better for you. What I mean by this, is that it is really easy to pick up bad habits (I picked up while I was learning Python, that I had to unlearn later). So bear with me while I give an overview of where you will start and what you can expect up ahead, so you can learn Python knowing how you will end up using it later on, and hopefully not have to unlearn much later on.

First it is important to understand there are two ways of using Python. There is the interactive interpreter and there is executing scripts saved in text files (Python modules). The trick is knowing when to use the interactive interpreter and when to write your code in a text file. Interactive Interpreter

The interactive interpreter allows you type in a line or block of Python code and see the result after pressing enter.

$ python
Python 2.5.1c1 (release25-maint, Apr 12 2007, 21:00:25)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello World!"
Hello World!

In the example above, I launched the interactive interpreter, and typed print "Hello World!". If you had made a mistake, like leaving off a closing quote, you receive an error message immediately:

>>> print "Hello World!
  File "<stdin>", line 1
    print "Hello World!
                     ^
SyntaxError: EOL while scanning single-quoted string

This makes learning Python easy. You can try out features of the language as you are learning them, and get feedback immediately when you are doing something wrong. Compared to other languages that do not have interactive interpreters, you have to write your code (or at least part of it), before compiling and receiving error message. Python Scripts/Modules/Packages oh my!

At some point you will want to reuse useful code you have figured out how to write and that is why you will want to save your code a file (.py extension). One confusing (to learn) but extremely useful aspect of Python are scripts, modules, and packages. Most of the tutorials leave how to create Python modules and packages until later on, but I believe that if you know they are coming and have a basic abstract concept of what they are for, you will be more prepared for them when the time comes.

First a summary and then I will go into some more details:

Script
Code is executed immediately. Great for writing programs which execute from the command-line.
Module
Reusable set of Python code, can be imported into other modules and scripts.
Package
A group of Python modules.

This is where I picked up my first bad habit while learning Python. See when you are first learning Python, you start with the interactive interpreter and then want to write a script to do something useful. For example, the classic hello world script.

What you are not told right away is that a python script is a module, but if the code in that file is written the way you would instinctively write it, the reusable aspect of it being a module will not work properly. Let me use the hello world example to make this more clear.

First there is hello world in the interactive interpreter:

>>> print "Hello World!"
Hello World!

Fairly simple. Now if we create new python script called "helloworld.py" and put the following code in it:

print "Hello World!"

And then we run the script with Python from the command line:

$ python helloworld.py
Hello World!

We see that it printed "Hello World!". Now for the module aspect. If you launch the python interpreter from the same directory as helloworld.py (details on starting the interactive interpreter and command-line execution will be explained later), you can use your helloworld.py module from within the interactive interpreter:

$ python
Python 2.5.1c1 (release25-maint, Apr 12 2007, 21:00:25)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
Hello World!
>>>

The import command which you will learn about more later, is used to import python modules and packages. In this case, we used it to import our helloworld.py script. Notice that after I typed "import helloworld" it automatically executed the print statement? Most of the time you do not want your code to execute upon being imported. We can fix this by creating functions or class in the module. I will create a new file called helloworld.py that has the print statement in a function instead (Note: Later on you will learn about functions and whitespace, but for now just pay attention to the code and the behavior it has when it is executed from the interpreter or command line) . helloworld2.py:

def myPrintFunction():
  print "Hello world2!"

Now if you launch the python interpreter from the same directory as helloworld2.py. I can import helloworld2 and then call the myPrintFunction():

$ python
Python 2.5.1c1 (release25-maint, Apr 12 2007, 21:00:25)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld2
>>> helloworld2.myPrintFunction()
Hello world2!

As you can see above, "Hello world2" was not printed until I called the function. This means myPrintFunction() is reusable in other code and in the interactive interpreter. =o) But now let us call execute helloworld2.py from the command line:

$ python helloworld2.py
$

Hmm, now helloworld2.py called from the command line did not print anything (The $ means there is a command prompt). In this case Python defined the function and then exited. Not very useful as command line program if you ask me.

Now here is the trick that you normally do not learn right away while learning python. There is a simple if statement you can add which will allow you to have code that is executed only when it is executed from the command line, while allowing it to still behave as a reusable Python module. Let us add this to a new file called helloworld3.py:

def myPrintFunction():
  print "Hello world3!"

if __name__ == "__main__":
  print "Calling from command line."
  myPrintFunction()

The 'if __name__ == "__main__":' piece of code is basically saying "Only execute the block of code below if I am executed from the command line, and not execute when imported as a module. Now let us import helloworld3.py into the python interpreter and watch it's behavior:

$ python
Python 2.5.1c1 (release25-maint, Apr 12 2007, 21:00:25)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld3
>>> helloworld3.myPrintFunction()
Hello world3!

Just as before we were able to call myPrintFunction() when ever we want, but notice it did not print "Calling from command line" or "Hello world3!" immediately after the import statement. Now let us call helloworld3.py from the command line:

$ python helloworld3.py
Calling from command line.
Hello world3!

Now we have output from the command line and we can reuse the code in other modules and in the interactive interpreter!!! =o)

To summarize, if you can write you Python scripts as follows:

<functions and classes here>
if __name__ == "__main__":
  <command line code here>

Then your code will run as script and be reusable as a Python module. The trick is to write most of your reusable code in functions or classes so that they can be called from the interactive interpreter, other modules, and in the <command line code here> section of your script. Note that you can always write a script to be useful at the command line level only or at the module level only and there are reason why you would wish to do this, but I have found that practicing with this method makes it easier to turn prototype scripts into useful reusable Python modules. Learning Python Method

Now that you have basic, potentially abstract and blurry understanding of the interactive interpreter, scripts, modules, and packages. Here is what I recommend you should do while you learn Python:

  1. Learning by tutorial
    1. Follow along using the Python interactive interpreter (allowing you to experiment and learn quickly).
    2. As you begin to feel comfortable with a particular feature, write a script/module version as shown in the "Python Scripts/Modules/Packages oh my!" section of this guide.
      1. Try importing your module into the interactive interpreter and using the functions/classes you have created.
      2. Try using the "if __name__ == "__main__":" to have the code execute as a command line script.

Once you get comfortable with this method, then writing reusable Python code becomes really easy. Then you will want to learn about Packages to organize your various related Python modules.

Making "learning python" even easier

Before I send you off to the beginning tutorials I am going to point out a few tricks and tips that will make learning Python even easier!

Interactive Interpreter Tips & Tricks

In at least Python 2.4 and greater there is a help() function. Calling help(object) displays helpful information on that object. Calling help() by itself starts an interactive help prompt.

You might ask where did this helpful documentation come from for object X? Well, Python has a concept called "doc strings". When help is called on an object it will show a lot of useful information including the doc string. You can add doc strings to modules, classes, and functions. Here is an example of adding a docstring to helloworld3 module and the function inside of it, which will now be called helloworld4.py:

"""
This is the module level doc string for helloworld4.
"""

def myPrintFunction():
  """
  This is the doc string of myPrintFunction
  """
  print "Hello world3!"

if __name__ == "__main__":
  print "Calling from command line."
  myPrintFunction()

Now we import the module helloworld4.py into the interpreter and ask for help on our myPrintFuction:

$ python
Python 2.5.1c1 (release25-maint, Apr 12 2007, 21:00:25)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld4
>>> help(helloworld4)
Help on module helloworld4:

NAME
    helloworld4 - This is the module level doc string for helloworld4.

FILE
    /home/king/proj/learning_python/helloworld4.py

FUNCTIONS
    myPrintFunction()
       This is the doc string of myPrintFuction
>>> help(helloworld4.myPrintFunction)
Help on function myPrintFunction in module helloworld4:

myPrintFunction()
    This is the doc string of myPrintFuction

Imagine if I had actually documented what the module and function do? Doc strings allow you to easily learn a new module (assuming the programmer used doc strings). In general you should document your functions explaining what they are for and what they return. The more complex the function the more documentation you will need.

For older versions of Python to get a help function, type from pydoc import help.

IPython

I have to say my life got easier when I found IPython. It's basically an enhanced Python shell with lots of cool features to make life easier. For learning Python, the most useful features are tab-completion of objects and a simplified help system.

For an example of the tab-completion, let us import helloworld4 and type helloworld4.m and press tab. The line will now read: hellworld4.myPrintFunction. Or let say you want to learn about what is in the string module which ships with Python... First import the string module (import string) and then type string. and press tab and you should see the following:

$ ipython
Python 2.5.1c1 (release25-maint, Apr 12 2007, 21:00:25)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.3 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import string

In [2]: string.
string.Template            string.center
string._TemplateMetaclass  string.count
string.__class__           string.digits
string.__delattr__         string.expandtabs
string.__dict__            string.find
string.__doc__             string.hexdigits
string.__file__            string.index
string.__getattribute__    string.index_error
string.__hash__            string.join
string.__init__            string.joinfields
string.__name__            string.letters
string.__new__             string.ljust
string.__reduce__          string.lower
string.__reduce_ex__       string.lowercase
string.__repr__            string.lstrip
string.__setattr__         string.maketrans
string.__str__             string.octdigits
string._float              string.printable
string._idmap              string.punctuation
string._idmapL             string.replace
string._int                string.rfind
--More--

For getting help, you can use the help function as mentioned previously or you can use IPython's ? syntax by typing a ? at the end of an object:

In [4]: import helloworld4

In [5]: helloworld4?
Type:           module
Base Class:     <type 'module'>
String Form:    <module 'helloworld4' from 'helloworld4.pyc'>
Namespace:      Interactive
File:           /home/king/proj/learning_python/helloworld4.py
Docstring:
    This is the module level doc string for helloworld4.

Let say for a moment the documentation for helloworld4.myPrintFunction did not tell me exactly what I was looking for and it is now time to look at the code. Add a second ? to view the source code from within IPython:

In [7]: helloworld4??
Type:           module
Base Class:     <type 'module'>
String Form:    <module 'helloworld4' from 'helloworld4.pyc'>
Namespace:      Interactive
File:           /home/king/proj/learning_python/helloworld4.py
Source:
"""
This is the module level doc string for helloworld4.
"""

def myPrintFunction():
  """
  This is the doc string of myPrintFuction
  """
  print "Hello world3!"
if __name__ == "__main__":
  print "Calling from command line."
  myPrintFunction()

IPython has many many many more useful features, but you can read about that in the IPython Documentation. The edit command is rather useful. Editor tips and tricks

Some of the Python code editors not only have syntax highlighting, but they also have tab-completion and help tooltips that pop up when writing the code. By default Python ships with IDLE which is a combination interpreter and Python code editor. It now has tab completion and displays tooltips as well. I personally do not like the look and feel, but it is much better than a plain text editor.

My current editor of choice is Komodo Edit, which is a free (non-open source), slightly watered down version of Komodo IDE by ActiveState. See Komodo Edit vs IDE for the differences. The main reason for my choice is that it has the tab completion, help tooltips as well as it is cross-platform (since I do cross-platform development, this is a must). And most importantly for at least a new Python user, it mostly works out of the box without having to go through a long configuration process. I highly recommend this editor as your first editor for Python.

PyDev (Eclipse) is more powerful and the refactoring tools are amazingly useful, but it is much harder to configure and get everything working the way you want, but once it is working, you will not regret it. I DO NOT RECOMMEND PYDEV AS AN EDITOR OF PYTHON WHEN YOU ARE FIRST LEARNING PYTHON! (Unless someone who knows what they are doing sets it up for you and teaches you how to use it or you have used Eclipse for other languages.)

There are many other good editors and IDEs out there. I will list a few that you might want to check out:

Integrated Development Environments (IDEs):

  • eric (open source; GPL)
  • Komodo IDE (commercial)
  • Wingware IDE (commercial)
  • FIXME: Finish list

Python Editors:

  • notepad++ (Windows only)
  • FIXME: Finish list

Time to learn Python

Now you are ready to start learning Python. If you come back an re-read this guide after you have learned more Python it will start making more sense, but hopefully it has created a foundation to help you learn Python more efficiently. Feel free to conact me at kingb | at | caltech.edu if you have any questions, comments or suggestions. If this was helpful, please let me know. I plan on improving this guide over time.

Now to send you on your journey. The Python Beginners Guide is a good place to start. The standard Python Tutorial is also a good starting point, but can be a little technical at times (jump around a bit, you do not need to go completely in order). The Python Module Index is a must have reference once you want to start learning about the modules and packages that come with Python.

There is also a free online copy of the Dive into Python book, but when I last checked, it has not been updated since May 20th, 2004 and therefor is bit out of date. It still may be useful.

I am going to look into which Python books are out there that have been updated to the latest version of Python. When I find out which books are consider the top ones at the moment, then I will post references to them here.

Personal tools