in Coding

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. []

Write a Comment

Comment

14 Comments

    • Thanks, Jason! I would definitely recommend starting with the interactive textbook How to Think Like A Computer Scientist. It’s all online, doesn’t require fiddling with Python on your machine, and you can save your progress in the book and work on it anywhere, whenever you have a spare minute.

      I wish I’d started earlier…it takes effort and thought, of course, but as you more and more into it, you get better and better at solving problems.

  1. Thanks so much for writing this. I’m up to ex. 45 in LPTHW and had earlier abandoned Think Python. I’ve looked at the Codecademy course and not done much there. Mostly I’ve been sticking with Zed, but now and then I search around and look at other things for a little break. Anyway, you’ve encouraged me to pick up Think Python again. Thanks!

    • Thanks for reading, Mindy. Do check out How to Think Like A Computer Scientist. It’s a slightly simplified version of Think Python, but it’s pretty good.

      I’m enrolled in the CodeFellows Python Bootcamp now, and in the first week we’re using How To Think… as a review of Python. It does a good job of it.

      Good luck, and keep going!

  2. Great post, Luke.

    I found this to be very useful, also. http://codingbat.com/python

    It’s a small site with problems ranging from basic to advanced. The site was started by Stanford Lecturer / Google Best Practices engineer, Nick Parlante.

    This site helped me create a self-study curriculum. Thanks.

  3. Thanks for post, your post gave me some path how to continue my learning process. After finishing codeacademy tutorial I was feeling a little bit lost, because I am still not able to do any project by myself in real world, but anyway after I have put some effort to learn basics I don’t want to quit now. I have found your experience very usefull and I am going to try with “How To Think Like a Computer Scientist”

    • Thanks, Marin!

      Check out newcoder.io, it made me feel like I was doing some ‘real coding’ while getting a better idea of some of the cool things that Python can do!

  4. Hi Luke,
    This is a great article. It really lays out your thinking comprehensively. I’ve been wondering where to start and I’ve just signed up to How To Think Like a Computer Scientist at your suggestion. I’m thinking I’ll move on to Learn Python The Hard Way when I’m finished and see how that goes.
    Thanks. You’ve been a signpost for me. I mean that as the highest compliment.

    • Thanks very much for the kind words, Rob. Another book I’d recommend is the eBook Python 101. It goes over the basics in more depth; I think it does a great job of giving someone who’s got a little bit of coding (or a little bit of Python) under their belt a deeper understanding of how powerful Python is.

      Let me know how you like How To Think Like A Computer Scientist, and check out newcoder.io once you’re through that…newcoder is pretty great, discusses a bunch of things we learned at Code Fellows.

  5. Mark, thanks for sharing this great review. I was a bit lost after learning some Python, because I simply didn’t know how to apply this knowledge to a software concept. Hopefully, your advice on “How to think like a Computer Scientist” will further keep me interest on learning Python to grow my career opportunity.

    By the way, I’m very excited that you have attended the CodeFellows. Will you be sharing your experience or tips that you learned from the classes? I would be interested to know how tough it can be for a student with no formal CS training.

    • Hi, Young,. CF are already refactoring their program, so check out their website to see what their current route to a bootcamp is. I certainly felt underprepared for the class, but learned quite a bit. If I were to do it over, I would have made sure to (a) have been comfortable with testing (using unittest), (b) gone through a book like Head First SQL to better understand models and ORM, and (c) read a little bit about data structures.
      Regardless, I’m using what I learned at CF as a foundation to develop more knowledge about all of the above.

  6. Hello Luke , It was really enlightening for me too to read what you have written in such a beautiful manner about how one can learn Python. i am actually very focus oriented and i was searching for things on web that can ‘actually help’ me to complete a project in python. I have done programming before but on your suggestion i am definitely going to follow the sequence you said from Codeacademy>>LPTHW>>Think python >>newcodr.io. Thanks for writing such great and helpful post .