Learn to Program using Python: Writing and Using Scripts
This document is part of a series of online tutorial lessons designed to teach you how to program using the Python scripting language.
Something for everyone
Beginners start at the beginning, and experienced programmers jump in further along. Learn to Program using Python: Lesson 1, Getting Started provides an overall description of this online programming course.
I have been taking it slow and easy for the first few lessons. My informal discussion has been designed to familiarize you with the Python interactive programming environment while teaching you some important programming concepts at the same time.
It's time to learn about scripts
This lesson provides an introduction to the use of scripts, and of necessity will depart from the interactive mode used in previous lessons.
Some system stuff
Unfortunately, it will be necessary for me to get into some system stuff in this lesson.
Don't panic! I still plan to write this lesson at a level appropriate for beginning programmers.
It will be necessary for me to get into some system stuff that really has nothing in particular to do with Python programming. Rather, it will involve getting your computer set up for using scripts with Python.
Up to this point, I have concentrated on the interactive programming capability of Python. This is a very useful capability that allows you to type in a program and to have it executed immediately in an interactive mode.
But, interactive can be burdensome
By now you may have realized that you sometimes find yourself typing the same thing over and over. That is where scripts are useful.
Scripts are reusable
Basically, a script is a text file containing the statements that comprise a Python program. Once you have created the script, you can execute it over and over without having to retype it each time.
Scripts are editable
Perhaps, more importantly, you can make different versions of the script by modifying the statements from one file to the next using a text editor. Then you can execute each of the individual versions. In this way, it is easy to create different programs with a minimum amount of typing.
You will need a text editor
Just about any text editor will suffice for creating Python script files.
You can use Microsoft NotePad, Microsoft WordPad, Microsoft Word, or just about any word processor if you want to.
The Programmer's File Editor
I'm fond of an editor named The Programmer's File Editor. You can learn about it at the following URL: http://www.lancs.ac.uk/people/cpaap/pfe/
Arachnophilia and NoteTab
Must be a plain text editor
Whichever editor you choose, make certain that it produces plain ASCII text in its output (no bold, no underline, no italics, etc.).
Combining scripts with interactive mode
It is also possible to combine script files with interactive mode to incorporate pre-written scripts into interactive programs.
In order to use script files, you must prepare you computer to use them. This doesn't amount to much in the way of effort, but it is critical.
Where is Python located?
When you first installed your Python software, a directory should have been created somewhere on your hard drive containing a file named python.exe.
On a Windows system, unless you forced the program to be installed somewhere else, it was probably installed somewhere on your C-drive. I forced the program to be installed on my D-drive.
On my machine, the file named python.exe is in a directory named Python, which in turn is contained in a directory named Program Files on my D-drive.
My directory listing
To help you get oriented, here is a list of files appearing in my Python directory running under the WinNT 4.0 Workstation operating system. The file mentioned above is highlighted in boldface.
If you are running under Windows, I expect that your Python directory will contain pretty much the same set of files.
However, if you are using some other operating system, your list of files may be different, but hopefully there will still be a file named python.exe (or the equivalent executable file for your system).
Setting the path
You will need to cause the directory containing the file named python.exe to be listed in your system environment variable named path.
Do you know how to set the path?
If you already know how to set the path, go ahead and do it. If you don't already know how, you may need to get some help.
I'm not going to try to tell you how to do it, because the procedure varies from one operating system to the next, and if you don't do it correctly, you may cause problems that are difficult to recover from. (I don't want to be responsible for that.)
Get help if you need it
So, if you need help in setting the path, get it, but make sure that the person helping you knows what he is doing.
Create a script file
Once you have the path variable properly set, use any plain text editor and create a file named junk.py that contains the Python programming statements shown in Figure 1. (Note that the figure number at the bottom is not part of the program.)
Where do I put the script file?
Store this file in any directory on your hard drive. You may want to create a new directory for the sole purpose of storing Python script files.
A command prompt window
Then open a command prompt window and make the directory containing your new script file become the current directory.
How do I open a command prompt window?
With MS Windows, you can open a command prompt window by pulling up the Start menu, selecting Programs, and then selecting Command Prompt. At least this is how it is done on WinNT 4.0 Workstation. Win95 and Win98 should be similar.
The procedure will be different if you are using some other operating system.
What is the current directory?
If you don't know how to cause a particular directory to become your current directory, you will need to get someone who knows how to navigate the directory structure to show you. It isn't rocket science, but it will be different for different operating systems, so you will need to know how to do it on your operating system.
Running your script file
Once you have accomplished all of the above, you should be able to enter the following command at the command prompt and you should see the result of executing your script appear on the screen.
Figure 2 shows what my screen look like when I do this. (Again, as a reminder, the figure number at the bottom is not part of the program output.)
I highlighted my command
I highlighted the command that I entered in boldface (in Figure 2) to separate it from the command prompt and the output produced by the program.
Is this the correct output?
If you go back to the script file and do the arithmetic, you will see that the output value of 13 produced by the program is correct.
Congratulations are in order
If you got an output value of 13, -- congratulations -- you have just written and executed your first Python script.
Obviously, there is a lot more that you will need to learn before you can write that "killer script" that takes the world by storm, but at this point, you have the tools to experiment with some simple scripts.
I recommend that you look back into the earlier lessons and convert some of the interactive programs listed there into scripts, execute them, and confirm that the scripts behave as expected.
In the future...
In future lessons, I will switch back and forth between scripts and interactive mode, depending on which seems to be the most appropriate at the time.
I like cut-and-paste programming
However, I am a strong advocate of cut-and-paste programming.
Cut-and-paste programming works well with scripts, and not so well with interactive mode.
Therefore, any time there is very much typing involved, you can usually expect to see me using scripts instead of interactive mode.
1. In your own words, what is a script?
Ans: A script is a text file containing the programming statements that comprise a Python program.
2. A script is a one-time affair, True or False?
Ans: False. Script files are reusable.
3. Convert the interactive program shown in Figure 3 into a script, execute it, and confirm that you get the correct result.
x=6 # create and populate x
>>> y=5 # create and populate y
>>> x+y # add x to y and display the sum
In case you didn't get any output, note that unlike in interactive mode, in order to cause a script to produce an output, you will need to use a statement something like the following:
4. Convert the interactive program shown in Figure 4 into a script and execute it.
a=b=c=10 # assign 10 to several variables
>>> a+b+c # add them together
>>> a=b=c=20 # assign 20 to same variables
>>> a+b+c # add them together
5. Convert the interactive program shown in Figure 5 into a script and execute it.
6. Convert the interactive program shown in Figure 6 into a script and execute it.
>>> _+22 # add 22 to the continuation variable
If you were unable to get this to work, don't be dismayed. The special variable whose name is the underscore character is available only in interactive mode. Therefore, you can't use that variable in a script, and you will need to develop a workaround.
7. Convert the interactive program shown in Figure 7 into a script and execute it.
Copyright 2000, Richard G. Baldwin
About the authorRichard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
This material was originally published in June, 2000.