15-112 Lecture 1 (Tuesday, May 21, 2013)

Accessing the University's computers

In order to ensure that your work can be properly graded, please use the University's computers. We want to, for example, ensure that your work is done on the same version of Python, so we can grade it correctly and accurately, with no surprises.

To do that, you want to ssh to unix.andrew.cmu.edu or ghcXX.ghc.andrew.cmu.edu, where XX is 01, 02, 03, ..., 81. unix.andrew is a pool of machines that exist exclusively for remote access. the ghc machines at seats in labs, but you can use them remotely, also.

From a Mac or Linux machine, you can use a terminal window. From there, for example, ssh unix.andrew.cmu.edu. From a Windows machine, you'll need an "ssh client". You can download one here: Tectia SSH client.

Basic Linux Commands

Our servers run Linux, which is very unfamiliar to many of you. In class, I introduced many of the basic commands as I used them during demos. But, you probably want to study up.

The basics of the file system's organization are described in About AFS. This tutorial will help you to understand how files are organized and accessed.

The basic commands used to access the file system, run programs, and generally navigate and manage your use of the computer are described here: UNIX Commands.

Programs are human-readable documents. They can be writtedn and edited in any text editor that can support saving files in "Plain (ASCII) text". In class, I used my favorite tool, which is pretty old school, vi. A quick start guide can be found here: Steve Litt's VI Quick Start.

Another popular old school tool is emacs. You can find a quick start guide here: Columbia University's A Quick Guide to Emacs.

There are also a ton of other, greeat, more modern, tools, such as Xcode for Macs. Or the open source Eclipse. But, to give you fair warning, although these tools are prettier and, by some measures, more powerful and easier to use -- they tend to have a higher start-up cost.

Interpreted vs. Compiled Programs

Computers require very detailed instructions formatted in a way that is very hard for humans to understand. To avoid spending our time operating at at an unnecessary level of detail, and formatting instructions in a way that is natural for machines, but unnatural for people, we write programs in a high level language that gets translated by another computer program into the detailed, machine-oriented instructions that the computer can directly understands.

In some languages, such as C, C++, or Java, this translation is typically performed before the program is run, in advance. The tool that performs this step is called the compiler. And the result, is called a compiled program.

In some languages, such as Python, Ruby, and Perl, the high level instructions are translated as the program runs. In these cases, a program called an interpreter is run. It is given the high level program as input, and translates it step-by-step as it runs.

Compiled programs tend to run faster, because the work is done in advance. But, interpreted programs are faster to edit and maintain, because there is no need to compile before running.

This semester, we'll program in Python.

"Hello World" in Python

We began class with a program:


print "Hello world!"

In this example, the first line, "#!/usr/bin/python" runs the program, "/usr/bin/python", whcih is the python interpreter, and gives it the file to run as input. The "#!" is actually a "cookie" understood by the operating systems loader. It tells the operating system that, instead of trying to load and run this text file, which isn't directly runnable, it should load and run "/usr/bin/python", giving it this file as input, and let it interpret it. Basically, it tells the operating system that this is a "Script" or "Intepreted program", and where to find the interpreter.

We then ran this program from the console, by typing, "./python.py". It, unsurprisingly, printed "Hello world!" to the screen.

We also used a variant of this with 'Hello world' (notice the single quotes), which did the same thing. And, we printed a double quote, by enclosing it in single quotes, and vice-versa.

What do we take from this?

Variables and Arithmetic

The next example we looked at shows that Python represents things in much the way that you learned in Algebra or Pre-Algebra in middle or high school.


x = 5
y = 7

z = x + y
print z

z = x * y
print z


We notice above that...

We also looked at the example below, which demonstrates that Python understand decimal-notation "fractional" numbers, and can do math with them, and even mixing that mixes them with integer numbers:


w = 2
x = 5.2
y = 7.3

z = w + x
print z

z = w * y
print z


Strings Strings represents text, including characters, such as represent letters, symbols, and numbers. They are called strings, well, because we string together these characters. As we charge forward, we'll learn that strings are essentially listss of characters -- but more on that soon.

For right now, let's take a look at a quick example:


name  = "Gregory Kesden"

# Notice the difference between printing name with, and without, quotes
print name
print "name"

# Notice the len() length function. 
print "The length of name is",
print len(name)      # This prints 13

print name[0]
print name[1]
print name[2]

print name[0:4]
print name[0:4] + name[4:len(name)]


So, here's what we want to notice about the code above:

Console Input

Programs get more fun when we can get data from outside of the program, rather than only statically from within it. One way to do this is to get it from the user, as illustrated below:


name = raw_input("name> ")
print name

number1_string = raw_input("number> ")
number1 = int(number1_string)

number2 = int(raw_input("number> "))

# here We add two "int" numbers
print number1 + number2

# here we try to add a number represented as a string to a number -- Oops!
print number1_string + 5

What are the take-aways?

The last line shows us what happens if we don't do the conversion -- Python gives us an error, because the "+" operator adds numbers, but concatenates strings. It tries to concatenate a number to a string -- and can't do that. (If we wanted, we could, of course, cast the number to a string, and then concatenate using the string's "+" operator).

Traceback (most recent call last):
  File "./example.py", line 15, in 
    print number1_string + 5
TypeError: cannot concatenate 'str' and 'int' objects