This tutorial works with the Linux operating system. If you're not a Linux user, take a look at Appendix A: Using the Linux operating system.

Hello, world!

Whether you're an experienced programmer or an absolute beginner, the hallmark exercise of learning a new language is writing a simple script that prints out the greeting "Hello, world!"

To begin, we'll open up the terminal, which is a program that allows us to send commands to the Linux shell. The shell is the interface between you and the Linux kernel which controls the CPU. To start up the shell, open up the main menu and find the terminal. It will be in a different location depending on your flavor of Linux. I'm running KDE 3.5 and the path is Menu -> Utilities -> Terminal.

You should see an empty screen with some character or text in the upper left corner followed by a blinking cursor. Depending on what type of shell your version of Linux is running the text will be something like $ or ~> or servername~>. It doesn't matter. I'm using a shell called the Korn shell, my display is shown below.

Type the four commands shown below, pressing the "Enter" key after each command to execute it.

The first command is mkdir, which stands for "make directory." If you're used to graphical operating systems such as Windows or Mac OS, a directory is simply another term for a folder. Here we create a directory named pyscripts to store our Python scripts.

The second command is cd, which stands for "change directory." We change to the pyscripts directory we just created -- this is like double clicking a folder to open it.

The third command is touch, which is a Linux program that either creates a file with the specified file name or updates the timestamp of an existing file. We create a file named "hello.py."

The last command is emacs, which starts up a text processing program. When you enter this command, Emacs should open up in a new window. There are many Linux programs for text processing. We're using Emacs here because it is the most popular. If you are familiar with another program, such as vi, feel free to use it instead.

The Emacs window has three sections. The large white section where you type code into is called the buffer window. Text in the buffer isn't written to a file, it is floating in your computer's RAM until you actually save the file.

Below the buffer window is the mode line. Right now our mode line is telling us that we have opened up "hello.py".

The final section is the echo area. Emacs echos, or prints, the results of commands there for us to read.

Type the following code into your buffer window.

After you are finished, save the file by clicking File -> Save, or by typing Ctrl-x Ctrl-s.

If you saved the file correctly, Emacs will print something like "Wrote /somepath/pyscripts/hello.py" in the echo area.

Let's go through this script line by line:

The first line, #!/usr/bin/env python tells the shell that we are now programming in Python. The second line imports a code module that has already been written and stored elsewhere on the computer. The sys module allows us to interact with the system and access certain system variables. Everything written after the " # " sign is a comment, and is not executed by the computer. In the third line, we create a variable, named " ver " and assign it a value with the equals sign. Here we set version equal to some text from the system module, telling us what version of Python we are using. The fourth and fifth lines will print text to the terminal when the script is executed. The first printed line will say, "Hello, World!" The second line will say, "My version of Python is:" and then it will print the value of the ver variable we created earlier. The plus sign here joins the two phrases together.

Your text will likely remain black instead of changing to a mix of red, beige and magenta, as mine did above. To see if your version of Emacs is already equipped to recognize Python code, click Options -> Syntax Highlighting (Global Front Lock Mode). If the text changes color, you're good to go. If it does not, and you want Emacs to do syntax highlighting in your future scripts, visit Appendix B: Enabling Emacs to recognize Python.

Now close Emacs by clicking File -> Exit Emacs, or by typing Ctrl-x Ctrl-c.

Go back to the Terminal window, and type python hello.py like below. This command tells the shell to use Python to execute the script we just wrote.

If your script was written correctly, the terminal will respond by outputting the results of the script. If you made a typo in the script, an error message will print. In a simple script such as this one, it will be easy to use the message to diagnose and fix your error.

Congratulations, you just wrote your first program in Python.