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:
As an illustration of this last point, consider your python program:
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:
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:
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:
For our third program, let's make a small calculator:
This program prints the cost of our groceries. Now let's add sales tax.
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.