Learn Python the Hard Way — Exercise 1

Exercise 1 is titled “A Good First Program.” This exercise begins by assuming you’ve successfully installed Python and downloaded a text editor in order to embark upon your coding journey. Given those prerequisites, Exercise 1 looks something like this:

ex1.py
1 print "Hello World!"
2 print "Hello Again"
3 print "I like typing this."
4 print "This is fun."
5 print 'Yay! Printing.'
6 print "I'd much rather you 'not'."
7 print 'I "said" do not touch this.'

Pretty basic exercise. Just printing a bunch of lines to be read. The key thing I got out of this exercise was learning the procedure for setting up and running these programs. I’ve been using Windows for all of my Python learning, so this entailed using Powershell to run these programs. You might be making the same mistakes I made at first when trying to get these things to run, so here’s a few things to consider:

  1. Did you run Python upon first opening Powershell?
  2. Did you properly set up a directory where you will save your programs?
  3. Did you change to that directory in Powershell?
  4. Did you type “python ex1.py” into the terminal?

These mistakes will stop you from even getting started, regardless of if you typed your code correctly or not, so ensure you’ve set yourself up correctly. The book details how to read and troubleshoot code errors, so I’ll leave that to the reader to sort out. One thing to note, as Shaw does, is make sure you’re literally typing “print” at the beginning of each line. Otherwise you just have a mess of words that don’t do anything.

Shaw encourages these additional exercises as Study Drills for Exercise 1:

  1. Make your script print another line.
  2. Make your script print only one of the lines.
  3. Put a “#” (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.

For part 1, all that needs to be done is simply type another line beginning with “print” per the rest of your lines, and make whatever statement you wish.

8 print "Coding is so much fun."

Parts 2 and 3 are related, and you will see the connection as soon as you find out what the “#” character does. I’ll wait…

OK, you’ve probably figured out that it is used for commenting, or adding literal descriptions into your code. Type the hash, type your words, boom. Comment made. However, it can also be used to “disable” code by turning it into a “comment.” From this, you can see all that is necessary to instruct your program to print just one line of the code is to “comment out” all of the rest. And thus, Exercise 1 and the Study Drills have been completed. Hooray! What a great first step, right?