CMSC 14100 — Lecture 2

Using a computer to program

One of the main things that we'll be doing in this class is programming, or translating an algorithm into precise enough language that a machine can execute it. I mentioned earlier that algorithms are really just constructive proofs and if a program is just a very specific instance of an algorithm, what's the difference between a proof and a program? It turns out there really isn't one!

This idea of formalizing and encoding proofs into something that even a machine could execute was a question that was being explored as early as the early 20th century. It's this question that ultimately led to the first results in computer science in the 1930s. Notice that all of this happens well before computers as we know them existed.

We call the algorithm translated into instructions that a computer can understand a program—the act of creating a program is programming. To learn how to program, we also need to learn at least one programming language and if we want to run our programs on a real computer, we will unfortunately have to learn how to use computers and various tools.

This extra trickiness is one reason why computer science classes often seem difficult. Is it computer science really that tricky? Maybe, maybe not. But we often don't separate computer science (which we talked about last class) with programming (which we didn't really talk about now). So that's really two things that we're trying to learn. Add to this the laundry list of tools that are needed to program and an entire programming language and suddenly, it makes sense that there's going to be a bit of a learning curve because we're really dealing with at least three different things.

Our language of choice is Python (specifically, Python 3). Python is a language created by Guido van Rossum in the early 90s. It is in wide use today, with a reputation for being quick to pick up and pleasant to read. It is also fairly general and many of the concepts used can be applied to other languages.

Here is the first program that everyone writes. (Why? This practice is largely due to the influence of the 1978 Kernighan and Ritchie book "The C Programming Language".)


print("Hello world!")
    

There are a few things to notice here.

Now that we have some code, how do we use it? There are two main ways to execute Python code: by interacting with the interpreter, or by running a Python program.

The interpreter can be run from the terminal by using the command ipython3. You can think of the interpreter as a very, very powerful calculator: you plug in code and it runs it immediately and gives you a result. This is very useful for trying things out and running one or two lines of code.

The other way involves writing an entire program in an editor like Visual Studio Code, saving it as a file, and running that file like a program. Suppose we have a program saved to a file called hello.py. Then to execute this program we can run

python3 hello.py

Here, hello.py is just a text file with Python instructions in it. By convention, we name these files with a file extension of .py.

Version Control

Another tool we'll be using is a version control system. This will be one of the ways you manage assignments and submission. Broadly, a version control system is for managing versions of your code. For our purposes, this means tracking and managing changes.

Version control systems are extremely useful tools for keeping track of changes and managing large projects where multiple people will simulatneously be working on parts of it. While they were developed for managing software projects, version control systems can be used to manage many other kinds of projects where being able to track changes and manage concurrent work by multiple people is necessary.

There are many version control systems out there (Subversion, CVS, Perforce, etc.). We will be using git, originally authored by a guy named Linus Torvalds. It is available for download on all of the major platforms at git-scm.com. git is currently the most popular and widespread VCS in use. In addition to being used almost everywhere nowadays, it will be used in many future CS courses here.