IDLE

Python Interpreter Quick Tutorial

Start IDLE in Windows:  Start->Programs->Python->IDLE

From here you can type and run anything you want.

 

Using the Python Interpreter for Printing a single line

The symbols “>>>” indicate that this is a Python interpreter.  It will run any valid Python statement or evaluate any valid Python expression.  You can always use a Python interpreter (with the “>>>” symbols showing) to test out a single Python statement.  Here are some simple examples.

This first statement is actually a complete Python program, consisting of a single statement.

>>> print “howdy”

howdy

 

Above, you see that the program has been run and the result of running the program is shown – in the single line showing the word howdy.

Try another program like this:

>>> print “this program works”
this program works

Each time you type in a statement (at the >>> prompt), Python will run the program.  Now try writing a program where you make a mistake.

>>> prnt "hello world"

SyntaxError: invalid syntax

 

Here, the interpreter is telling you that it doesn’t understand what you typed.  (It’s not providing you with a lot of details, but it is telling you that something is typed incorrectly.  If you look at it closely, you’ll see that the word “print” has been misspelled.  I’ll fix the spelling error but then do something else wrong.

 

>>> print "hello world

SyntaxError: EOL while scanning string literal

 

In this case, the message is a little bit more informative.  “EOL” stands for end-of-line.  This message is telling us that the interpreter ran into the “end of line” while it was still in the middle of a string.  A string is any sort of characters enclosed in double quotes.  Basically, I forgot to type the final double quotes.  So Python couldn’t figure out where the string ended.

 

Here’s another attempt; this time I’ll leave out the first double quotes:

>>> print hello world"

SyntaxError: invalid syntax

 

Here’s another attempt; this time I’ll leave out all of the quotes:

>>> print hello world

SyntaxError: invalid syntax

 

Here’s another attempt, using only the word hello, without quotes.

>>> print hello

 

Traceback (most recent call last):

  File "<pyshell#5>", line 1, in <module>

    print hello

NameError: name 'hello' is not defined

 

Now … this error is different.

 

Using the Python Interpreter to Print the value of an expression

 

Try this:

>>> print 2+2

4

 

Here, Python has evaluated the expression 2+2 (and come up with the value of 4).  And then printed the value, shown on the next line.

 

Try a few more:

>>> print 2 + 5 – 1

 

>>> print (2+5) * 2

 

(Python prints out 6 after the first statement and prints out 14 after the second statement.)

 

The Assignment Statement

 

In Python, and in most programming languages, you can use variables.  A variable has a name; you get to choose the name (as long as it starts with a letter or the _ character and it doesn’t have any embedded spaces.  Variables are able to “hold values” for you.  You can put a value into a variable by using an assignment statement.  Here’s an example:

 

>>> x = 6

 

In this case, the Python interpreter doesn’t say anything in response, but internally it now knows that you have a variable called “x” and it knows that the value of x is 6.  You can confirm this by printing out x, like this:

 

>>> print x

6

 

You can see that Python has printed out the current value of x; it is 6.

 

 

Now you can look at the error message up above when I tried to do this: print hello

At this point, Python thought that hello was the name of a variable.  But since I hadn’t ever given this variable a value, it was undefined.  That’s why Python said: NameError: name 'hello' is not defined

 

You can use whatever variables names you like.  How about this:

 

>>> world = 4

 

Python has no reponse – so everything seems to be okay.  To check it, let’s print out the value of the variable called world:

 

>>> print world

4

 

You can see that Python has 4 as the value of the variable called world.

 

You can also print out the value of an expression.  Try this:

 

>>> print x + world

10

 

You can see that Python knows how to add.

 

Try this:

>>> print world + x


(Python responds with 10)

 

Try this:

>>> print x + x

 

(Python responds with 12)

 

Try this – with no spaces before or after the + sign:

>>> print x+x

 

(Python responds with 12)

 

Try this:
>>> print x-x

 

(Python responds with 0)

 

You can assign a new value to a variable.  Try this.

 

>>> x = 3

 

(Python says nothing)

 

Then try:
>>> print x

3

 

Python has no memory of the fact that x used to be 6; it just knows that x is 3 at the moment.

 

Try this:

>>> print x+world

 

What do you expect?  Python responds with 7.

 

You’ll notice that the assignment statement uses the equal sign but it does NOT mean the same thing as it does in algebra.  To see this, try out the following Python statement:

 

>>> x = x + 3

 

This is invalid in algebra.  (x is never equal to x+3.)  But what happens in Python?  Remember that this is an assignment statement.  Python will evaluate the expression on the right side of the equal sign, take that value, and assign it to x.  Since the current value of x is 3, we would expect the new value of x to be 6 (3 + 3).  Let’s try it:

 

>>> x=x+3

>>> print x

6

 

You can see that the Python interpreter was silent when we said x = x+3 (which indicates that the assignment statement was successful).  Then, when we printed x, we found that it’s current value (the new value) is 6.  Let’s try it again.

 

>>> x = x + 3

>>> print x

 

What do you expect?  (Python prints 9.)

 

Basically, an assignment statement in Python uses the equal sign to take the value from the right side and then assigns it to the variable on the left side.  You can think of the qual sign as representing an action that is essentially a left-pointing arrow.

 

 

What happens when you close the interpreter?

 

Let’s try it.  I’ll close the interpreter window.  And then start a new one (a new Python shell).

 

Let’s try to print out x.

 

>>> print x

 

Traceback (most recent call last):

  File "<pyshell#0>", line 1, in <module>

    print x

NameError: name 'x' is not defined

 

You can see that the Python interpreter has NO idea what the value of x is.  This shows you that the interpreter only remembers things during a single session.

 

Typing Python programs into a file (rather than using the interpreter to run single statements)

 

If you would like to write a program and save it in a file do the following…

From the menu in IDLE, Select File->New Window

Type your code here (in the new window).  You can type line after line – until you have the complete program that you want.

When you are finished, save your file by selecting File->Save, go to your My Documents folder and create a new folder for all the wonderfully python programs you will create and save your file in that new folder.  Note: you’ll only need to create this folder the first time; from then on, you can just go to the MyDocuments folder, then go to the folder you use to store your Python programs, and then save your file.  When you save your file, be sure to use “.py” at the end of the file name.  This allows the Python interpreter (and everyone else) to know that this is a Python program.

In order to run your program, simply hit F5 or click on the “Run” item and choose “Run Module”.  This will cause the Python interpreter to execute your program – the one you just saved – in the Python interpreter window.

If you change your program, then just save it and hit F5 (or Run/Run Module) again.k

One thing to notice is that every time you hit F5 (or Run/Run Module), the Python interpreter restarts.  You can see that by setting a variable in the interpreter, then reloading your program, and then trying to print out the variable.  You’ll see that the variable is undefined.  I did it this way:

Here’s my program, saved in a file called howdy.py.
print "howdy there folks"

 

Here’s what happened when I hit F5 the first time:

>>> ================================ RESTART ================================

>>>

howdy there folks

Then I set a variable and printed it like this:

>>> print x

5

Then I hit F5 again.  Here’s what happened.

>>> ================================ RESTART ================================

>>>

howdy there folks

 

Then I tried this:

>>> print x

Traceback (most recent call last):

  File "<pyshell#2>", line 1, in <module>

    print x

NameError: name 'x' is not defined

 

So… you can see that when it says “RESTART” it really has restarted the interpreter and any variables that you’ve defined are lost.

 

To let IDLE know where you will be saving your programs you’ll want to force a run of the program by hitting F5 from your program editor (or selecting Run->Run Module from your program editor). 

This will force a run of your program in IDLE along with resetting IDLE so that it now knows where to look for your future programs.

Now that IDLE knows where to look for your future programs you can create other programs and save them in that same directory.  To run these new programs (or to run the first program you wrote) you do not normally want to use F5 again as you may have typed code into IDLE that you want preserved (F5 will reset all variables in IDLE when it restarts.  Instead, you want to type the following in IDLE.  Let’s assume you named your second program program2.py and saved it to the same location as you saved your first program.

>>>import program2

This is my second program…

>>> 

 

The import is used the first time you want a program loaded into IDLE however.

If you want to now change program2 and re-run it, this time after you save your code, do the following:

 

>>>reload(program2)

This is still my second program…

>>> 

 

Notice: the import statement does NOT need parentheses. But the reload function DOES need parentheses!  (That’s just the way it is.)

 

It won’t hurt anything if you typed ‘import program2’ a second time, it just won’t run your code again without the ‘reload’.

 

You can continue to type code into IDLE, and you can continue to create programs outside of IDLE for your permanent records.  Just remember that every time you restart IDLE you’ll have to configure it to search for files in the right place by opening a program in the IDLE editor and running it with F5. 

 

Special Notes for when you use IDLE from your PSU/MCECS Account

 

When you log into a Windows machine in one of the PSU laboratories, you will see a Desktop directory and you will also see a My Documents directory.  (This is normal.)  But, under the covers, the MCECS/Computer Action Team has set the system up so that these two directories (Desktop and My Documents) associated with your userid are actually stored somewhere else, on a main server.  There are several nice things about this.  First, if you log into one computer in the lab today and then you log into a different computer tomorrow, you will be using the same “My Documents” and “Desktop”.   They are your own personal folders that are directly associated with your userid.  Second, the server where your Desktop and My Documents folders are stored are backed up regularly.  So, if any of the desktop computers that you use, e.g., in a lab, happen to crash and lose their hard drive, you won’t lose any files;  your files (in My Documents and Desktop) are actually stored somewhere else.

 

Because of these nice features, you should store all of your Python programs that you write in My Documents (or in some folder that you create within My  Documents). 

 

This means that EVERY time you use IDLE, you need to issue the Run/Run Module command (or hit F5) to let the Python shell (the Python interpreter) know which directory/folder that you are using.  Note: if you close the window where you are typing in your program and then create a new window, you’ll have to do the same thing.  Another way to let Python know which directory/folder you are using is to open a file (in the editor window) from the correct directory/folder.