This is Part I of Understanding Programming: A Primer, an exclusive series for EDUKWEST designed to teach you the basics of computer programming. This series is designed to be a tutorial and you will be the most successful if you work through the tutorial with your text editor open. Key in the code as it appears and try to make it run correctly yourself. You’ll find that by the end of this series, you’ll be able to develop simple programs and understand the process of computer programming.
Before we write any programs, we have to get your development environment set up. I won’t make you buy anything (with the exception of one $2.99 program, if you want to run this all on an iPad), and you may even have a few options to choose from.
Figure 1.1: The Author’s Development Environment. Doobie Brothers’ music not included
Setting Up Your Development Environment
On the Mac
If you’re using a Mac you have very little set up to do. We’re going to be using a language called Python in this series and it runs natively in the Mac’s command line. If you’ve never used the command line before, it’s pretty easy to find. Look inside your applications folder and find the utilities folder. Inside the utilities folder you’ll find the “Terminal” program. Double-click it.
Figure 1.2: The Utilities folder on the Mac
When you double-click, after a moment, the terminal program will load and the command line will display. The cursor will appear flashing at the end of a line known as the prompt.
Figure 1.3: The terminal in the author’s computer. Note that the author changed the default colors through the terminal’s preferences
On the PC
The PC command line does not process Python code out-of-the-box like the Mac. I recommend you use the virtual Linux server provided by my friends at Koding.com. Point your browser to www.Koding.com and register for their free account. The free account gives you a virtual Linux machine that you can access through your web browser–and it can process Python code identically to the Mac.
Once you have your account, click the green button toward the upper left-hand corner of the screen that looks similar to a DOS prompt. That will put you in the command line of your terminal.
Figure 1.4 Command Line in Koding.com. You can using Koding.com on a Mac as well if that is your preference.
On the iPad (if you dare)
I wanted to include this option for the tablet-centric. I should note that I haven’t actually tried coding on the iPad myself, but I imagine it will work just fine. In the app store for your tablet you’ll find Python 2.7 by Jonathan Hosmer. This is a full Python development environment for Python programming.
If you elect this option, download the app and read whatever documentation is included. The process for executing code will likely be different since we are working in a command line environment, however the code will be identical.
Figure 1.5: Python on iOS requires this $2.99 download through iTunes
If you do get all this working on the iPad, I would like to hear from you. Let me know your thoughts on using this environment and on coding on a tablet in general.
Writing and Running Your Code
Now that you have your environment set up, it’s time to (finally) write some code. We’ll write code in a text editor. A text editor is different than a word processor. A word processor will introduce invisible formatting code into a file. If you were to run code written in a word processor it would likely error due to the embedded formatting code.
A text editor, on the other hand, saves the text you enter as pure text–without introducing any unnecessary formatting. The text produced by a text editor can be easily processed by the Python interpreter. Within the command line environments in the Koding.com virutal machine and on the Mac there are two text editors which you can use. They are known as nano and vi. These text editors have a heritage that goes back to the 1970’s–so as you use them you can truly feel “old school!”
Nano is an easy to use Linux-based editor. It’s pretty intuitive. You can simply start typing your code when the nano environment loads. Nano has a number of control codes which are listed at the bottom of your screen for your reference. The most important to note are CTRL-O which saves your file and CTRL-X which exits nano and returns you to the command line.
By default, nano will save your file in the same folder you were in when you opened nano itself.
Figure 1.6: The nano text editor.
You can start nano by typing nano and then hit enter on the command line.
vi is a slightly more complex (and even more “old school”) text editor. The most important thing to understand about vi is that there are two modes–command mode and insert mode. To move into insert mode you press ‘i’ on your keyboard. When you want to move into command mode to issue commands like write and quit, hit the ESC key. If you screw up and find yourself in the wrong mode, press ESC a lot and that seems to get you back into command mode from wherever you are at.
There’s an old joke about vi that goes like this: VI has two modes, and you’re in the wrong one. If you don’t think it’s funny now, you will after you use vi for a bit!
When you enter vi, type i to move into insert mode and you’ll be able to type your code. When you’re done, hit ESC to move into command mode. All commands must be preceded by a colon. So to write your file you’ll enter :w. To quit you’ll enter :q. vi also lets you combine commands. For example, the combination :wq will write your file and then quit back to the command line.
Figure 1.7: vi wants you to help poor children in Uganda. It offers limited help for new programmers worldwide.
You can start vi by typing vi and then hitting return on the command line.
So now you should have your text editor started and be ready to enter your code. Enter the following code into your text editor exactly as it appears:
print “Hello World.“
print “This is my first program. Programmers are rock stars!“
It should be at least somewhat evident what this code does. The print command prints content to the command line. In this case you are printing two strings. Strings are simply lines of characters. Strings are always enclosed in quotes.
Once you’ve typed the code it’s time to run it. In nano, save your file by typing CTRL-W. When prompted, enter the filename HelloWorld.py. The “.py” extension indicates that this is a Python source code file. Next enter CTRL-X to quit and you’ll be returned to the command line. In vi, enter command mode and then save your file by typing :w HelloWorld.py. This will write your file under the appropriate file name.
Figure 1.8: Saving your file in vi.
Finally, in vi, hit :q to quit and return to the command line.
Now your cursor should appear at the prompt. Run the program by entering the following at the prompt (Don’t type the ‘$’– That just represents the prompt itself):
$ python HelloWorld.py
If you’ve done everything correctly you should see the result of your program printed before the next prompt.
Figure 1.9: Execution of the HelloWorld.py program
If your output doesn’t look similar to figure 1.9, you need to load your text editor and code again and find your error. You can reload your code at the command line prompt with the following command:
vi: vi HelloWorld.py
Congratulations! You have successfully written your first program.
Understanding the Print Command
In the preceding example we used the print command to print out a string. The print command is also capable of printing out the following:
Integers: Integers are “whole numbers” and don’t have a decimal point. In Python, you can print an integer like this:
Floating Point Numbers: Floats are more precise numbers that include a decimal point. In Python you can print a floating point number like this:
Expressions: Expressions must be evaluated before a result is determined. Expressions are usually arithmetic problems. To print an expression in Python:
print 3.0 / (6+4)
In the expression above, the part of the expression in parenthesis will be evaluated first due to the order of operations. The result printed would be 0.3.
We’ll look at expressions more closely in our next lesson.
Please contact the author at email@example.com if you are interested in this and similar lessons for classroom use.
If you are interested in a formal version of this course for yourself or others, LearnToProgram has a special discount for EDUKWEST readers. Programming for Absolute Beginners is the starting point for our Development Club which provides over 30 self-paced courses, and as many live courses as you want to take. For more information and to access the EDUKWEST reader discount, visit: