Course Home
Syllabus
References
Reflections

Lessons

Related Links

Introduction

These videos by Bucky Roberts present the basics of Python. As you watch the videos, try to run the code yourself in IDLE. Then try the exercise below. Exercises will generally include information that is not included in the videos, so it's import to work through both the videos and the exercises.

Numbers and Math
Variables
How to Save Your Programs

Exercise: The IDLE Environment and a Preview of Strings

This exercise was taken from Learn Python The Hard Way. It has been edited to fit our IDLE 2.6.6 development environment.

1
2
3
4
5
6
7
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.'

Select "New Window" from the "File" menu. Use "Save" from the "File" menu to create file named ex1.py. The ".py" extension identifies the file as a Python program. Type the above into the window and save the file as needed.

NOTE: On these pages, code that belongs in Python files generally appear with line numbers as in the listing above, or in a screen shot of an IDLE editor window, like this:

sample .py file

The IDLE editor is the tool for writing and saving programs. The Python Shell can be used as a "practice area" for typing in statements directly or for testing out the programs you've written in the editor.

Warning

Do not type the numbers on the far left of these lines. Those are called "line numbers" and they are used by programmers to talk about what part of a program is wrong. Python will tell you errors related to these line numbers, but you do not type them in.

While the window for ex1.py is selected, choose "Run Module" from the "Run" menu.

If you did it right then you should see the same output I have below. If not, you have done something wrong. No, the computer is not wrong.

What You Should See

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

You may see some other stuff before the RESTART line. This is fine, but if your output is not exactly the same, find out why and fix it.

If you have an error, you may see something like this:

EOL error

It's important that you can read these since you will be making many of these mistakes. Even experienced programmers make many of these mistakes.

EOL stands for "End Of Line". An end of line character is the character you type when you press the Return key. The end of line character doesn't show up on the screen. You know you've pressed it because your cursor moves to the next line.

The message above tells you that Python unexpectedly ran into an end of line character while it was collecting characters for a string (If you're not sure what strings are, don't worry. Some videos on strings are coming up. Just consider this a preview of coming attractions). Let's take a look at a version of ex1.py that could cause this error:

Notice that Python has drawn a red rectangle and placed the cursor inside the rectangle. This rectangle tells us where Python ran into trouble. This rectangle is drawn after trying to run ex1.py and getting the dialog box with the error message. The information on the bottom right of the screen tells us that the cursor is at line 3, column 26.

After the print command on line 3, Python saw the double quote and started collecting characters for a string. When Python sees a double quote, it starts collecting string characters until it finds another double quote. The trouble started when Python got to column 26. Python found an end of line (EOL) character, which is not a valid string character.

How do we fix the problem? Add a double quote to the end of line 3:

Now everything's cool. But how do you fix problems when you don't understand the error? Google can be a big help here. Just do a search on the error message. You'll usually get some useful information.

Extra Credit

The Extra Credit contains 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. Put a '#' (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.
  3. Use the '#' to make your script print only one of the lines.

Note

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

Exercise: Numbers and Variables

Create a Python file (for example, ex2.py) and put the following code into it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven


print "There are", cars, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to put about", average_passengers_per_car, "in each car."

Note

The _ in space_in_a_car is called an underscore character. Find out how to type it if you do not already know. We use this character a lot to put an imaginary space between words in variable names.

What You Should See

Try to Run the code. The output in the Python Shell window should look like this:

>>> ================================ RESTART ================================
>>>
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.
>>>

Extra Credit

When I wrote this program the first time I had a mistake, and python told me about it by sending me a message in the Python Shell:

>>> 
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport

Traceback (most recent call last):
  File "/Users/tjleone/Documents/Projects/AWE/Python/learn-python-the-hard-way/ex4.py", line 14, in 
    print "We can transport", carpool_capacity, "people today."
NameError: name 'carpool_capacity' is not defined
>>> 

Explain this error in your own words. Make sure you use line numbers and explain why.

Here's more extra credit:

  1. Explain why the 4.0 is used instead of just 4.
  2. Remember that 4.0 is a "floating point" number. Find out what that means.
  3. Write comments above each of the variable assignments.
  4. Make sure you know what = is called (equals) and that it's making names for things.
  5. Remember _ is an underscore character.
  6. Try running python as a calculator like you did before and use variable names to do your calculations. Popular variable names are also i, x, and j.