April 4, 2000
This is the first in a series of online tutorial lessons designed to teach you how to program using the Python scripting language.
There is something for just about everyone here. Beginners start at the beginning, and experienced programmers jump in further on. You simply need to enter the series of lessons at the point that best fits your prior programming knowledge.
If you don't know anything about programming, these lessons will take you from ground zero to the outer-space habitat of the modern object-oriented computer programmer.
If you already know how to program, these lessons will teach you how to program using the Python scripting language.
If you already know how to program using Python, these lessons will teach you how to use the Python scripting language to take advantage of the ever expanding Java programming environment, without the requirement to learn how to program in Java.
If you already know how to program using Java, these lessons will teach you how to use Python for rapid prototyping of Java programs.
When you complete this series of tutorial lessons, you will have completed the equivalent of three or four semesters of computer programming studies at the community college level.
The fact that you are reading this page probably means that you already know how to use a computer on the web. I'm not talking about rocket science here. I am talking about knowing how to download and install software from specified web sites, and how to execute that software offline.
Windows would be handy
Although not absolutely necessary, it would also be very handy for you to have access to, and know how to use Microsoft Windows. The programming skills that I am going to teach you are applicable to a broad range of operating systems. However, I will use Microsoft Windows as my teaching platform, so you will need to be able to follow instructions explained in Windows jargon.
Strong desire to learn
This should be self-explanatory, but just in case it isn't, I will make a couple of comments in this regard.
Don't expect a miracle to happen
Simply reading these lessons won't cause you to experience a miraculous transformation that will cause recruiters to start waving money at you.
Practice, practice, and more practice
I am going to provide you with a lot of information, but you will also need to put your brain in gear and write a lot of programs to make the information stick in your brain. Very few people become expert musicians without a lot of practice. Similarly, very few people become expert programmers without a lot of practice.
So, if you want to learn to program, plan to spend a lot of time in front of your computer, not just reading, but programming as well.
First of all, it's free. I like that. You won't need to invest an arm and a leg just to get started. All you will need to do is go to http://www.python.org/ and download the software. Yes, Virginia, there really is a Santa Claus, and he lives on the Web.
What is Python?
You might also want to visit http://www.python.org/doc/Summary.html,
which is where I extracted the following:
|“Python is an interpreted, interactive, object-oriented programming
Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, as well as to various windowing systems (X11, Motif, Tk, Mac, MFC). New built-in modules are easily written in C or C++. Python is also usable as an extension language for applications that need a programmable interface.
The Python implementation is portable: it runs on many brands of UNIX, on Windows, DOS, OS/2, Mac, Amiga...
Python is copyrighted but freely usable and distributable, even for commercial use.”
What is JPython?
Another very important reason that I chose Python is the availability of JPython. JPython will become very important later in the course once we become involved in the use of the Java class libraries.
The following was extracted from http://www.jpython.org/docs/whatis.html.
|“JPython is an implementation of the high-level, dynamic, object-oriented
language Python seamlessly integrated with the Java platform and certified
as 100% Pure Java.
JPython is freely available for both commercial and non-commercial use and is distributed with source code.
JPython is complementary to Java and is especially suited for the following
A pathway into Java
In other words, Python and JPython provide a relatively painless pathway into the exciting, and often complex world of Java.
To learn more about Java, visit my online Java tutorials at this URL.
More facts, less hype
So, beyond the hype, why did I really choose Python? Because it is an interpreted, interactive, object-oriented programming language that is relatively easy to learn.
Learn OOP without the details
With Python, you can learn object-oriented programming without the need to understand many of the complex details.
A beginner-friendly language
Python is a beginner-friendly language that takes care of many of the complex details for you behind the scenes. This makes it possible for you to concentrate on the big picture of what your program is intended to accomplish without getting bogged down in detail.
Sneaking up on OOP
With Python, you can “sneak up” on object-oriented programming concepts. You don't have to deal with the object-oriented nature of the language on the first day, or for a long time, for that matter. (With Java, you can't write even the simplest program without encountering a need to understand object-oriented programming concepts.)
Python has an interactive mode
The interactive mode makes it easy to try new things without the burden of the edit-compile-execute cycle of other programming languages such as Java. Interactive mode is a very friendly environment for persons in the learning stages of a language.
Python has a non-interactive mode
Once you know that something works properly, it is very easy to “put it in a can” so to speak, and then execute it as a script. This lets you execute it without retyping it every time.
Python combines interactive and non-interactive modes
This combination gives you the ability to use previously written script files while working in the interactive mode. Using this approach, you are able to minimize your interactive typing effort while still writing and testing large Python programs in interactive mode.
Small core, large library
Like Java, Python has a small compact core and a large, extensible library. Thus, much of what you will need to do has already been written and tested for you. Your task will be to write the code to glue those library components together, and to write new capabilities on an as-needed basis.
The first step is to go to http://www.python.org/ with your web browser. Download, and install the Python software using the download link that you will find there.
I'm going to assume that either you already know how to do this, or you can get help from a friend. In other words, I'm not going to try to explain how to download and install the Python software.
Starting the programming environment
As I mentioned earlier, all of my instructions will be based on the use of Python running on my Windows NT operating system. I believe that those instructions will also be directly applicable to Windows 95 and Windows 98.
However, as mentioned earlier, you have many choices for using Python. If you are running on some other platform, you will need to translate my instructions from Windows jargon into the jargon of your platform.
Enough talk; let's begin
Find the Start button on the task bar on your Windows desktop. Select
where x.y is the version of Python that you have downloaded and installed. At the time of this writing, I have version 1.5.2 installed on my computer, and the selection shows on the menu as Python 1.5.
What do you see?
When you make this selection, you should see a menu having at least the following four options
Don't be bashful about reading the manuals. There is a wealth of information contained there, including a short, to-the-point tutorial written by the author of Python, Guido van Rossum.
The library reference
A good way to get a feel for the breadth and power of Python is to select Python Manuals from the menu and then select the Library Reference link. (The Library Reference is also available online as of the time of this writing.)
Most of what you see there probably won't mean much to you at this point in time, but hopefully will be familiar territory after you complete this course.
IDLE (Python GUI)
When I select the first item on the menu on my Windows NT machine, I get an error message about a missing DLL file. But when I click OK, the message goes away and everything seems to work anyway.
This selection brings up a window entitled “Python Shell.” This is one of the interactive programming environments available with Python on Microsoft Windows.
What does it look like?
When you bring up the Python Shell, you should see something like Figure 1 at the top of the window. (Note that I inserted some line breaks to force the text to fit in this narrow page format.)
What is a GUI?
GUI is an acronym for Graphical User Interface. This window is a GUI. It can be used for interactive Python programming.
To make it go away when you are finished, simply click the button in the upper-right corner that is labeled with an X.
The Python (command line) selection
When I select the second item in the menu mentioned above, I get essentially the same thing as the GUI, but in a “black screen” window commonly referred to as a DOS box, a console, a command-line window, or whatever you choose to call it.
The command-line window
This window can also be used for interactive Python programming in much the same way that the Python Shell can be used. Each has some practical advantages and disadvantages.
To make this screen go away, hold down the Ctrl key and press the Z key (Ctrl-z). Then press the Enter key. (You can also click the X in the upper right-hand corner.)
Your first Python program
You can use either of these windows to write and execute your first Python program.
Hello World in Python
As has become the custom in programming circles, we will make our first Python program one that displays “Hello World” on the computer screen. We will write and execute it interactively.
The Python prompt >>>
The three right-angle brackets that you see in both of the interactive screens (>>>) make up the Python interactive prompt. When the cursor is blinking to the right of that prompt, you can enter a Python programming statement interactively.
Let's do it
Type the following text to the right of the Python prompt and press the Enter key:
print "Hello World"
If all goes well, your interactive Python screen should then look something like Figure 2.
Pay particular attention to the line that reads Hello World following your entry. That is the output from your program.
You have just written (and executed) your first Python program, and possibly your first computer program as well. Not only that, you only had to type one line of code to write and execute your program.
Note that your entire program, the output from your program, and a new prompt are all shown in Figure 2.
Python is ready for more
Python has provided a new prompt so that you can expand your program, or write another one.
The Java version of Hello World
In contrast, the simplest program that I know how to write in Java is shown in Figure 3.
This Java program also displays “Hello World” on the screen, but the program output is not shown in the box. This box shows only the program code.
The edit, compile, execute cycle
This Java program code must first be captured in a Java source file. Then it must be compiled. After it is compiled, it can be executed to produce “Hello World” on the computer screen.
More complex than Python
While not very complex by programming standards, this Java program is certainly more complex than the one-line Python program shown earlier. A great deal more programming knowledge is required to write, compile, and execute the Java program.
The following three steps were are required to write and execute this Java program:
Ans: False. These lessons will have something for just about everyone, depending on where the user enters the sequence of lessons. Beginners start at the beginning, experienced programmers jump in later on.
2. Python is useful only as a compiled language, True or False?
Ans: False. Python can be used in an interactive, interpreted, or compiled mode.
3. JPython is part of the Java programming language, True or False?
Ans: False. JPython is an implementation of Python integrated with the Java platform from Sun and certified as 100% pure Java, but was not written by Sun, and not belonging to Sun.
4. Both Python and JPython are free, True or False.
Ans: True. As of this writing, both are freely usable and distributable, both for commercial and non-commercial use.
5. Python is an object-oriented language, True or False.
6. Python has a large core and a small library, True or False?
Ans: False. Just the opposite is true, small core and large library.
7. What does the Python interactive prompt look like?
Ans: The Python prompt is >>>
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
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.