So you've decided to learn Python. Maybe you've already taken a look at the Python site. But where to begin? Are there good books for learning? Programming exercises? What are essential tools one must know about? I've gotten these questions from several interested friends and colleagues. Each time, I tried to reply with a helpful mail. At some point I realized that a nice write-up as blog post would serve just as well and allow other people to benefit, too.

So, I'd like to help you getting started with Python quickly. But let's be clear: This post is meant for people who have some experience with programming and want to get pointers for learning Python. If you are new to programming, be warned! In that case, this might confuse more than it helps. That being said, let's go.

Which Python Version?

Perhaps you've noticed already: There are two slightly incompatible Python versions out there:

  • 2.7: The 2.x line has been in active use for years. There are tons of open source libraries and you can do anything short of flying (wait, you actually can).
  • 3.2: This is the new branch, where all future development will take place. It's still Python goodness all the way down. As a bonus, it improves a few things and removes some old warts. Unfortunately, that makes it backwards-incompatible with 2.7. So, not all of those libraries for 2.7 work with 3.2 yet.

Which one to start with, then? Go with Python 2.7, if:

  • you want to use certain libraries right now and they don't work under 3.x yet.
  • you don't know which libraries you need, but it might be lots of different ones.
  • you want to work with a web framework. The two largest frameworks (Django and Pyramid/Pylons) don't operate under 3.x as of now.

Go with Python 3.2, if:

  • you want to use the language for one specific task and the library you need is already ported to Python 3.
  • you just want to learn the language itself and there's no immediate requirement for 3rd party libraries.

When in doubt, pick whichever one - it won't be difficult to switch later. Already got Python on your system, but don't know which one? Just start python on the command-line and check the version number it says.

Where's the documentation?

If you're like me, you want to know about the official docs, because... well, because they're official and probably most up-to-date. A first look at the documentation might be overwhelming ("Oh my god, where do I start??"), so here are the most important parts:

  1. The Python Tutorial (2.7 or 3.2). This is a very nice introduction. I would recommend the first 5 chapters up to "5. Data Structures".

  2. The Library Reference (2.7 and 3.2). All the libraries included by default are described here. As the name implies, this is more of a reference and less of a learning resource. Still, let me point out three important sections that you will definitely need later:

    • Built-in Functions: These are always available in your code and highly useful.
    • Built-in Types: Shows the data types in the language (booleans, integers, lists, ...) and the methods they offer.
    • How do you capitalize a string? How to split a string into words? Want to know, which other string methods are available? Unfortunately, the string documentation is buried deeply within the library reference: Click on "5.6 Sequence Types" and then "String Methods". Oh, the last link is hidden within the first big paragraph of text on sequence types. I wish I knew why this one is so hard to find.

    Ignore the Library Reference for now, but keep it in mind for later.

Books Online

Of course, there are also many printed books. But to let you jump right in from your computer, here's a selection of ebooks:

  • Learn Python The Hard Way by Zed Shaw. A series of 52 exercises that let/make you do things yourself instead of just reading them. Very practically oriented (I've heard).
  • Think Python - How to Think Like a Computer Scientist: Often mentioned as a good beginner's book that also teaches a bit about programming in general.
  • A Byte of Python: Very beginner-oriented and easy to start with. I remember this one helped me a lot in my early Python days, but back then I didn't know about the other books, so I cannot compare them.

Python Exercises

At some point, only reading tutorials and books gets boring and you might want to start writing programs. Besides the exercises in the books above, there are some fun online challenges:

  • Project Euler: A number of mathematical and algorithmical programming problems, where you can track your progress nicely with a personal user account. I like it, but its focus on math does more to teach you algorithms in general than Python in particular.
  • Python Challenge: A "programming riddle" in the style of Notpron aimed to teach Python in the process. Why I like this very much: It poses challenges in several different areas (how to do HTTP-requests in Python, image analysis, ...), so you get a broad look at the language and what it can do.

Python Style Guide

Should I name my variables in CamelCase or lowercase_with_underscore? And what is it about Python's indentation: Do I use 2 spaces? 4 Spaces? Tabs? What other conventions should I follow in my code? Some people ignore these questions at first and start coding right away. Coding guidelines distract them from learning the language itself.

That is perfectly fine, but it is hard for me to do. When learning a new language, I need to know about the "right way". First, it means I don't have to rewrite my code later and I have consistency right from the start. Second, the existence and the contents of a style guide reveal something about the language's culture. If the guide tells you: "Do whatever the hell you want!" or there is none at all, that says something about the language (probably that it's C++. Wait, sorry - C++ is the one where there are several, contradicting best practices, which makes me feel guilty before having written a single line of project code). Third, following the guide means that I've got one less item to think about during programming. It gets easier to focus on the actual coding, because I don't need to worry about pesky style issues. Oh, and last: When everybody writes code along the same rules, reading other people's code gets so much easier. So, if you're one of those persons that likes to know about coding guidelines: Python has exactly one, official style guide and most Python programmers tend to adhere to it.

Libraries/Package Management

Sooner or later, you might want to use some of those thousands of Python libraries. Fortunately, Python has a package management system that makes things easy for you. Unfortunately, Python has more than one package management system, that makes things easy for you. There's:

  • EasyInstall: It's part of the setuptools library, and lots of Python packages tell you to simply write:

    easy_install SomePackage
    

    But this library is old and only works on Python 2.x. Do not use it anymore.

  • Distribute, a fork of the setuptools library which gives you EasyInstall both on Python 2 and 3. It also fixes lots of old bugs.

  • pip: The new kid on the block. Also, the better one. With pip, the syntax is:

pip install SomePackage

Which one, you ask? To keep it short: Use pip! If you are not convinced, read this. Ironically, you need setuptools/distribute to install pip. So, here's what you do:

  1. Download distribute_setup.py from here and execute it like this:
python distribute_setup.py
  1. Download the get-pip.py install script (make sure to save it as 'get-pip.py') and run
python get-pip.py
  1. You're good to go. Use pip for everything else from now on:
pip install WhateverPackage

The Ultimate Playground

Python has an interactive interpreter, that allows you to directly type and execute code. No compiling necessary, no text editor to write and save your Python file (if you don't want to). It's great for quickly trying out new things. In order to start the interpreter, simply run python from the shell of your choice.

Now, wouldn't it be really nice if that interpreter also had automatic tab-completion? Enter IPython, an enhanced Python shell that gives you goodies like auto-completion, a nice help system and much more. In order to get it, you need IPython itself and a working readline module for the tab-completion. For Python 2.7, that means:

pip install ipython

Then, you need readline. On Linux, you should have it already. On OS X or Windows, probably not (according to this). On Max OS X you therefore do:

pip install readline

On Windows, you grab a binary pyreadline installer right here and run it.

An experimental IPython branch for Python 3 is now available. In that case, it's best if you read the IPython Python 3 notes yourself to check out the current state of things.

Once you have IPython installed, whenever you want to use the interactive interpreter you simply use ipython instead of python.

Summary / tl;dr

  1. Install Python or check which version you already have installed.
  2. Install distribute and pip for Python (details here)
  3. If you're on Python 2.7, grab IPython (see The Ultimate Playground above)
  4. Whooo, you can actually start. Do one or more of the following:

If anything in this post was particularly helpful (or not helpful at all), just send a mail. I'd be happy to improve parts or point out further things.