1. How to Program, Part I
  2. How to Program, Part II
  3. How to Program, Part III
  4. How to Program, Part IV
  5. How to Program, Part V
  6. How to Program, Part VI
  7. exercises
  8. pyMPI tutorial
  9. Calculating PI, Part I
  10. Calculating PI, Part II
  11. Calculating PI, Part III
  12. Dividing Work
  13. More MPI
  14. Poogle - Web Search
  15. Mandelbrot Sets
  16. Mandelbrot, The Code
  17. Mandelbrot, The Images
  18. Mandelbrot In CUDA
  19. Conway's Life, Part I
  20. Life Code Listing
  21. Conway's Life, Part II
  22. MPI Life Code Listing

How to Program, Part I

Please take the survey.


This is to be a very basic tutorial in how to program, designed for the complete novice. It will be based on Python, because that language is:

  1. Free (you can get it from here)
  2. Has a simple and uncluttered syntax.
  3. Runs on virtually all computers.
  4. Makes it easier to introduce concepts one at a time.
  5. All sources used in these python lessons can be obtained from here
Go to the C:\Python27 directory to run programs from the command line. Use either "write" or "notepad" to create text files. Note that if you create a file with notepad choose "All Files" as the type. Here we see an example of someone running the "hello" program illustrated below. Notepad remains open so that you can edit the program.

As an illustration of this last point, consider your python program:

apple.py
1print "apple"
$ python ./apple.py
apple

This is a complete program that will execute on any computer.

All the examples in this tutorial will be formatted in the above fashion. We show the file name in the blue header, the code listing with line numbers below it, and an execution of the file below that using white text on a black background.

The program name serves as a link to a plain text version of the program which you can easily download.

The following links show you how to download all the course files at once and give them a test run:

  1. Setup on Windows
  2. Setup on a Mac
  3. Linux users can figure this out from the Mac setup. :)

This program shows you how to use the print statement. You can tell python to print any words you want, and it will. Try it!

Now let's look at your second program:

applex.py
1x = "apple"
2print x,x
$ python ./applex.py
apple apple

Here you assign a variable. We set x equal to "hello" and then are able to use it in print statements. You can think of the variable "x" as being like a special box where you can store and identify things. The command "print x,x" prints what's in the box twice.

The bit of text inside the quotations is commonly called a "string." Thus, we say that x is a string variable. We could have used single quotes instead of double quotes, and the program would work the same way.

If we want, we can add "comments" to the code. Comments are pieces of text which do not affect the execution of a program, but does help humans to understand the code better.

Here is an example of the use of comments:

helloc.py
1# the variable x contains a greeting
2x = "hello" # I should remember to change this to aloha for the Hawaiian version
3 
4print x,x
$ python ./helloc.py
hello hello

For our third program, let's make a small calculator:

shopping.py
1gallon_milk = 3.89
2loaf_bread = 1.50
3box_cereal = 2.50
4groceries = 3*gallon_milk + 2*loaf_bread + 5*box_cereal
5print groceries
$ python ./shopping.py
27.17

This program prints the cost of our groceries. Now let's add sales tax.

shoptax.py
1gallon_milk = 3.89
2loaf_bread = 1.50
3box_cereal = 2.50
4groceries = 3*gallon_milk + 2*loaf_bread + 5*box_cereal
5tax = 0.06 * groceries
6total = groceries + tax
7print "groceries=",groceries,"/tax=",tax,"/total=",total
$ python ./shoptax.py
groceries= 27.17 /tax= 1.6302 /total= 28.8002

Obviously this program is not rounding to the nearest cent -- but we won't explain how to do that just yet. Already you have learned how to do something useful with Python! You can use it as a calculator.