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:
my_eyes = 'brown' my_hair = 'black' print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
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)
- 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.
TRUE or FALSE -> if has true in it, true. TRUE AND FALSE -> if has false in it, false.
- Exercise 30: Story with a bear.
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 elements.append(i)
- 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: new.append(j+3) else: new.append(j) elements.append(new) # 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.
- Every if statement must also have an else.
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": bear_room() elif next == "right": cthulhu_room() else: try: 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." start()
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 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() thing.apple() 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!