Friday, January 18, 2013

Great Python workshop at AAS

Demitri Muno held a great workshop at the 2013 Long Beach AAS, "Astronomers Learning Python" or somesuch.  Here are my notes on the best stuff I learned.

  • To make your code not care where python is, start as "env python".
  • None is a special null value.  Better than declaring value as -999.
  • The string join operator is +.
  • Triple quotes are pretty powerful: d = ''' this is a single string with everything included including linefeeds.'''
  • Dictionaries behave like hashes in perl.  Lots of useful ways to sort, work with them.
  • Since whitespace controls loops in python, you must pick tabs or spaces, and be consistent.  They recommend tabs.
  • Both ! and "not" are the not operator, but "not" is more readable, so preferred.
  • Here's how to efficiently loop, and keep track of the index in the loop:
a = ['a', 'b', 'c']
for index, item in enumerate(a): 
     print index, item

  • String formatting (this is preferred over deprecated %d style):
print "xxx {0}, {1}".format(math.pi, "b") 

  • It is harder to code in python using a standard text editor (emacs or vi).  Better to use a dedicated coding editor.  It will help you!  He recommends TextWranger. 
More later!


2 comments:

  1. I disagree with two items:
    1. spaces > tabs. In my editor, I map the tab key to be 4 spaces. Tabs render differently in different editors/environments, which can get ugly.
    2. It is not hard to code in python with a standard editor. I find writing python *not* in vi to be incredibly frustrating. Both vi and emacs have syntax highlighting and means of incorporating autocompletion. They are also universally available and at least vi is pretty light weight.

    ReplyDelete
  2. I read your blog and found the effective and highly productive about Python Workshop ....thanks

    ReplyDelete