| Author: | Stéfan van der Walt |
|---|---|
| Date: | 14/02/2007 |
From the Python website:
Python is a powerful, yet easy to learn 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 both scripting and rapid application development in many areas and on most platforms.
To recap without the geekspeak:
but realise that there is no magic bullet.
it's fun!
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
- Reasons not to are philosophical and practical
- Let's stick to practical for now
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. Also, realize that The Mathworks is like the crack dealer on the street: the first hit is free; once you are addicted it becomes quite expensive.
An academic license or a student version sells for under $100. If you are a business and need the important toolkits, you are looking at 50K per year. If you are an entrepreneurial student and dream of starting your own business once you graduate, ask yourself what you could do with the extra cash saved from a single site license. If your fledgling business grows, ask yourself what you can do with the cash saved from 50 site licenses (hint, that is 2.5 million dollars a year).
To quote Greg Wilson's Scipy06 slides:
- Best time to teach scientists something new is in grad school
- They're in for the long haul, so investment makes sense
- They welcome excuses to procrastinate
- And everyone welcomes a Plan B
Software Carpentry course -- learn to develop, rather than to grind out code
- Version control
- Unit testing
- and more...
Let's print all the elements of a sequence...
C++
vs. Python
Now someone tells you to change the length of the list, the element types, the numbers of elements etc.
Python's for loop can use any iterable -- including generators:
Numbers (including complex) and strings (print, concatenate, index)
Be careful of integer division. '1/2' gives 0! You overcome this by using floating point numbers, i.e. '1/2.0' or shorthand '1/2.'.
Lists (index, concatenate, remove, length) and dictionaries (lookup)
Tuple unpacking
Brace yourself -- no braces!
Control flow
Find structure of classes while refactoring code.
List comprehension
x = [str(n) for n in range(10)]
Import statements (not a flat namespace) (e.g. pickle and math)
import pickle
x = [1,2,3]
f = file('/tmp/blah','w')
pickle.dump(x,f)
f.close()
f = file('/tmp/blah')
x = pickle.load(f)
f.close()
Exceptions (try, catch)
try:
f = file('/some/nonexistent/file')
except IOError:
print "Could not open file"
try:
something_silly
except:
print "We caught some or other exception"
Docstrings and using the documentation.
def foo(bar):
"""Snobs the snafu."""
print "Bar: ", bar
def foo(bar):
"""Snobs the snafu.
After the first line summary, the detailed working of
a function can be described here.
"""
print "Bar: ", bar
Install Python.
Python should already be installed on most Linux systems, but under Windows you need to download the installer. It is available on the university archive.
Play around with the tutorial.
The Python homepage (here you will find the tutorial).
An intensive course on basic software development practices for scientists.
An interactive Python shell.
Stani's Python Editor