Tuesday, September 18, 2012

Intro to Python

Python is an awesome cross platform opensource language that allows for quick development of anything from simple scripts to fully functional programs. There are numerous libraries that can perform just about any task you would ever need to do, from accessing your gmail account to high performance computing on the worlds largest super computers.

You can download the official python source and installers here. At the present, I would install the latest 2.x series which is 2.7.3. There is a 3.x series, with the latest development being 3.2.3, however not every library officially supports the new 3.x series, which has made some significant changes. As this is a blog focused on the use of python for photographers, the library PIL (Python Imaging Library) will be constantly used. Unfortunately, PIL does not officially support the python 3.x series.

Several import libraries to get working on your machine:
  1. PIL
  2. Numpy
Note: If you are using a windows machine I would highly suggest using  Christoph Gohlke's windows installers for the libraries that you are interested in.


Assuming that you have python installed, try opening the IDLE (python shell). You should see something like:
This is 2.7.2 on windows 7.

If so, your good to start writing python code. Note: you essentially will be creating text files with a *.py extension so any text editor will work for writing python code. There are also other IDEs you can use. A list is maintained here.

You can also access python through the command line by typing python (if your path variables are setup correctly):

So, if you have python up and running try out the following code:

>>> print 'Hello, World'
Hello, World
>>> x=200
>>> print x
200
>>> x="foo bar"
>>> print x
foo bar
>>> x=10
>>> y=100
>>> z=x*y
>>> print z
1000
>>> x='Print Me'
>>> for i in x:
            print i

P
r
i
n
t

M
e
>>> x=range(10)
>>> print x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in range(5):
            print i
0
1
2
3
4
>>> a=list()
>>> for i in 'Dude':
            a.append(i)
>>> print a
['D', 'u', 'd', 'e']

Play with the above code examples, exploring what the commands are doing. If you can master these commands, then we can start writing useful code!

No comments:

Post a Comment