Course Tools

Read time: 13 minutes (3391 words)

Today, we will get set up to write programs using Python. If you intend to do all of your work in the lab periods, or in one of the Computer Studies labs located at most ACC campuses, there is not much work to do. However, I highly recommend that you set up a personal computer so you can work on class assignments at home or wherever you like. I carry a laptop around everywhere I go, and can work anywhere I end up (except in Yellowstone Park - not much Internet access there!) I own several computers (too many according to my wife!) and have set up my systems so I can work on any of them.

Installing required tools

To complete this course, you will need the following tools on your system.

Note

If you need help getting any of this done, see me right away. You cannot afford to get delayed doing course work because of system setup problems. Remember you can use any Computer Studies lab at any campus to do your work.

Our Work Environment

We will not be working in a full Windows development environment for this course. Instead, we will work mainly using a simple environment called the Command Prompt window. See The Command Line for more information.

Building “Hello, World”

For reasons long forgotten, it is a tradition for new programmers in any programming language to cause their very first program to display “Hello, World!” on the console. Are you up for this?

Open up the Python interpreter

Start off by going to the command prompt window and type in this:

c:\> python

With any luck, you should see something this:

c:\> python
Python 3.7.0 (v3.7.0:1bf9cc5093. Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64) on win32
Type "help", "copyright", "credits", or "license" for more information.
>>>

Wonderful! We have a running Python to play with. See that last line with the three angle brackets? That is the Python prompt. Python is waiting for you to type in some Python code. We do not know how to do that yet, I hear you cry out! Well, let’s get out first “program” out of the way. Type this:

>>> print("Hello, World!")
Hello, World!
>>>

When you pressed the “Enter” (or “Return”) key at the end of the first line, what you typed was processed by Python, and whatever happened was displayed on the screen. Well, actually, something displayed only if that line made something get displayed, and the print thing made whatever was found inside the parentheses print. In this example, what was inside those parentheses was a string, just a sequence of characters surrounded by double quotes. You see the result on the next line followed by another prompt.

Let’s try something a bit different:

>>> message = "Hello, World!"
>>> print(message)
Hello, World!
>>>

Hmmm, what have we here? Well, actually, we told Python to save a copy of the string with the text we want to display somewhere where it can find it later. We also told Python to tag that place with the name “message”. Later we will call this name a “variable” since we can save another string later and use the same name. Don’t get frazzled, this will get clearer as we go.

That is it. If you get this working, we are set to start our adventure in programming.

I promised a look at a more interesting chunk of Python. Here is a script I use to manage a remote server. You probably cannot figure this out on your own, but you will be able to later. For now it is just an example I use all the time.

from fabric.api import *
import os

def weberver()
    env.hosts = ['www.pylit.org']
    env.svn_root = '/var/www/'

def restart_apache():
    sudo('/etc/init.d/apache2 restart')

def list_repos():
    run('ls %s' % env.svn_root)

This script is processed by a python program called fab. When I want to do a task on a remote server, I do this from the command line:

fab webserver list_repos
fab webserver restart_apache

Does not look like much here, but I do not need to log into the remote server, and issue the commands manually! It saves a ton of my time!

The important thing to note here is that we have a working Python installation.