Dive into Python

Author: Bartosz Telenczuk

What is Python?

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

If someone uses words likes this, he usually tries to sell you something...

Why Python?

It is important to realize, that because Python is developed by the community it differs from the commercial applications. Whereas the latter put more emphasis on a ready product to sell, the open source community cares more for its core functionality (no eye-candy and fireworks).

What Python is NOT?

What can I use it for?

and many other applications.

Batteries included

A rich standard library (boundled with every Python interpreter) :

  • csv -- CSV reader
  • pickle -- object serialization
  • re -- regular expressions
  • urllib -- handling of HTTP requests
  • threading -- high-level threading interface
  • multiprocessing -- process-based threading
  • xml.* -- XML parsers,
  • sqlite3 -- interface for SQLLite databases

and explosion of user-contributed libraries in almost any application area (image processing, HDF5 in/out, 2d and 3d visualizations, etc.)!

But how fast is it?

Python can save YOU much time!

figures/human_vs_machine_time_mod.png

...but takes more time of your computer.

However, if your code runs too slow -- PROFILE AND OPTIMIZE!

Optimization is important but should not be done prematurely. There are execellent tools for optimization such as Cython.

Python Testimony

"I was a huge matlab user for almost a decade. At some point I "hit the wall" and could no longer be productive in matlab. The extra overhead of managing complex data structures, developing complex GUIs, and working with networked data and databases was consuming most of my programming energy. Yes, matlab provides you a simple, comprehensive interface, and a fairly complete set of numerical libs, but when you want to work with complex data in a realistic networked environment, you hit the limits of the language and environment pretty hard. Then you rewrite what you like about matlab in python and get on with it. matlab is a great tool for beginners and intermediates. For experts, it has limitations which are hard to overcome. My advice to students: if you aspire to be an expert, bite the bullet now and build a set of tools that can scale with you on your ascent. "

John Hunter, author of matplotlib

Start learning. NOW!

Greg Wilson:

Software Carpentry course:
  • Version control
  • Unit testing
  • and more...

Python Autumn School

Trento, Italy, October 4th-8th 2010 (Application deadline: August, 31st)

Day 0 — Software Carpentry & Advanced Python

Day 1 — Software Carpentry

Day 2 — Scientific Tools for Python

Day 3 — The Quest for Speed

Day 4 — Practical Software Development

Your First Python Program

prices = {'milk': 1.00, 'wine': 2.50, 'apples': 0.6}
total = 0

def get_price(product, quantity=1):
    """Calculate the total amount to pay"""
    price =  prices[product]*quantity
    print product, quantity, price
    return price

#Testing the code
if __name__=='__main__':
    basket = ['milk', 'wine','apples']

    total_price = 0
    print "Item", "Qty", "Price"
    for item in basket:
        price = get_price(item)
        total_price += price
    print "Total: %.2f Euros" % total_price

Discuss: indentation, datatypes (lists, strings, integers, dictionaries), introspection (string methods), list methods (append, len, etc.), iterators, defining function, returning values/tuples, keyword arguments

Extend: read price list from csv file, add non-existing element to the basket; catch the exception; make independent of lower- or upper-case; pretty printing of the bill; refactor code (define seperate procedures for reading price list, checking out and printing the bill); import the function from an external script; write simple tests for the functions

Building blocks

Python does not use braces to define code blocks: use identation!

Always use 4 space for a single indentation level!

Datatypes and their methods:

  • numbers: a=1

    Beware: 1/2 == 0, 1/2. == 0.5

  • strings: b="Hello ITB!"

  • lists: c = [a, b]

  • tuples: d = (1,2)

Building blocks

Lists can be conviniently created using lists comprehension:

series = [2**i for i in range(10)]

To access selected range of items you can use slicing

print series[2]
print series[2:5]
print series[:-3]
print series[4::2]

Building blocks

Defining functions is easy:

def foo(arg1, arg2=1):
    return arg1+arg2

Use interators for looping:

for i in range(5):
    print i

If statments are also easy:

if score==5:
   print "Joopi!"
elif score==4:
   print "ok!"
else:
   print ":("

Building blocks

Always use docstrings to provide documentation for the user:

def foo(x):
    """Takes a number and returns its square"""
    print x**2

Definitions in any Python file can be imported using import. Try this:

import this

You can list the functions within a module or read their docstrings using dir and help (in IPython you can use tab completion and ?).

import csv
print dir(csv)
print help(csv.reader)

Builiding blocks

You can catch exceptions using try... except... clause:

try:
    f = file('/some/nonexistent/file')
except IOError:
    print "Could not open file"

Coding Style

Check PEP8 for complete list of coding conventions.

What next?

Literature

  1. Greg Wilson, Software Carpentry: Getting Scientists to Write Better Code by Making Them More Productinve, Computing in Science and Engineering, Nov/Dec 2006
  2. Sebastian Bassi, A Primer on Python for Life Science Researchers, PLoS Computational Biology vol. 3 (11) pp. e199
  3. Susan M Baxter, Scientific Software Development Is Not an Oxymoron, PLoS Computational Biology vol. 2 (9)