Learn-by-example lessons with games and puzzles

Home

Introductions

Now that you've installed Python, it's time to play with it.  In your Start Menu, under Python, select "Idle (Python GUI)".  (GUI stands for Graphical User Interface.)  You should see a "Python Shell" window appear that looks something like this:

Select the window, and you'll notice a flashing vertical line | to the right of the >>>.  Type print('Hello, world!') and press Enter.  The result should look like this:

>>> print('Hello, world!')
Hello, world!
>>> 

Imagine that this shell window is a kind of sandbox for playing with Python programs.  Within the shell, each ">>>" is called a prompt.  A prompt with a flashing vertical line cursor lets you know that the shell is waiting for your next instruction.  Just now, you gave the instruction to print a message "Hello, world!", and that's exactly what Python did.  The single-quotes ' ' tell Python where your message starts and stops.  The parentheses ( ) tell Python where your information for print begins and ends.  Now try printing your name.  Do the same thing, but this time put your name instead of world inside of the single-quotes.  If your name happened to be Rumpelstiltskin, it would look like this:

>>> print('Hello, Rumpelstiltskin!')
Hello, Rumpelstiltskin!
>>> 

Again and again, the shell reads what you type, evaluates what it means, and prints any result of what is computed.  It's OK to make a mistake.  All programmers do.  Look what happens when we forget to put a single-quote and right parenthesis ') before pressing Enter:

>>> print('Oh no! A mistake!

SyntaxError: EOL while scanning string literal (<pyshell#2>, line 1)
>>> 

Mistakes in programs are called errors or bugs.  Errors will be a common part of learning.  As a programmer, you should expect that your instructions will have errors, be patient with yourself, and look at understanding and correcting the errors as a puzzle.  As problem solvers, programmers write program code (many instructions), test their code, and correct or debug their code.  Coding, testing, and debugging happen again and again until there is success.  It takes patience, practice, and persistence.  Stick with it!  Enjoy the puzzling that comes with programming, and you'll become an excellent problem solver.

What a program prints out is called output.  When a program reads in what you type, it is called input.  Type in the two lines you see below to see an example of input and output:

name = input('What is your name? ')
print(name)

When you press Enter after the first line above, the 'What is your name? ' message is printed, and Python waits for your input.  Type your name and press Enter.  The prompt appears again.  Enter the second line.  Something interesting has just happened.  There are no single-quotes inside the print, and instead of printing "name", the name you entered was printed.  Let's see why.

So far, all of our input and output information has been a sequence or string of characters.  In programs, we often put single-quotes around these strings of characters, which are usually just called strings.  In this simple two-line program, we read in an input string and then print it out.  But between reading the input and printing the output, we have stored the input string in a named location in the computer's memory called a variable.  It is called a variable because the value we store there can vary (change). Variable means changeable.  When we type "name =", what we are telling Python to do is to take the value after the equal "=" and store it in a variable called "name".   This is called an assignment, because the value to the right of the equal is assigned to (placed in) the variable to the left.  I find it helpful in my thinking to replace the equal with a left arrow (←), and imagine the value going into a mailbox with the name of a variable.

Now enter the second line print(name) again.  You'll see that the string with your name is still stored in variable name.  The variable name has become a symbol for the name you entered.  If you ever want to see what's in a variable just type the variable and press Enter.  Try it now with variable name.  If your name were Arthur, what you've just done might look like this:

>>> name = input('What is your name? ')
What is your name? Arthur
>>> print(name)
Arthur
>>> print(name)
Arthur
>>> name
'Arthur'
>>> 

Notice that the value of variable name is shown with single-quotes.  You can also use single-quotes to show the stop and start of a string.  Try printing a simple message using single-quotes:

>>> print('Hello, world!')
Hello, world!
>>> 

What happens if we try our input example above using an apostrophe within the string?  Let's see:

>>> name = input('What's your name? ')

SyntaxError: invalid syntax (<pyshell#8>, line 1)
>>> 

The problem is that the single-quote in "What's" is part of our string, but Python thinks we're using that single quote between the "t" and "s" to end the string, and Python can't understand the meaning of the characters that come next.

Just as plus (+) adds numbers together, we can add or concatenate two or more strings, creating a new string made by gluing the others end to end.  Try entering this line:

print('Hello, ' + name + '!')

Soon, you'll want to know how to save your instructions so that you can use them again.  Type Control-N (or menu File New Window) and a new window will be created.  In that window, type the following code:

name = input('What is your name? ')
print('Hello, ' + name + '!')

Now save the file by typing Control-S (or menu File Save As...).  Under "File name:", name the file "hello.py" and click the Save button.   You have now created a Python module.  You can run the module by pressing F5 (or menu Run Run Module). 

To Play is to Learn

For fun, try typing these instruction statements:

for i in range(100):
	print(name + ' ', end='')

For now, you don't need to understand these lines.  In fact there are many interesting programming pieces we'll be playing with very soon.  Soon, you'll be playing with code for simple games.  Step by step, piece by piece, you'll come to understand the programming patterns you'll see again and again.  When I began to program, I typed in other programmer's code ... sometimes incorrectly!  Over time, I studied the examples, played with them, modified them, and came to understand the beautiful patterns inside.  I hope you enjoy your discoveries too!

If you ever want to exit the shell, just click the red close window button at the top, or choose File → Exit from the menu.

© 2009 Todd Neller