Python Introduction

Install Python 2.7

You can skip this section if you are using a lab computer with Python already installed. Otherwise, you want to download 2.7.x from the Python web site, not 3.y.z.

To choose the right installer, you need to select between Mac and Windows, as well as between 32 and 64-bit. If you’re not sure about the machine word size, from the Windows start menu you can choose Control Panel » System and Security » System, and look for the System type.

The install process is entirely straightforward, just work your way through the steps. I’ll illustrate them below, but you probably won’t need it.

Start your program

  1. From the Windows Start menu, select Python 2.7 » IDLE (Python GUI). There will be a similarly named program in /Applications/Python 2.7 on the Mac.

  2. A window called the “Python Shell” will appear. It displays a prompt like “>>>”. This is the window in which you will interact with your program.

  3. You need a second window, in which you will type and save the content of your program. Select File » New Window from the menu.

  4. While in the new window, select File » Save and give it a filename that ends with .py (this will enable syntax coloring). You can also choose the folder to save it in, such as your Documents or Desktop folder.

  5. Type your code into the .py file, and save it with File » Save, or Ctrl-S or ⌘-S.

  6. Use Run » Run Module (F5) to run it, and interact with the program in the Shell window.

Printing messages

Here is a program you can try that prints messages on the screen. You can copy and paste this block into your .py file, then save and run. (Note: the coloring used on this web page may not precisely match the colors you see in IDLE.)

print "Hello!"
print 3+4
print "3+4"
print "2*7 is", 2*7

When you run this program, the Shell window will contain:

>>> ====================== RESTART ======================
>>> 
Hello!
7
3+4
2*7 is 14
>>> 

The double quotes indicate a string of characters (text). That portion is output exactly as written. Any portion not in quotes is interpreted by Python. Thus the difference between printing 3+4 (which produces 7) and printing "3+4".

Fixing errors

The most common error you will see is probably “Invalid syntax”. It pops up like this:

Dismiss that message, and you see it also highlights a portion of your program in red.

Often the red part is just after the source of the actual error. In this case, we omitted the comma between the two different things in the print statement on that line.

Another kind of error shows up in the Shell window. It might look like this:

Traceback (most recent call last):  
  File "C:/Users/league/Desktop/myprogram.py", line 4, in   
    print "2*7 is", x  
NameError: name 'x' is not defined

The actual error is the last line. This one is called a NameError. But the Traceback section can help you too, by telling you which line number to examine (in this case, line 4).

Doing calculations

We can store variables very simply in Python by using an equals sign. Variable names consist of a sequence of letters, numbers, and underscores (no spaces), but they cannot start with a number. Here are examples of valid variable names:

  • quiz3
  • the_last_day
  • Frank
  • x

These are not valid:

  • quiz 3 (contains a space)
  • 2nd_quiz (starts with a number)

Variable names are case-sensitive, so x and X are both valid, but they are not the same variable. Below is a program that uses variables to perform a computation.

quiz1 = 32
quiz2 = 40
quiz3 = 16
average = (quiz1 + quiz2 + quiz3) / 3.0
print "Average is", average

When you run it, the output should look something like this:

>>> ====================== RESTART ======================
>>> 
Average is 29.3333333333

Python (and most languages) distinguish between numbers that are integers (whole numbers), and numbers that can contain decimal points. The latter are called floating-point numbers. When you divide two integers, Python performs integer division, which eliminates any remainder. You can overcome this by including a decimal point in your numeral, even if it’s just 3.0 – that forces it to be represented as floating-point.

>>> 9/2
4
>>> 3/4
0
>>> 9/2.0
4.5
>>> 3.0/4
0.75

Getting input

Python has a very handy built-in function for receiving input from the user of your program. It works like this:

name = raw_input("Enter your name: ")

On the left side of the equal sign is a variable name. On the right side is the raw_input function. In the parentheses, you specify a string that will be the prompt presented to the user. When you run the above program, it looks like this:

>>> ====================== RESTART ======================
>>>
Enter your name: 

You are expected to type something at this point, and press enter. Whatever string you type will be placed into the variable name. Here’s a more complete sample program:

name = raw_input("Enter your name: ")
print "Welcome to my program,", name

year = int(raw_input("What year were you born? "))
age = 2013 - year

print "You are", age, "years old."

Running the program looks like the following. The parts the user types are indicated by «angle quotes».

>>> ====================== RESTART ======================
>>> 
Enter your name: «Chris»
Welcome to my program, Chris
What year were you born? «1988»
You are 25 years old.