Video Coming Soon...

Created by Zed A. Shaw Updated 2024-12-10 18:57:40

1: A Good First Program

If you skipped Exercise 0, then you are not doing this book right. Are you trying to use IDLE or an IDE? I said not to use one in Exercise 0, so you should not use one. If you skipped Exercise 0 please go back to it and follow at least the "Minimalist Instructions" but you should do the entire exercise eventually. It's a great way to introduce yourself to the computer.

You should have spent a good amount of time in Exercise 0 learning how to install Jupyter, run Jupyter, run the terminal, and work with both of them. If you haven't done that, then do not go on. You will not have a good time. This is the only time I'll start an exercise with a warning that you should not skip or get ahead of yourself.

Type the following text into a Jupyter cell:

View Source file ex1.py Only

print("Hello World!")
print("Hello Again")
print("I like typing this.")
print("This is fun.")
print('Yay! Printing.')
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')

Your Jupyter cell should look something like this:

Don't worry if your Jupyter window doesn't look exactly the same; it should be close though. You may have a slightly different window header, maybe slightly different colors, and the left side of your Jupyter window won't be the same, but will instead show the directory you used for saving your files. All of those differences are fine.

When you create this cell, keep in mind these points:

  1. I did not type the line numbers on the left. Those are printed in the book so I can talk about specific lines by saying, "See line 5..." You do not type line numbers into Python scripts.
  2. I have the print at the beginning of the line, and it looks exactly the same as what I have in in the cell. Exactly means exactly, not kind of sort of the same. Every single character has to match for it to work. Color doesn't matter, only the characters you type.

Once it is exactly the same, you can hit SHIFT-ENTER to run the code. If you did it right then you should see the same output as I in the What You Should See section of this exercise. If not, you have done something wrong. No, the computer is not wrong.

What You Should See

The Jupyter output will look like this after you hold SHIFT and hit ENTER (which I'll write as SHIFT-ENTER):

You may see different window appearance and layout, but the important part is that you type the command and see the output is the same as mine.

If you have an error it will look like this:

Cell In[1], line 3
  print("I like typing this.
        ^
SyntaxError: unterminated string literal (detected at line 1)

It's important that you can read these error messages because you will be making many of these mistakes. Even I make many of these mistakes. Let's look at this line by line.

  1. We ran our command in the Jupyter cell with SHIFT-ENTER.
  2. Python tells us that the cell has an error on line 3.
  3. It prints this line of code for us to see it.
  4. Then it puts a ^ (caret) character to point at where the problem is. Notice the missing " (double-quote) character at the end though?
  5. Finally, it prints out a "SyntaxError" and tells us something about what might be the error. Usually these are very cryptic, but if you copy that text into a search engine, you will find someone else who's had that error, and you can probably figure out how to fix it.

Study Drills

The Study Drills contain things you should try to do. If you can't, skip it and come back later.

For this exercise, try these things:

  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.

From now on, I won't explain how each exercise works unless an exercise is different.

An "octothorpe" is also called a "pound", "hash", "mesh", or any number of names. Pick the one that makes you chill out.

Common Student Questions

These are actual questions that real students have asked when doing this exercise.

The Blue Plus

If you ever want to create a file with Jupyter and use its editor you can use the "blue plus" as shown in this image which I've placed a giant red circle around:

The blue plus in the top left of the Jupyter window

If you don't see this then you most likely started Jupyter wrong by accidentally starting the Jupyter Notebook in your start menu instead of typing jupyter-lab.

Previous Lesson Next Lesson

Register for Learn Python the Hard Way, 5th Edition (2023-2024)

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.