Some thoughts on learning Python and how to program

I have been learning Python over the past several months1, using three different resources: Zed Shaw’s Learn Python The Hard WayCodecademy’s Python course, and Allen Downey’s book Think Python.

Below I’d like to share a few thoughts about the strengths and weaknesses of each and a bit of reflection on my own learning. If you’re thinking about learning programming by learning Python, I hope this may be useful.

My experience with lpthw started with the now defunct udemy course set up by author Zed Shaw. Zed asks you to begin by going through his command line crash course. The first time I tried, I quit half way through and tried out Codecademy’s Python course. This isn’t because the command line course is bad; it’s just that I wanted to do (what felt like) actual programming, not learn more about how to zip around in the terminal. In retrospect, this reflects my own experience as a classroom teacher; the best way to get students to do something that they may not want to do is by wrapping it in something more enjoyable. Hopefully Zed’s figured out how to do that in the book version (3e) of lpthw.

Codecademy’s Python Course

Learning Python at Codecademy

Learning Python at Codecademy

My first attempt at Python was through Codecademy (here’s my profile). Codecademy was very appealing to me at first because:

  1. Units are broken up into small chunks with activities that lead up to a larger project. (Writing the pig latin translator program was an awesome feeling.)
  2. Instant feedback on whether or not your code works.
  3. The potential of getting help in the form of the community forums.

However, I soon ran into some difficulties. Sometimes the code running Codecademy seemed a bit too strict; I would write code that I thought should work, but would get an error. After a few times of having this happen I opened up TextMate, pasted the code, ran it in Terminal, and lo and behold it worked. The Codecademy IDE seems to be looking for very specific uses of different functions, meaning that it can overlook correct answers. Similarly, you can accidentally “outsmart” the IDE. e.g. I could submit code that returned a syntax error but still passed me to the next level. There is good discussion of this on the site in the discussion forums, but it seems like these complaints are largely ignored.2

As I became more comfortable with writing Python in Textmate, the second perceived benefit above was rendered moot by how easy it is run code written in Textmate in a terminal window. I did this throughout LPTHW, and working in Codecademy’s online development environment felt a bit clunky afterward. Part of this clunkiness was caused by my being in China; Amazon web services are inconsistently blocked here (there doesn’t seem to be rhyme or reason to this blocking, it’s done just to the point that the service’s unreliability is irritating and you don’t want to use it).

With that being said, after I worked through Learn Python the Hard Way and went back to Codecademy, I could appreciate that by being rather narrow about the code that is used to complete assignments, the site makes sure you are showing mastery of (or, at least, the ability to use) the concepts being taught.

Learn Python The Hard Way

lpthw

lpthw – the website, not the book

After getting frustrated with Codecademy, I went back to Learn Python the Hard Way (LPTHW). Now that I had some Python under my belt, I was happy to go through the rest of the command line course and then got to the meat of things, where I quickly came to appreciate both Shaw’s style of teaching and the thrill of getting my computer to do novel things that I want it to do.

With about 25% of the book left to go, my wife and I went on a two week trip to southwest China. During the trip I found myself thinking about how to solve some of the programming assignments that I hadn’t wrapped up before leaving. That’s when I knew that programming was the right thing for me to be learning.

Once I completed the course, I still did not feel confident enough to start away on any of the projects I have dreamed up (in retrospect, this is because I was thinking about really big problems instead of smaller, more manageable programs). Each time I would start, I’d find myself running into brick walls that I just wasn’t sure how to solve. I sought out help online but didn’t find a good Python community for new programmer. I went back to Codecademy for a short while to practice some of the concepts from LPTHW, but, after again being frustrated with the IDE, I picked up another “beginner” text: Think Python.

Think Python

Allen Downey's book Think Python is available from O'Reilly or for free from thinkpython.com

Allen Downey’s book Think Python is available in paperback from O’Reilly or for free from thinkpython.com.

Think Python, by Allen Downey, is written as an introduction to computer science, and now that I had an idea of how to write basic programs in Python, I found that gaining an understanding of cs concepts was very fulfilling, and helped me to relate to the material in a different way. When I would through examples or mini-puzzles/projects, I’d often wonder why something worked; this books has helped me to have a much better understanding of why what I’m doing works or doesn’t.

I like that Downey uses a variety of examples, including graphics, in the book. Some of the early examples require you to remember concepts from middle school geometry, and it was fun to get that sort of a mental workout in addition to the workout from programming.

As I go through Think Python, I’ve constantly referred back to the work I did in lpthw. Topics like classes that were difficult to understand before now make perfect sense. I’ve also created a memrise course with the CS vocabulary from Think Python, which I hope will help me to be able to formally build upon this knowledge and eventually learn about algorithms &c, someday.

General Suggestions On Learning Python

Take notes. Zed makes a big deal of this in lpthw, and it worked wonder for me. I also learned quite a bit from following his advice and commenting every bit of each program after it was done. Taking notes is especially important, I think, for Codecademy, since the interface doesn’t make it easy to jump back to earlier units for review. After a while I kept a document open as I read and worked and typed up my notes there.

Annotations in my (legally) self-printed copy of Think Python.

Code annotations in my (legally) self-printed copy of Think Python.

As I’ve been reading Think Python, I’ve been annotating the code in the book with handwritten comments, just to make sure I (a) understand what’s going on and (b) to force myself to reference any methods I am not sure about. Since I’ve started doing this, I’ve felt much more confident in my understanding of the material and in my ability to use it.

A shortcoming of the Codecademy interface is that the IDE isn’t really expecting you to write in comments, and so the screen quickly becomes crowded. On the other hand, this is something that the interactive How To Think Like A Computer Scientist seemed to try to answer; you can highlight as you read the text…but I still am not that sure as to where my highlights went. 

Google/RTFM. This was another hugely important lesson I learned from LPTHW. Python can do all sorts of things, but it’s up to you to find out what they are and how they might fit with the problems you want to solve. The difficulty with this, I think, is that when you Google for “How can I…with Python?” you get a huge number of different suggestions on how “best” to solve the problem. It took me a while to not be distracted from my programming by all the chatter.

I’ve also picked up a copy of Mark Lutz’s Python Pocket Reference, and have the Python docs loaded on my tablet. This helps me keep what I need to know at hand.

Sometimes it isn’t your fault (but it usually is). One of the cool things about writing code, I think, is the process of going back and fixing your own errors. When programs I wrote didn’t work, it was because I had screwed up somehow. Missing a colon, incorrect indentation, or just plain unworkable code. With Codecademy, there were often times when the code I wrote didn’t work, but only because I hadn’t written the code exactly the way they wanted; when I copied and pasted it into TextMate and ran the code in the terminal, it worked fine.

You can always learn more about the basics. From each of the three approaches I’ve used, I’ve learned more and more about concepts that I thought I “knew”. At the same time, there’s a danger of staying with that which is safe at the expense of learning new things. Make sure that if/while you’re reviewing, you’re learning new things and challenging yourself to go beyond what you’ve already learned and are comfortable with.

Sometimes simpler is better. A signal difficulty for me in learning to program was, very disspiritingly, getting Python itself to run the way the each book said it should. And, you know, it ain’t just me:

 

Upgrading to Mavericks broke Python; a graphics library doesn’t play nice with the version of Python that comes with OS X; one of the libraries used in the NLTK book doesn’t work with Python 2.7.3 on OS X. And so forth. From this, I think it’s best, at least at first, to use an online interactive environment to code in. This will save you from wasting time trying to figure out how to get Python to work the way your book says it should. If I were to do it all over again, I’d probably start with How To Think Like A Computer Scientist: Interactive Edition. It is based off of Downey’s Think Python, and teaches Python 3. The creators of the website, CS educators, have created the best online, interactive textbook I’ve seen. Other disciplines could learn from them.

Online communities are very usefulI’ve found quite a bit of helpful information in reddit’s /r/learnpython and the “Newbies” section of the Google+ Python community. It makes learning much easier when you can get code critiques.

Keep Learning, Start Making

I’m about to finish Think Python, and am now very confident in my ability to problem solve and break larger problems down into smaller, more manageable ones. After finishing the book, I’ll be referring to the NLTK book to learn some of the NLTK’s tools for corpus analysis while I start working on a project I have in mind to help people write better covering letters and resumes for job applications. As that project progresses, I’ll also be working on learning Django and developing the project into a webapp. After that, I’d like to start working on (or, even better, with) a project for improving language learning through the use of a lexical approach to language teaching, which thanks to the NLTK should be rather easy to apply.

Learning to program is one of the most exciting things I’ve done, and I’m looking forward to seeing where it may take me.

  1. Why Python instead of Ruby? I wasted hours and hours reading about the two, but chose Python because of its cool libraries, especially the NLTK. What brought me to try my hand at programming was the desire to be able to do work on questions related to linguistics. Because of my initial interest in being able to use the NLTK, I’ve been learning Python 2.7 rather than Python 3.  []
  2. See, e.g. this thread. []

{ 0 comments… add one }

Leave a Comment