← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 1 · Chapter

Chapter 1 — Introduction to Computing & Computational Thinking

Introduction to Computer Science Free sample edition

The module's primary reading · ~35–45 minutes · Objective 1 (explain what a program and an algorithm are and how a Python program runs, write and run short programs, trace them line by line to predict their output, and read an error message to tell a syntax error from a runtime exception) · Read it first this week; the lecture, the slides, the tutorial, the quiz, and the Coding Lab all build on it.


In plain language

A computer is a machine that follows instructions exactly as they are written, one after another. This week you write those instructions yourself, starting with an algorithm (a list of steps that solves a problem) and turning it into a program (those steps written in a language a computer can run). You will run your first lines of Python, predict what each one prints, and then check your prediction by running it. You will also learn to read an error message (the report Python prints when it cannot go on) from its last line, so a red message turns into a clue instead of a verdict. No experience is assumed; you need a web browser and some curiosity.

Why you already care about this

Picture a friend following your recipe for pancakes. Your card says "add 2 cups of flour, then the milk, and make 8 pancakes." Your friend adds the flour, then pours in the whole 1-liter carton of milk, because the card never said how much. The pancakes are soup. Your friend did exactly what you wrote, not what you meant.

A computer is that friend, every time. It never guesses and never fills a gap with common sense. That sounds like a weakness, but it is the source of a computer's power: a list of exact steps runs the same way on the first try and on the thousandth. Your phone's step counter, the receipt from a café, and the reading tracker on a library app all work because someone wrote steps a machine could follow literally.

This chapter teaches you to write those steps, run them, and check what really happened. You will meet a small reading-log program that grows through the whole course, starting this week with a single line.

By the end of this chapter you will be able to:

  • write a short algorithm in plain English and trace it by hand;
  • break a task into small, ordered steps a machine can follow;
  • write and run a one-line Python program with print, including several comma-separated arguments;
  • predict what a short expression prints, then confirm the prediction by running it;
  • read an error message from its last line and tell a SyntaxError from a NameError;
  • name the people and dates behind programming and Python, and use the course's tools to check a chatbot's claim about code.

Carry this line with you through the whole course: the computer does exactly what you wrote, not what you meant.

Where we are

  • This week: what an algorithm and a program are, breaking a task into steps, your first Python program, tracing short expressions, reading two kinds of error, and the tools you will use all term.
  • How they connect: this chapter is the foundation, so there is no earlier week to lean on; every later week asks you to write code, predict what it prints, and run it to check.
  • Where we're going next: Week 2, Variables, Data Types & Expressions, gives values names, explains why 10 / 2 prints a decimal, and adds more operators.

This is the first week of the course, so nothing earlier is assumed. You do not need to install anything. You need a browser, a free online Python editor, and the habit you will build here: predict, then run. Next week leans hardest on Section 4, where you trace expressions, so spend extra time there.

1. Algorithms and programs: steps a machine can follow

Suppose a friend texts you two prices for the same bus pass, 3 dollars at one window and 7 dollars at another, and asks which is higher. You glance and answer in a second. A machine cannot glance. It needs the steps spelled out.

Here is the idea in plain words. A recipe, a set of directions to a classroom, and the steps for finding the bigger of two numbers all share one shape: a short list of instructions that anyone can follow and get the same result. When the list works for every case, not only for 3 and 7, it is an algorithm.

An algorithm is a finite, unambiguous list of steps that solves a whole class of problems, such as finding the larger of any two numbers rather than of one particular pair.

A program is an algorithm written in a language a computer can run; the computer carries out its instructions literally and in order.

So the algorithm comes first and lives in plain English. The program is the same algorithm translated into a language such as Python.

Worked example — the larger of two numbers. Here is an algorithm in plain English, with no code at all.

  1. Look at the first number and the second number.
  2. Compare them: is the first number bigger than the second?
  3. If it is, report the first number.
  4. Otherwise, report the second number.

Now trace it by hand on the numbers 3 and 7. Step 1: the first number is 3 and the second is 7. Step 2: is 3 bigger than 7? No. Step 3 does not apply. Step 4: report the second number, 7.

In words: for 3 and 7, the algorithm reports 7, the larger number. Notice what happens with a tie, such as 9 and 9. The first number is not bigger, so step 4 reports the second number, 9, which is still correct. A good algorithm covers every case, including the dull ones.

Misconception check. Students often think a computer "understands" what they mean, so a vague step is fine. That is tempting, because people fill gaps for each other all day. But a computer executes what you wrote. If step 2 said "pick the nicer number," no machine could follow it. Every step must be exact enough that two strangers would carry it out the same way.

Pause and choose one of these to think about before you read on:

  • (a) Think of a set of directions someone once gave you that only worked because you already knew the place. Which step would a machine have gotten stuck on?
  • (b) Think of a routine you do the same way every time, such as locking up or packing a bag. Which step would you need to spell out more exactly for a stranger?

Memory hook: an algorithm is the plan; a program is the plan made runnable.

2. Decomposition: break a big task into small steps

A reading log is a short record of what you read each day. Say you want a computer to make the first entry in yours. "Make a log entry" is too big for one step. A machine needs it broken down.

Programmers call this way of working computational thinking: approaching a problem the way a programmer does, by breaking it into small steps a machine can follow literally and in order, then checking each step.

The core move is decomposition, which means splitting one large task into smaller steps, each simple enough to carry out and check on its own, and putting them back in order.

Worked example — decomposing the first log entry. The big task is "make the first entry in a reading log." Broken down, it becomes:

  1. Decide what the entry should say: the words "Reading log" and the day number, 1.
  2. Decide exactly how the line of output should look: Reading log: day 1.
  3. Write one instruction that shows that line.
  4. Run the instruction.
  5. Compare what appeared with what you planned in step 2.

Each step is small enough to check. If step 5 shows a difference, you know to look back at step 3. Section 3 writes the instruction for step 3.

Order matters as much as the steps. Python runs a script from the top line to the bottom line, one line at a time, and it never skips ahead or goes back unless you tell it to. Here is a three-line program from the reading log.

print("Reading log: day 1")
print("Goal:", 300, "pages")
print("Pages so far:", 45)

Output

Reading log: day 1
Goal: 300 pages
Pages so far: 45

The lines appear in exactly the order they were written. The reading log's goal is 300 pages and you have read 45 so far; you will meet those two numbers again next week.

Misconception check. Students often think the computer reads a whole program at once and does the important parts first. It does not. Python runs line 1, then line 2, then line 3. If you want the goal printed first, it has to be on the first line.

Memory hook: big task, small steps, right order.

3. Your first program: edit, run, read the output, fix

Think about texting a photo to a friend. You pick it, check it, send it, and look at what arrived. If the wrong photo went, you fix it and send again. Programming works the same way.

That working cycle is the edit, run, read the output, fix loop: the working cycle of programming. You change the code, run it, compare what it printed with what you predicted, and repair any difference before running again. You will go around this loop many times in this course, and each pass is quick.

Your first program uses print, Python's built-in function that displays its arguments as one line of output, with one space between each pair, and then ends the line. An argument is a value placed inside the parentheses of a function call, such as each comma-separated piece handed to print. Text inside quotation marks is a string, a piece of text inside quotation marks, which Python keeps exactly as typed instead of reading it as a number or a name.

Worked example — the reading log's first line. Here is the whole program.

print("Reading log: day 1")

Output

Reading log: day 1

In words: the program printed the string exactly as typed, without the quotation marks. The quotation marks tell Python where the text starts and stops; they are not part of the output, which is exactly what a program prints when it runs, shown in its own block under the label Output.

Now give print several arguments, separated by commas.

print("Goal:", 300, "pages")

Output

Goal: 300 pages

The three arguments are the string "Goal:", the number 300, and the string "pages". print showed them in order and put exactly one space between each pair. You did not type those spaces; print added them.

Here is the same idea in a different setting, a phone's step counter.

print("Steps walked:", 3200, "before lunch")

Output

Steps walked: 3200 before lunch

Misconception check. Students often think the commas will show up in the output, or that the spaces have to be typed inside the quotes. Neither is true. A comma inside the parentheses separates arguments, and print turns each separation into a single space. If you want a comma in the output, it has to be inside a string.

In practice (your tools). Open a free browser-based Python runner, an online editor that runs Python 3 with no account and nothing to install. Delete any sample code so the editor is blank. Type print("Reading log: day 1") exactly, including both quotation marks and both parentheses. Press the Run button. The output appears in a separate panel, usually below or beside the code. Compare it with what you predicted, character by character. If it differs, edit and run again: that is the loop.

Memory hook: edit, run, read, fix, and go around again.

4. Predict, then run: tracing your first expressions

Before you run a line, say out loud what you expect it to print. Then run it. Programmers call this a trace: to read a program one line at a time and write down what each step does, so you can predict its output before you run it. The gap between your prediction and the real output is where the learning happens.

An expression with two operators raises a question: which one goes first? Python follows operator precedence, the rule that decides which operator Python works out first when an expression has several, such as multiplication before addition, with parentheses always going first.

Worked example — four traces. Predict each line, then check the output.

print(2 + 3 * 4)
print((2 + 3) * 4)
print("2" + "3")
print(2 + 3)

Output

14
20
23
5

Here is the reasoning, line by line.

  1. 2 + 3 * 4: the * goes first, so 3 × 4 = 12, then 2 + 12 = 14. Python printed 14.
  2. (2 + 3) * 4: the parentheses go first, so 2 + 3 = 5, then 5 × 4 = 20. Python printed 20.
  3. "2" + "3": these are two strings, not two numbers, so + joins them end to end. Python printed 23, the characters 2 and 3 side by side.
  4. 2 + 3: these are two numbers, so + adds them. Python printed 5.

In words: the same symbol, +, adds numbers and joins strings, and the quotation marks decide which job it does. That one fact explains a large share of the surprises in a first programming course.

The precedence rule matters outside the classroom too. Say a café receipt lists one sandwich at 4 dollars and two drinks at 3 dollars each (an illustrative example). The right total is 4 + 2 * 3, which prints 10. If you add parentheses in the wrong place, (4 + 2) * 3, Python prints 18. Both lines run without any error, and only one matches the receipt: 18 dollars versus 10 dollars, a total 1.8 times too high. A cashier who trusted the wrong line would overcharge every customer, so check the grouping before you rely on a total.

Misconception check. Students often predict that 2 + 3 * 4 is 20, reading left to right as they would a sentence. That is tempting, but Python multiplies before it adds, so the result is 14. When you are unsure, add parentheses that say what you mean, and run both versions to see the difference.

One more line, as a preview. Run print(10 / 2) and Python prints 5.0, not 5. That decimal point is not a mistake. Week 2 explains why / always gives a decimal number; for now, notice that predicting and running caught a detail you might have missed.

In practice (your tools). Type all four lines from the worked example into your browser runner and run them together. Then change one line, run again, and see which output changed. Python Tutor at pythontutor.com runs the same lines one step at a time; paste a line there, click the button to visualize it, and step forward to watch what Python does.

Memory hook: predict, then run; the run is the referee.

5. Reading an error message: start at the last line

When a dishwasher stops mid-cycle, the little screen shows a code. You do not panic; you look up the code. An error message works the same way. An error message is the report Python prints when a program cannot go on; you read it from the bottom up, because its last line names the error type.

Errors come in different kinds, and the two you meet this week behave differently.

The first kind comes from the parser, the part of Python that reads a program's text and checks that it follows the language's rules before any line runs. A SyntaxError is the error type Python reports when the parser refuses a program's text, so no line of that program runs at all.

The second kind happens while the program is running. A NameError is the error type Python reports when a running program uses a name Python does not know, such as Print with a capital P or a word that needed quotation marks. Python is case-sensitive, treating capital and lowercase letters as different characters, so that to Python print and Print are two different names.

Worked example — two broken programs. Each program has two lines. Predict what each prints before reading the output.

The first program has a capital P on line 2.

print("Reading log: day 1")
Print("Goal: 300 pages")

Output

Reading log: day 1
NameError

Line 1 ran and printed its string. Then line 2 tried to use the name Print, which Python does not know, so the program stopped. The last line of the error message names NameError.

The second program is missing a closing parenthesis on line 2.

print("Reading log: day 1")
print("Goal: 300 pages"

Output

SyntaxError

This time nothing printed, not even line 1. The parser read the whole text first, found that the parentheses did not match, and refused to run any of it. The last line of the error message names SyntaxError.

Think back to the pancake recipe: your friend stopped at the step that made no sense, and Python stops the same way. In words: if some lines printed before the error, the program was running, and the error happened along the way. If nothing printed at all, the parser rejected the text before it began. The real message has more lines and some extra wording; you will learn to read those later, but the type on the last line is the part to find first.

Caveat: this week you meet only two error types. Python has many more, and later weeks add them one at a time, each read the same way, from the last line.

A word that needed quotation marks causes the same NameError. print(Reading) makes Python look for a name called Reading, find nothing, and stop; print("Reading") prints the word.

Misconception check. Students often think a red error message means they failed. It does not. An error is a map to the bug: the last line says what kind of problem it is, and the rest points toward where. Programmers at every level read error messages many times a day.

In practice (your tools). Type the first broken program into your browser runner and run it. Scroll to the bottom of the error panel and find the last line; it starts with the error type. Fix the capital P, run again, and confirm both lines print. Then try the second program and fix the missing parenthesis the same way.

Memory hook: read the last line first; the type is the clue.

6. Where it came from, and the tools you will use

Every phone, laptop, and bus-pass reader runs on ideas that are older than you might guess. A short history shows that writing exact steps for a machine is a long human project, not a recent trick.

In 1843, Ada Lovelace published notes on a mechanical calculating machine called the Analytical Engine. Her notes include the first published description of a stepwise sequence of operations for a machine to carry out, which is why Lovelace is often called the first programmer.

In 1936, Alan Turing published a paper describing an abstract machine, now called a Turing machine, and showed that some well-posed problems cannot be decided by any algorithm at all. Turing's result is a limit: there are questions no program can always answer.

In 1952, Grace Hopper developed an early compiler, a program that translates code people write into instructions a machine can carry out. Hopper's work is part of why you can write print instead of long strings of machine instructions.

In 1947, engineers working on the Mark II computer found a moth stuck in one of its relays and taped it into their log book. That log page survives in a national museum's collection. The word "bug" for a fault in a machine is older than the moth by decades, so the moth made the word famous rather than inventing it.

Python itself has dates too. Guido van Rossum released Python to the public in 1991. Python 3.0 arrived in 2008, and Python 2 stopped being supported in 2020. This course uses Python 3 only.

Now the tools. This course runs on four free tools, all in the browser:

  • a free browser-based Python runner, an online editor that runs Python 3 with no account and nothing to install;
  • Python Tutor at pythontutor.com, which runs a program step by step and draws what happens at each step;
  • the official Python documentation at docs.python.org, where every fact about the language is written down;
  • one approved chatbot, used as a pair-programmer whose code and claims you run before you trust them.

Worked example — checking a chatbot's claim. Suppose you ask a chatbot what print(2 + 3 * 4) displays, and it answers 20, explaining that Python works from left to right. That is a confident claim. Your job is to check it, not to believe it. Type the line into your browser runner and run it.

print(2 + 3 * 4)

Output

14

The run printed 14, not 20, because * goes before +. The chatbot's explanation contradicts what the code prints. You caught the slip by running the code, and that habit is the one this course grades.

Misconception check. Students often think a chatbot's answer about code is reliable because it sounds sure of itself. A chatbot predicts likely text; it does not run your program. Treat every claim it makes about what code prints as a guess to test. The run decides.

In practice (your tools). Ask your approved chatbot to predict what print(10 / 2) displays. Write its answer down. Then run the line in your browser runner and compare. If the two differ, note which one was right and why; if they match, note that you confirmed it by a run rather than by trust. When you want the official word on a rule, search docs.python.org for the topic.

Honesty note: these history notes are short on purpose. Each person named here did far more than one sentence can hold, and the readings page offers places to go deeper.

Memory hook: the chatbot proposes; the run decides.

Key terms

  • algorithm — A finite, unambiguous list of steps that solves a whole class of problems, such as finding the larger of any two numbers rather than of one particular pair. (first used: 1. Algorithms and programs: steps a machine can follow)
  • program — An algorithm written in a language a computer can run; the computer carries out its instructions literally and in order. (first used: 1. Algorithms and programs: steps a machine can follow)
  • computational thinking — Approaching a problem the way a programmer does: breaking it into small steps a machine can follow literally and in order, then checking each step. (first used: 2. Decomposition: break a big task into small steps)
  • decomposition — Splitting one large task into smaller steps, each simple enough to carry out and check on its own, and putting them back in order. (first used: 2. Decomposition: break a big task into small steps)
  • edit, run, read the output, fix loop — The working cycle of programming: change the code, run it, compare what it printed with what you predicted, and repair any difference before running again. (first used: 3. Your first program: edit, run, read the output, fix)
  • print — Python's built-in function that displays its arguments as one line of output, with one space between each pair, and then ends the line. (first used: 3. Your first program: edit, run, read the output, fix)
  • argument — A value placed inside the parentheses of a function call, such as each comma-separated piece handed to print. (first used: 3. Your first program: edit, run, read the output, fix)
  • string — A piece of text inside quotation marks, which Python keeps exactly as typed instead of reading it as a number or a name. (first used: 3. Your first program: edit, run, read the output, fix)
  • output — Exactly what a program prints when it runs, shown in its own block under the label Output. (first used: 3. Your first program: edit, run, read the output, fix)
  • trace — To read a program one line at a time and write down what each step does, so you can predict its output before you run it. (first used: 4. Predict, then run: tracing your first expressions)
  • operator precedence — The rule that decides which operator Python works out first when an expression has several, such as multiplication before addition, with parentheses always going first. (first used: 4. Predict, then run: tracing your first expressions)
  • error message — The report Python prints when a program cannot go on; you read it from the bottom up, because its last line names the error type. (first used: 5. Reading an error message: start at the last line)
  • parser — The part of Python that reads a program's text and checks that it follows the language's rules before any line runs. (first used: 5. Reading an error message: start at the last line)
  • SyntaxError — The error type Python reports when the parser refuses a program's text, so no line of that program runs at all. (first used: 5. Reading an error message: start at the last line)
  • NameError — The error type Python reports when a running program uses a name Python does not know, such as Print with a capital P or a word that needed quotation marks. (first used: 5. Reading an error message: start at the last line)
  • case-sensitive — Treating capital and lowercase letters as different characters, so that to Python print and Print are two different names. (first used: 5. Reading an error message: start at the last line)
  • compiler — A program that translates code people write into instructions a machine can carry out. (first used: 6. Where it came from, and the tools you will use)

Summary

  • An algorithm is the plan in plain words; a program is that plan in a language a computer can run.
  • Decomposition breaks a big task into small, checkable steps, and Python runs them from top to bottom.
  • Programming is a loop: edit, run, read the output, fix.
  • print shows its arguments with one space between each pair.
  • Python multiplies before it adds, parentheses go first, and + joins strings but adds numbers.
  • Read an error from its last line: SyntaxError means nothing ran; NameError means an unknown name stopped a running program.
  • A chatbot's claim about code is a guess until a run confirms it.

Check your understanding

Work these before looking at the answers — they are ungraded, and the answers (with reasoning) are at the bottom of the page.

  1. Predict what each line prints, then explain the difference: print(5 + 2 * 3) and print((5 + 2) * 3).
  2. A campus shuttle display runs print("Bus", "7" + "1") and then print("Bus", 7 + 1) (an illustrative example). Predict both lines of output.
  3. A student's step-counter program has two lines, print("Step counter") and then print(steps). What appears when it runs, and which error type does the last line of the error message name?
  4. Write a plain-English algorithm for finding the smaller of two numbers, then trace it on 12 and 5.

Answers (worked reasoning — read after attempting)

  1. The first line prints 11: the * goes first, so 2 × 3 = 6, then 5 + 6 = 11. The second line prints 21: the parentheses go first, so 5 + 2 = 7, then 7 × 3 = 21. Same numbers, same operators, different grouping, different result.
  2. The first line prints Bus 71, because "7" + "1" joins two strings into 71, and print puts one space after Bus. The second line prints Bus 8, because 7 + 1 adds two numbers. The quotation marks decide whether + joins or adds.
  3. Line 1 runs and prints Step counter. Line 2 asks for a name, steps, that the program never created, so the program stops, and the last line of the error message names NameError. Because a line printed first, you know the program was running when the error happened; it was not a SyntaxError.
  4. One version: step 1, look at the first number and the second number; step 2, is the first number smaller than the second?; step 3, if it is, report the first number; step 4, otherwise, report the second number. On 12 and 5: is 12 smaller than 5? No, so step 4 reports the second number, 5. Check a tie too: on 6 and 6, step 4 reports 6, which is correct.

Next up in this module: the Readings & Resources page, the lecture tutorial, the practice set, and the quiz — and next week, variables give your values names, and the rule behind the decimal that / prints finally gets its explanation.

Built with The Course Maker · samples.thecoursemaker.com