Editor and source file
Before we move on to writing Python programs in source files, we need an editor to write the source files. The choice of an editor is crucial indeed. You have to choose an editor as you would choose a car you would buy. A good editor will help you write Python programs easily, making your journey more comfortable and helps you reach your destination (achieve your goal) in a much faster and safer way.
One of the very basic requirements is syntax highlighting where all the different parts of your Python program are colorized so that you can see your program and visualize its running.
If you are using Windows, then I suggest that you use IDLE. IDLE does syntax highlighting and a lot more such as allowing you to run your programs within IDLE among other things. A special note: Do not use Notepad - it is a bad choice because it does not do syntax highlighting and also importantly it does not support indentation of the text which is very important in our case as we will see later. Good editors such as IDLE (Notepad++ and also VIM) will automatically help you do this.
If you are using GNU/Linux or FreeBSD, then you have a lot of choices for an editor. If you are just beginning to program, you might want to use geany. It has a graphical user interface and has buttons to compile and run your python program without a fuss.
If you are an experienced programmer, then you must be already using Vim or Emacs. Needless to say, these are two of the most powerful editors and you will benefit from using them to write your Python programs. I personally use Vim for most of my programs. If you are a beginner programmer, then you can use Kate which is one of my favorites. In case you are willing to take the time to learn Vim or Emacs, then I highly recommend that you do learn to use either of them as it will be very useful for you in the long run.
In this book, we will use IDLE, our IDE and editor of choice. IDLE is installed by default with the Windows and Mac OS X Python installers. It is also available for installation for GNU/Linux and BSDs in their respective repositories.
If you still want to explore other choices of an editor, see the comprehensive list of Python editors and make your choice. You can also choose an IDE (Integrated Development Environment) for Python. See the comprehensive list of IDEs that support Python for more details. Once you start writing large Python programs, IDEs can be very useful indeed.
The Source File
Now let's get back to programming. There is a tradition that whenever you learn a new programming language, the first program that you write and run is the 'Hello World' program - all it does is just say 'Hello World' when you run it. As Simon Cozens puts it, it is the 'traditional incantation to the programming gods to help you learn the language better' :) .
Start your choice of editor, enter the following program and save it as helloworld.py
If you are using IDLE, click on File → New Window and enter the following program. Then click on File → Save.
#!/usr/bin/python #Filename: helloworld.py print('Hello World')
Run this program by opening a shell (GNU/Linux terminal or DOS prompt) and entering the command python helloworld.py. (In Windows, you could also find the folder in which you saved the program, and then drag the actual file into the command line after you've typed python. This will automatically add the file path after the python.)
If you are using IDLE, use the menu Run → Run Module or the keyboard shortcut F5.
The output is as shown below.
$ python helloworld.py Hello World
If you got the output as shown above, congratulations! - you have successfully run your first Python program.
In case you got an error, please type the above program exactly as shown above and run the program again. Note that Python is case-sensitive i.e. print is not the same as Print - note the lowercase p in the former and the uppercase P in the latter. Also, ensure there are no spaces or tabs before the first character in each line - we will see why this is important later.
A word of caution on the shebang. If you are a Windows user, you probably need to edit that to something like
and some GNU/Linux or Unix users may have to change it to something like
#!/usr/local/bin/python3.2 or #!/usr/bin/python3.2
The standard for Python 2.x if you have both Python 2.x and 3.x installed
For Python 3.x you can use
I recently signed up to a remote hosting service where I had to start python scripts "hard-wired" with the correct path like above.
How It Works
Let us consider the first two lines of the program. These are called comments - anything to the right of the # symbol is a comment and is mainly useful as notes for the reader of the program.
Python does not use comments except for the special case of the first line here. It is called the shebang line - whenever the first two characters of the source file are #! followed by the location of a program, this tells your GNU/Linux and Unix system that this program should be run with this interpreter when you execute the program. This is explained in detail in the next section. Note that you can always run the program on any platform by specifying the interpreter directly on the command line such as the command python helloworld.py .
- Important -Use comments sensibly in your program to explain some important details of your program - this is useful for readers of your program so that they can easily understand what the program is doing. Remember, that person can be yourself after six months!
The comments are followed by a Python statement. Here we call the print function which just prints the text 'Hello World'. Please understand now is that whatever you supply in the parentheses will be printed back to the screen. In this case, we supply 'Hello World' which is referred to as a string - don't worry, we will explore these terminologies in detail later.