What are the important language features (idioms) of Python to learn early on?

Python is a language that can be described as:

“rules you can fit in the palm of your hand with a huge bag of hooks”.

Nearly everything in python follows the same simple standards. Everything is accessible, changeable, and tweakable. There are very few language level elements.

Take for example, the len(data) builtin function. len(data) works by simply checking for a data.__len__() method, and then calls it and returns the value. That way, len() can work on any object that implements a __len__() method.

Start by learning about the types and basic syntax:

  1. Dynamic Strongly Typed Languages
  2. bool, int, float, string, list, tuple, dict, set
  3. statements, indenting, “everything is an object”
  4. basic function definitions

Then move on to learning about how python works:

  1. imports and modules (really simple)
  2. the python path (sys.path)
  3. the dir() function
  4. __builtins__

Once you have an understanding of how to fit pieces together, go back and cover some of the more advanced language features:

  1. iterators
  2. overrides like __len__ (there are tons of these)
  3. list comprehensions and generators
  4. classes and objects (again, really simple once you know a couple rules)
  5. python inheritance rules

And once you have a comfort level with these items (with a focus on what makes them pythonic), look at more specific items:

  1. Threading in python (note the Global Interpreter Lock)
  2. context managers
  3. database access
  4. file IO
  5. sockets
  6. etc…

And never forget The Zen of Python (by Tim Peters)

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!

Updated February, 2009

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s