December 19, 2014
Hot Topics:

Learn to Program using Python: Writing and Using Scripts

  • February 10, 2004
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Preface

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.

Introduction

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.

What Is A Script?

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

Some other editors that I like are Arachnophilia, which is available at http://www.arachnoid.com/arachnophilia/ and NoteTab, which is available at http://www.notetab.com/.

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.

Getting Started

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.

  • INSTALL.LOG
  • py.ico
  • pyc.ico
  • pycon.ico
  • python.exe
  • pythonw.exe
  • UNWISE.EXE
What about your files?

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.)

a=2
b=3
a=2*a
b=3*b
c=a+b
print c

Figure 1

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.

python junk.py

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.)

D:\Baldwin\AA-School\PyProg>python junk.py
13

D:\Baldwin\AA-School\PyProg>

Figure 2

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.

What's Next

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.

Practice, practice

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.

Review

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
11
>>>

Figure 3

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:

print x+y

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
30
>>> a=b=c=20   # assign 20 to same variables
>>> a+b+c         # add them together
60
>>>

Figure 4

5.  Convert the interactive program shown in Figure 5 into a script and execute it.

>>> x=5
>>> y=6
>>> x+y
11
>>> x=5.55555
>>> y=6.66666
>>> x+y
12.22221
>>>

Figure 5

6.  Convert the  interactive program shown in Figure 6 into a script and execute it.

>>> 5+6
11
>>> _+22 # add 22 to the continuation variable
33
>>>

Figure 6

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.

>>> print "Dick\012Baldwin" 
Dick 
Baldwin 
>>>

Figure 7

 

Copyright 2000, Richard G. Baldwin

About the author

Richard 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.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel