Learning Python was a lot easier after being familiar with Javascript.

I was warned about Python’s spacing and indentation issues, which I took in stride. I also really enjoyed the lack of extra () and {}. Also, coming from javascript, I felt that the language was pretty intuitive. For loops and while loops were similar. There was even an equivalent between the console.log() in javascript and print in Python; I remember asking, several months ago, the difference between console.log and return in javascript, and I can only now belatedly appreciate just how elementary that question was.

For beginners completely new to web development, I’d still recommend learning html/css/javascript first, along with a simple introduction to github, just so they can get over the hurdles of understanding functions for the first time, why they need to use the terminal, and how file paths work, etc.

Anyway. I should probably preface that before I started Learn Python The Hard Way (LPTHW) this time, I went through about 43% of Codecademy’s Python track. It was helpful insofar as getting me used to Python syntax (its unique way of writing functions!) and some simple exercises, but LPTHW is definitely the meatier of the two tutorials. Here’s my brief review of the first 43 chapters:

Chapters 1-10

Breezed though these chapters, mainly because there are only so many differences between javascript and python re: comments, variables, and strings. I think I’d previously written about beginners possibly getting tripped up by having to use the terminal. The other thing that must’ve tripped up true programming beginners would probably be typos, but I didn’t have as much a problem with that. The thing I did have to re-familiarize myself with was the concept of substitution:

my_eyes = 'brown'
my_hair = 'black'
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)

Chapters 11-20

Some notes for review:

  • Put a comma at the end of each print line so that print doesn’t end with a newline character and go to the next line (Chapter 11)
  • variable = raw_input(“> “)  (Chapter 11)
  • txt = open(filename) creates a variable named ‘txt’ and opens up the file. print txt.read() opens up the file and spews out the contents of the file. Commenting each line of code helped me understand it a lot better. (Chapter 15)
  • from sys import argv, typically seen at the top of a file, means that sys is a package, and the argv feature is retrieved from that package. (Chapter 15)
  • Googled why I had to do output.close(): https://mail.python.org/pipermail/tutor/2012-January/088031.html (Chapter 17)

Chapters 21-3

  • int(raw_input()) vs. float(raw_input())
  • control-d exits you out of the python shell
  • If you have a file named example.py, you can type python to get into the python shell, type from example import * to import all the features from the example.py package, type help(example) to see the contents of that file, and run commands against that file.
  • Make sure to change a piece of string into a list using .split(‘ ‘) first if you want to use something like .pop(0) to break off the first word in the list.
  • def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words
  • Exercise 26: more practice with functions.
  • Exercise 27:
    TRUE or FALSE -> if has true in it, true.
    TRUE AND FALSE -> if has false in it, false.
  • Exercise 30: Story with a bear.

Chapters 31-43

  • Start with an empty list
    elements = []
    , then use the range function to count from 0 to 5</p>
    for i in range(0,6):
         print "Adding %d to the list." % i
         #append is a function that lists understand
  • There a bunch of methods you can use on lists, like list.append(), list.extend(), list.pop(), list.sort(), list.reverse(), etc.: https://docs.python.org/2/tutorial/datastructures.html
  • My solution for printing out a 2D list like [[1,2,3][4,5,6]]:
    #starting with an empty list
    elements = []
    #then use the range function to do 0 to 5 counts
    for i in range(0,2):
         new = []
         #append is a function that lists understand
         for j in range(1,4):
             if i == 1:
    # now we can print them out too
    print elements
  • Use while loops sparingly in python. Usually a for loop is better.
  • Review your while statements and make sure that the thing you are testing will become False at some point.
  • When in doubt, print out your test variable at the top and bottom of the while-loop to see what it’s doing.
  • Think of using print like using your console.log() in javascript.
  • Every if statement must also have an else.
  • Using try/except:
    def start():
        print "You are in a dark room."
        print "There is a door to your right and left."
        print "Which one do you take?"
        next = raw_input("> ")
        #print (how_much)
        if next == "left":
        elif next == "right":
                how_much = int(next)
                print "yay number!"
            except ValueError:
                print "Not a number or left or right. You stumble around the room until you starve."
  • Q: What’s the relationship between dir(something) and the “class” of something?
    A: dir(something) gives you all the attributes of the object. The class is like the blueprint for the house.
  • You can only use numbers to retrieve items from a list. You can use any type to retrieve items from a dictionary. For example: list[1] versus dictionary[‘word’].
  • for a, b in states.items():
        print "%s is abbreviated %s" % (a, b)
  • In Exercise 40 we finally start talking about classes!

    “Here’s why classes are used instead of modules: You can take the above class and use it to craft many of them, millions at a time if you want, and they won’t interfere with each other. With modules, when you import there is only one for the entire program unless you do some monster hacks.”

  • When you instantiate a class, you get an object. Example:
    thing = MyStuff()
    print thing.tangerine
  • Exercise 41 is pretty fun: you run a script that helps you memorize how to read each of the components in a module. For example, in English, class apple(object): def sky(self, horse) translates to “class apple has-a function named sky that takes self and horse parameters.”

One day I’m going to look back on these notes and think: “Did I really have to take notes on that?” But in the meantime…

Update: This already happened for some bullet points, hah!

blog comments powered by Disqus
  • Hi, I'm Linda! I enjoy going to tech meetups, learning web development, and blogging about the learning path. Follow me @LPnotes

Subscribe to The Coding Diaries