Python en:First Steps
Revision as of 19:10, 18 March 2009
In This Guide: |
Introduction
We will now see how to run a traditional 'Hello World' program in Python. This will teach you how to write, save and run Python programs.
There are two ways of using Python to run your program - using the interactive interpreter prompt or using a source file. We will now see how to use both of these methods
Using The Interpreter Prompt
Start the interpreter on the command line by entering python at the shell prompt.
For Windows users, you can run the interpreter in the command line if you have set the PATH variable appropriately.
If you are using IDLE, click on Start → Programs → Python 3.0 → IDLE (Python GUI).
Now enter print('Hello World') followed by the Enter key. You should see the words Hello World as output.
$ python Python 3.0b2 (r30b2:65106, Jul 18 2008, 18:44:17) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print('Hello World') Hello World >>>
Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement. We use print to (unsurprisingly) print any value that you supply to it. Here, we are supplying the text Hello World and this is promptly printed to the screen.
- How to Quit the Interpreter Prompt
- To exit the prompt, press ctrl-d if you are using IDLE or are using a Linux/BSD shell. In case of the Windows command prompt, press ctrl-z followed by enter key.
Choosing An Editor
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 (and also VIM) will automatically help you do this.
If you are using Linux/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 be benefitted by 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 Linux and BSDs in their respective repositories.
We will explore how to use IDLE in the next section. For further details, please refer the IDLE documentation.
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.
I repeat once again, please choose a proper editor - it can make writing Python programs more fun and easy.
- For Vim users
- There is a good introduction on how to make Vim a powerful Python IDE by John M Anderson.
- For Emacs users
- There is a good introduction on how to make Emacs a powerful Python IDE by Ryan McGuire.
Using A 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 <ref>The author of the amazing 'Beginning Perl' book</ref> 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.
<source lang="python">
- !/usr/bin/python
- Filename: helloworld.py
print('Hello World')
</source>
Run this program by opening a shell (Linux terminal or DOS prompt) and entering the command python helloworld.py.
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 and 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.
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 Linux/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 this just prints the text 'Hello World'. We will learn about functions in a later chapter, what you should 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.
Executable Python Programs
This applies only to Linux/Unix users but Windows users might be curious as well about the first line of the program. First, we have to give the program executable permission using the chmod command then run the source program.
$ chmod a+x helloworld.py $ ./helloworld.py Hello World
The chmod command is used here to change the mode of the file by giving execute permission to all users of the system. Then, we execute the program directly by specifying the location of the source file. We use the ./ to indicate that the program is located in the current directory.
To make things more fun, you can rename the file to just helloworld and run it as ./helloworld and it will still work since the system knows that it has to run the program using the interpreter whose location is specified in the first line in the source file.
What if you don't know where Python is located? Then, you can use the special env program on Linux/Unix systems. Just change the first line of the program to the following:
<source lang="python">
- !/usr/bin/env python
</source>
The env program will in turn look for the Python interpreter which will run the program.
So far, we have been able to run our program as long as we know the exact path. What if we wanted to be able to run the program from anywhere? You can do this by storing the program in one of the directories listed in the PATH environment variable. Whenever you run any program, the system looks for that program in each of the directories listed in the PATH environment variable and then runs that program. We can make this program available everywhere by simply copying this source file to one of the directories listed in PATH.
$ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin $ cp helloworld.py /home/swaroop/bin/helloworld $ helloworld Hello World
We can display the PATH variable using the echo command and prefixing the variable name by $ to indicate to the shell that we need the value of this variable. We see that /home/swaroop/bin is one of the directories in the PATH variable where swaroop is the username I am using in my system. There will usually be a similar directory for your username on your system. Alternatively, you can add a directory of your choice to the PATH variable - this can be done by running PATH=$PATH:/home/swaroop/mydir where '/home/swaroop/mydir' is the directory I want to add to the PATH variable.
This method is very useful if you want to write useful scripts that you want to run the program anytime, anywhere. It is like creating your own commands just like cd or any other commands that you use in the Linux terminal or DOS prompt.
- Caution
- W.r.t. Python, a program or a script or software all mean the same thing.
Getting Help
If you need quick information about any function or statement in Python, then you can use the built-in help functionality. This is very useful especially when using the interpreter prompt. For example, run help(print) - this displays the help for the print function which is used to print things to the screen.
- Note
- Press q to exit the help.
Similarly, you can obtain information about almost anything in Python. Use help() to learn more about using help itself!
In case you need to get help for operators like return, then you need to put those inside quotes such as help('return') so that Python doesn't get confused on what we're trying to do.
Summary
You should now be able to write, save and run Python programs at ease. Now that you are a Python user, let's learn some more Python concepts.
References:
<references/>