Lab 0: Intro to Programming¶
Learning Objectives
This page should help you
get a basic understanding of how a computer works, and
learn what it means to think algorithmically.
If you already have some experience with computer programming, you may skip this page and give Lab 1 a try. There is no assignment associated with this lab.
Computers¶
Computers are everywhere. Almost every modern electronic device, from sophisticated smartphones and laptops to the humble toaster, is a type of computer. We use them every day, but what exactly is a computer, and how do they work?
This turns out to be a complex question, but we give a short summary here.
Hardware and Software¶
Computers have two types of components.
Hardware refers to the physical parts of a computer, including internal circuit boards, processing units, storage drives, and wiring, as well as external devices such as keyboards and screens. Hardware makes it possible for computers to execute instructions, store and access data, and provide ways for human users to give inputs and get outputs.
Software consists of instructions and data that tell computer hardware what to do. If hardware is the body of a computer, software is the thought and intent, the mind.
The key feature that makes computers so useful is that the same set of hardware can perform a wide variety of tasks through different software. Computer programming is the art and science of getting a computer to do exactly what you want it to do by writing software.
Programming Languages¶
To understand how to communicate instructions to a computer, it is helpful to think about how humans communicate with each other: through language. A natural language is a human language, such as English or Spanish. Natural languages consist of vocabulary (words) and grammar: rules that dictate how words can be sequenced to form intelligible communication.
A programming language is an artificial language designed for constructing computer programs. Like natural languages, programming languages have vocabularies and grammars. The set of rules governing the structure and order of a programming language is called syntax. The first task when learning a new programming language is to become familiar with its syntax, similar to learning the vocabulary and grammar of a new natural language.
Because each programming language has its own syntax, code that performs the same task in different languages often looks similar but with important differences. For example, the following blocks implement the same operation in two programming languages: Python and C++.
# Python
def naive_dot_product(x, y):
total = 0
for i in range(len(x)):
total += x[i] * y[i]
return total
// C++
#include <vector>
using namespace std;
double naive_dot_product(const vector<double>& x, const vector<double>& y) {
double total = 0;
for (size_t i = 0; i < x.size(); i++) {
total += x[i] * y[i];
}
return total;
}
Note the similarities and differences between these code blocks.
For example, C++ requires ; in several places that Python does not, and the statements after for are quite different.
These differences are due to the two languages having different syntax rules.
In this class, we will focus on a single programming language with famously simple syntax: Python.
When Grammar Ain’t Good
In natural languages, correct grammar is desirable but not strictly necessary for communication. Consider the following sentence.
I am being excite to learning more about programmings!
Although the grammar is atrocious, most humans can easily extract the intended meaning (“I am excited to learn more about programming”).
In programming languages, by contrast, correct syntax is absolutely necessary. Computers cannot infer human-intended meaning from invalid syntax; they can only parse code according to the language’s syntax rules. Incorrect syntax prevents code from being translated or interpreted as executable instructions and causes a program to crash.
No Ambiguity
Natural languages can be ambiguous, meaning one sentence can have multiple meanings, even with correct grammar. Consider the following English sentence.
I just saw a large bird with my binoculars.
This statement could mean that 1) the speaker used their binoculars to observe a large bird, or that 2) the speaker saw a large bird that had taken possession of the speaker’s binoculars. There is ambiguity due to the ordering of the words. Another example comes from Peggy Parish’s beloved fictional heroine Amelia Bedelia, a well-meaning maid who is given a set of cleaning instructions.
“Now let’s see what this list says.” Amelia Bedelia read, Change the towels in the green bathroom. Amelia Bedelia found the green bathroom. “Those towels are very nice. Why change them?” she thought. Then Amelia Bedelia remembered what Mrs. Rogers had said. She must do just what the list told her. “Well, all right,” said Amelia Bedelia. Amelia Bedelia got some scissors. She snipped a little here and a little there. And she changed those towels.
The reader can hardly blame Amelia Bedelia for following the instructions as she understood them.
Unlike Mrs. Rogers’ list, programming languages are designed to be completely unambiguous to the computer. Programming statement are interpreted according to unchanging rules, so the computer does not choose between multiple possible meanings.
Algorithmic Thinking¶
Computers are perfectly obedient: they carry out instructions exactly as they are specified. A program that does not behave as desired has imperfect instructions. An effective computer programmer must therefore learn, in addition to syntax, how to specify instructions with complete precision.
Example: Scrambled Eggs¶
To get used to this idea, consider the task of asking a young teenager to make scrambled eggs for the first time. What instructions would you give them? Perhaps this:
Get the eggs out of the refrigerator.
Whisk the eggs in a bowl.
Cook the eggs on the stove.
These instructions might be sufficient for a teenage human, because humans can think for themselves and fill in instructional gaps through their powers of observation or by asking for clarification. A computer, however, is not a human and cannot think for itself. The following instructions might be more appropriate for a computer.
Walk to the refrigerator.
Open the refrigerator door.
Take the carton of eggs out of the refrigerator.
Close the refrigerator door.
Take the carton of eggs to the counter.
Gently set the carton of eggs on the counter.
Open the cupboard door.
Take a glass bowl out of the cupboard.
Close the cupboard door.
Gently set the glass bowl on the counter.
Open the carton of eggs.
Remove a single egg from the carton.
Firmly crack the egg on the countertop.
Hold the cracked egg above the glass bowl.
Pry the cracked egg shell open and allow the egg whites and yolk to land in the bowl.
Throw the empty egg shell in the garbage.
Repeat steps 12 through 16 until there are 5 eggs in the glass bowl.
Open the drawer.
Take out the whisk.
Close the drawer.
Gently whisk the eggs in the glass bowl.
Open the cupboard door.
Take a frying pan out of the cupboard.
Close the cupboard door.
Set the frying pan on the stove.
Turn on the stove.
Pick up the glass bowl.
Pour the eggs from the glass bowl to the frying pan.
and so on. This may seem like overkill, but imagine how things could go wrong if we skipped Step 13 or omitted the word “gently” from Step 10.
Computer programming consists of design, deciding on the steps to be performed, and implementation, translating those steps into computer instructions in a programming language. Design and implementation both determine the effectiveness and efficiency of a program. For example, could the above steps be reordered or simplified to reduce redundancy?
A Word About AI¶
Experience with generative artificial intelligence (GenAI) such as ChatGPT and other large language models (LLMs) can give the impression that computers actually can think for themselves or, on the other hand, that computers can fail to follow instructions. However, prompting an LLM is not the same as writing a program, and an LLM runs inside software that follows computational rules. A typical LLM has the following workflow:
Read the input prompt provided by a human user.
Translate the prompt into a mathematical format.
Feed the formatted prompt into a mathematical model.
Translate the output of the mathematical model into text.
Display the formatted output to the human user.
The mathematical model in Step 3 consists of a series of elementary mathematical operations. The result may appear intelligent, but it is still produced by a computer following a fixed set of computational rules.
Summary
Computers are made of physical hardware and intangible software.
Software refers to instructions written in programming languages, which have specific syntax (grammar) to make coding statements precise and unambiguous.
The task of computer programming is twofold: determining the steps for the computer to take (design), and translating those steps correctly into computer code (implementation).
Computers are perfectly obedient: they carry out instructions exactly as they are specified. If a program behaves incorrectly, the only recourse is to correct the software.