My advice on learning Python efficiently

Up to now I assumed you already knew Python or had some way of learning it. However, if you'd like my advice on a way I've found to learn the language that doesn't take you months to get up to speed with the essentials then this is the post for you.

If you know at least one programming language then click here to skip the disclaimer.

Disclaimer

If you're completely new to programming and you're thinking of starting off with Python as your very first language then congrats to you.

But, may I suggest an alternative option?

It's called Pyret.

Pyret is a new programming language that's being designed and built by Ben Lerner, Joe Gibbs Politz and others in the Pyret crew. Their focus is on building an awesome teaching language. Learn more here.

These guys have a long and distinguished track record of creating curriculum and software for teaching introductory programming at a high-school and collegiate level.

Check out the Bootstrap program and Shriram Krishnamurthi's new book called Programming and Programming Languages.

I hope you find those resources useful.

Reach out to me in the comments or via email if you need any more help.

Learning mistakes to avoid

There's one skill that's far more important than any other in your programming career.

And that's, the ability to learn efficiently.

If you master that skill then the world is at your feet.

According to John Sonmez there are at least 5 mistakes we make that hamper our ability to learn efficiently. And I agree with him on all points. They are:

  1. Reading a book cover to cover. This is bad because we only retain a tiny fraction of what we read and most books are too broad in scope to be worth reading cover to cover. When you're just starting out it's a huge waste of time and energy to learn concepts for which you have no immediate use. There's a time for that but it's not when you're just getting started. Instead, you should focus your attention on what you need to learn right now and ignore the rest.

  2. Diving in without a plan. I'm a repeat offender here. Sometimes I get so excited by a new technology that I dive right in, start playing with it, buy a couple books, read, read, read, play, play, play, watch some videos on it, subscribe to blogs and newsletters, read some more, play some more and by the end of it all I'm going around in circles and not making much progress. It's sad. I have fun. But I've never become an expert by doing that.

  3. Failing to narrow your scope. Having clear boundaries makes it easy to decide whether or not a new resource is worth your time. That's why learning Python by trying to build something in it is a great way to go. You'd realize how much of Python you don't need to know in order to accomplish any one task. You'll find that the more you narrow your scope at the beginning, the more you'll learn and the faster you'll progress.

  4. Trying to learn 2 (or more) things at the same time. If you got into Python because you're interested in doing Django web application development then it's highly likely you're doing this. You're learning a bit of Python here, a bit of Django there, a little more Python and then a little more Django. Essentially you're "spinning top in mud" as we Trinidadians like to say, i.e. plenty action and motion but not really getting anywhere.

  5. Spending too much time studying before you have experience doing. Because we're afraid to fail, we want to know what we're doing before we ever try. So we spend a lot of time learning before ever trying to apply any of it. I know that's been me in the past. The irony here is that the more I began to know is the more I realized I didn't know and so the more I tried to learn. This caused me to be even more afraid of trying to do anything because I "know" I'd be doing "shit". And who wants to be writing shit code. We all want to be writing beautiful idiomatic Python code. Don't be like me, start before you feel ready.

If you liked reading the above and want to learn more then I'd feel very bad if I didn't direct you to the source of this wisdom. Check out "5 Learning Mistakes Software Developers Make" by John Sonmez. I probably botched his points in my haste to summarize but do check out his stuff for much better anecdotes and deeper insights.

My advice

Step 1: Familiarize yourself with Python.

One of the great things about the Python community is its relentless focus on having good documentation. It's FREE and outstanding. Don't hesitate to take advantage of it.

So you're first steps to familiarization should be to install Python and set aside a couple hours to run through the Python tutorial.

Should I learn Python 2 or Python 3? There's been heated discussion about this recently but the short answer is "Learn Python 3".

Then, read "The Zen of Python". It helps with the mindset you should have when writing Python code.

Congratulations! You now know enough Python to write your first application.

Step 2: Write your first application.

Think of an application and write it in Python.

Since Python isn't your first language then I'm sure you've written something in another language that can be ported to Python. If for whatever reason you really want to write an application that's completely new to you then go right ahead. But I'd say try to work on something that you can get up and running in a couple of days (it took me 4 days to build my first application) or in a week for the most. At this point you're just trying to get a feel for the language.

For my first application I built a Whitespace interpreter. I originally built it in Racket, then Ruby, then Haskell and now Python.

As you can probably tell, it's my goto application to build after getting the gist of a language. It works for me because I end up spending my time trying to think in the language rather than trying to think about the particular application.

I can't stress this enough. This is the time to focus on learning to think in the language. So keep the application idea simple for yourself so that using the language becomes the main focus.

Obviously, I didn't know how to do everything I needed to do to get the project implemented in Python. But that's the point of this exercise. Let the application guide you in what to learn next. And when you're learning only learn what you need to know to get the feature implemented. You can always go back and learn more later on.

For instance, when I began implementing the virtual machine I knew I wanted a comprehensive test suite but the tutorial taught me nothing about that. However, a quick search brought me to the excellent documentation on Python's unit testing framework. I read what I needed to know to write and run my tests and moved on. Any questions I had I just returned to the docs.

Note: After building a couple more projects and using unit tests in all of them I decided it was time to learn the framework well. So I sat down and read all the docs from beginning to end. But I only invested that time later on because I knew it was something I'd be using in my projects time and time again. Be wise and do the same.

I tend to develop incrementally so if you look through the commits for the project you'd see how I setup the project, decomposed the problem into pieces and ultimately completed the first version of it. Remember, be a finisher.

Step 3: Learn a little more Python.

Hooray! You've completed your first Python application.

Now I suggest you invest a little more time learning what's available in the language. Python comes batteries included, meaning it has a vast and awesome standard library. You probably skimmed the documentation already when you were trying to get things implemented in your application. Great. I know that's what I did. Dive in a little more. Here are the parts worth your time to read: 1, 2, 3, 4, 5, 6.1, 6.2, 26.4 and 27.3.

Continue to skim the rest.

You don't need encyclopedic knowledge of Python just a rough idea of what's available.

When you do need something off the beaten path (or in the future when you've planned out some time for exploration) then it's wise to invest more time in the other areas.

Put your new found knowledge into practice by looking for opportunities to refactor your application or add more features.

Step 4: Build, build and build some more

Believe it or not but you now know enough Python to build almost anything you want.

Remember that it's not the languages that matter but what you do with them (unless you're into programming language theory, compiler design and that sort of thing).

If you have specific domain knowledge, in mathematics, physics, finance, biology for e.g., then leverage Python as your secret weapon to do awesome things and advance those areas.

If you run out of ideas for things to build then Talk Python Training has a course just for you called Python Jumpstart by Building 10 Apps.

Step 5: Broaden your horizons

Do you recall learning mistake number 5? If not, read it again I can wait.

Cool.

Well, if you've been following these steps then you've certainly put doing before studying. That's great.

But, there are surely holes in your knowledge.

Why?

You don't know what you don't know.

It's highly unlikely you'd ever try to use an idea, concept or tool you never knew existed.

This is the time to actively seek out more advanced knowledge. You have a couple real applications under your belt and you probably have tons of questions. That's awesome.

Quench your thirst for knowledge by starting off with these two great resources:

  1. Write Pythonic Code Like a Seasoned Developer
  2. Powerful Python

And, finally dip in and out of these two books as the need arises:

  1. Fluent Python
  2. Effective Python

Bonus

More tips and advice I didn't find a way to fit neatly into the prose.

  1. I can't stress enough the quality of the Python documentation. I already talked about The Python Tutorial and The Python Standard Library documentation but I hardly ever hear anyone talk about The Python Language Reference documentation. I sorely miss something like it when I'm writing Ruby and I forget a piece of syntax or the semantics of a construct. Having an authoritative resource on that information helps you compose programs with confidence. For e.g. if you forget the syntax for the if statement then rather than asking a question on Stack Overflow, look it up in the documentation where it's explained under "The if statement".

  2. Read PEP8. Raymond Hettinger did a great presentation (as usual) called Beyond PEP8 that's worth checking out once you've written a substantial amount of Python.

  3. Get in the habit of writing good documentation from early on. Docstring Conventions is a good resource to learn about the conventions you should follow.

  4. Read other people's code. I'd suggest starting off with Kenneth Reitz's code. For e.g. you can check out tablib. To find other sources you can look to the libraries you're already using and other popular libraries you hear about. Reading code is good to do because it let's you see how other people are using Python in the wild.

  5. David Beazley does awesome stuff. Check it out when you're ready to blow your mind.

Conclusion

Becoming an expert Python programmer can take years but laying the right foundation can be done in a couple of weeks. If you follow my advice and practice deliberately you'd be setting yourself up for success.

I wish you all the best on your journey.

P.S. Let me know in the comments below if any of this (unsolicited) advice was helpful to you. :)