Session 10

LPTHW – Lesson 36, 37, 38, 39

Lessons 36 and 37 were mostly review, so nothing too interesting there, but I do plan to periodically return to the keyword lists on p122 and the Python docs in general to ensure that I retain these keywords. Also, I skipped the homework assignments for these two lessons (create a game, read some code) as I plan to do both on a large scale after I’m finished with this book.

Lesson 38 covered lists and string manipulation. It introduced .split(‘ ‘) to split a string into a list and ‘ ‘.join() to reverse the process.

Lesson 39 was a critical one – it introduced dictionaries. Specifically, it covered:

The syntax to add a new item to a dictionary

cities[‘OR’] = ‘Portland’

How to iterate over a dictionary

for state, abbrev in states.items():

How to access one item in a dictionary

state = states.get(‘Texas’, None)

How to check if that ‘get’ retrieved anything useful

if not state:

print “Sorry, no Texas”

Another way to do the same thing:

city = cities.get(‘TX’, ‘Does Not Exist’)

It also introduced collections.OrderedDict if you need a dictionary that retains its order.

Finally, I found (what I think is) a really clear explanation of iterators and generators for a beginner: https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do

Session 9

LPTHW – Lesson 33, 34, 35

These lessons covered while loops and list indexes, but they also combined a few concepts from the previous lessons into a more complex exercise (another game). I’m noticing some recurring themes in these games by the way… Bears, Cthulhu… Anyway.

Interestingly, one of the most useful things I came across today was by accident. If you hit the up arrow in PowerShell – or any other terminal program, I think – it will pull up your previous commands. This can save a TON of time if you are using the same commands over and over, like I am to create new Python files for these exercises.

The “bear game” also taught me some other interesting lessons:

  • If you are using a boolean variable , you can just say variable or not variable to check True/False instead of variable == True or variable == False.
  • You can either use “while True:” (like in the bear room) or a recursive call to the current function (as in the cthulhu room) to create an infinite loop.
  • It is useful to create small functions for actions that will be repeated frequently by the program.
  • State variables (bear_moved) can be useful to track changes in the program.
  • You can use int(raw_input()) and Try/Except ValueError: to check if a number was entered by a user.
  • One other note from lesson 34… Indexes start at zero! So in letters = [‘a’, ‘b’, ‘c’, ‘d’], letters[0] = a

Session 8

LPTHW – Lesson 29, 30, 31, 32

Now this is finally getting interesting! These lessons introduced:

  • if, elif and else
  • range()
  • for loops
  • lists
  • append()

Lesson 31 also included a very basic version of a text game, which is fun to modify per the study drills (the “bear room” got my creativity flowing!)

There were also 2x other takeaways from these lessons:

  • range() is inclusive of the first, but NOT inclusive of the second argument, so range(0, 10) = 1 to 9.
  • There was some good advice about using the Python interpreter to experiment, and it helped me to retain the information on lists. I will keep this in mind for the later chapters.

I am blowing through this book much faster than expected, but I seem to remember the latter half of the book slowing down a LOT as the exercises become longer and more complex.

Session 7

LPTHW – Lesson 24, 25, 26, 27, 28

Lots of review in these lessons, and not much new stuff except for Boolean Logic, which seems pretty straightforward. The new functions were:

split()

sorted()

pop()

I remember these being pretty useful for manipulating user input and sentences.

 

Session 6

LPTHW – Lesson 15, 16, 17, 18, 19, 20, 21, 22, 23

These lessons covered a lot of material, so I won’t touch on everything, just some critical points. Lesson 15 actually took me quite a while to finish; I got hung up with an issue when I tried to write to, then read from the same file, like:

new_file = open(filename, ‘r+’)

new_file.write(‘x’)

new_file.read()

new_file.close()

That approach kept adding a bunch of white-space after the ‘x’. I still haven’t figured out exactly why… but it works fine if I just open the file in ‘w’ format, write to it, close it, then re-open it in read format. One thing I have learned about Python is that if you run into an issue like this, there are probably a bunch of different ways to solve the problem. You can always use a workaround and come back to fully understand the issue later.

When reading code, I have occasionally come across *args in function definitions, but never fully understood what it meant. Now I realize it is just a bucket to hold any arguments passed to the function, so they can be unpacked within the function, kind of the same way that argv works – I’m happy to finally (I think) understand that.

Seek also seems like a useful function that probably has many applications beyond just resetting a file to its initial position. I will try to find some more creative uses for it.

Finally, I need to remember to always close() files after opening them. I also need to learn when files are closed automatically, vs when they need to be manually closed.

Session 5

LPTHW – Lesson 12, 13, 14

The main value in these lessons was the introduction of the import statement and argv (which made waaaaay more sense to me this time around). I think the difference is that I have a better understanding of the how the command line and Python interact now. I am interested to mess around with argv some more to see if I can get one script to pass input to another…

Session 4

LPTHW – Lesson 7, 8, 9, 10, 11

These lessons covered stings, escape sequences and raw_input, which is one of my favorite functions! The first time through this book, I remember feeling a real sense of discovery after learning how to combine raw_input() and int() to do basic math within a program.

A few things I noted down to remember:

  • Commas suppress newlines between two print statements
  • I should remember to keep all lines < 80 characters
  • Remember to use “”” for long paragraphs of text
  • Remember to indent additional lines in a statement (beyond the first)
  • Spend some time learning the rest of the escape sequences

Session 3

LPTHW – Lesson 6

This was a very quick day, with just one easy lesson from LPTHW (a continuation of the string formatting from the last lesson). However, I want to try to make progress whenever possible, no matter how little. That progress will add up over time! Not much to note here on the lesson itself, just some simple string formatting with variables.

Session 2

LPTHW – Lesson 2, 3, 4, 5

Another pretty easy day that covered comments, basic math, variables and strings. Nothing much to note from lessons 2, 3 and 4, but lesson 5 did re-introduce me to %-style formatting. I had actually forgotten about this. A couple of Google searches seem to indicate that it has been replaced by a different {}.format() structure. In that case, I will make sure to learn / re-learn both. Also, although these lessons didn’t explicitly teach it, I went back and reviewed int() and round() as they seemed applicable to the math section.

Skipping ahead, it looks like the next 7 lessons are also pretty basic until Chapter 13, which introduces argv. I found this topic a bit confusing the first time around, but it should be easier to understand with more background. For the next couple of days, I may mix in a couple of code-reviews or other materials to keep myself from getting bored with the simpler lessons.

Session 1

LPTHW – Command Line Crash Course, Lesson 0, 1

Well, that was easier than I expected… First of all, I should mention that I completed the first 50 chapters of this book about a year ago. I haven’t worked with Python much since then, so I decided to review the material before moving on to something new. After doing the first few chapters though, it looks like I may not need to review all of this again in detail (especially the exercises). For now, I will plan to review each chapter, but “skim” if the materials seem too familiar. After all, I’m sure that there is some stuff I have forgotten – or have forgotten that I have forgotten.

Just a few notes for these first three chapters:

  • I have decided to focus on using keyboard shortcuts whenever possible. I have already figured out most of the frequently used ones for Notepad++ and Windows generally, but I will continue to learn them from Google as necessary.
  • I remember learning the pushd and popd PowerShell commands the first time around, but never actually using them. Looks like they could be pretty useful in certain situations. I will try to use them, at least occasionally, going forward.
  • I don’t think I ever really learned how to use the mv command the last time around, I just used it to rename stuff. Maybe this review will turn out to be useful, as I seem to be picking up details I missed before.

Well that’s it for the first three chapters. I think the next few should move really quickly (mostly printing) so I plan to review a bunch of them in the next post.