0 · Foundations of Programming

What Is Programming?

Before learning Python syntax, you need to understand what programming actually is, how computers execute instructions, and how programmers think about problems.

0.1

What Is Programming?

Programming is the process of creating instructions that a computer can follow to solve a problem or accomplish a task.

Think about a recipe.

A recipe gives a person a sequence of instructions. A computer program does something similar: it gives a computer instructions that it can execute.

1. What Is a Computer Program?

A computer program is a set of instructions that tells a computer what to do.

You will be able to
  • Define a computer program.
  • Explain why computers need instructions.
  • Identify programs you use in everyday life.

Think about a recipe:

  1. Get two eggs.
  2. Crack the eggs.
  3. Mix them.
  4. Cook them.

A computer program follows the same basic idea:

  1. Get some information.
  2. Process the information.
  3. Produce a result.
Example — Calculator
Get 10
Get 5
Add them
Display 15

The instructions tell the computer what information to use, what operation to perform, and what result to display.

Example — Python Program

Here is an actual Python program that performs the same basic idea:

Try It — Calculator
Loading Python...
Output
Python is loading. You will be able to run this example shortly.
Think About It
  • What would happen if the computer skipped the instruction that adds the two numbers?
  • Does a computer know what you want it to do without being given instructions?
  • What programs did you use today?
Important Idea
  • A program is a set of instructions.
  • Computers follow instructions to perform tasks.
  • Programs generally receive information, process it, and produce a result.

2. Programming Languages

Computers ultimately execute very low-level instructions, but humans need a practical way to describe what they want computers to do.

A programming language is a formal language that programmers use to write instructions for computers.

You will be able to
  • Define a programming language.
  • Name several programming languages.
  • Recognize Python as a programming language.

Some programming languages include:

Example — Python
print("Hello, world!")

Python processes this instruction and produces:

Hello, world!
Try It — Python
Loading Python...
Output
Python is loading...
Example — JavaScript
console.log("Hello, world!");

JavaScript accomplishes a similar task using different syntax.

Think About It
  • Why do you think there are many programming languages instead of only one?
  • If two languages can perform the same task, why might a programmer choose one over another?
  • Is Python the computer's native language? Why or why not?
Important Idea
  • Programming languages let humans express computational instructions.
  • Different languages use different syntax and have different strengths.
  • Python is one programming language among many.

3. Source Code vs. Machine Code

When programmers write a program, they normally write it in a programming language that humans can read.

This is called source code.

The processor, however, ultimately executes low-level machine instructions.

This is called machine code.

You will be able to
  • Identify source code.
  • Explain what machine code is.
  • Explain the relationship between source code and machine instructions.
Example — Source Code

A programmer might write:

x = 10
print(x)

This is source code because it is written in Python and is intended to be read and modified by programmers.

Example — Machine Code

At a much lower level, processors execute machine instructions represented as binary data.

10110000 01100001
11001010 00010110
00001111 11010001

The exact machine instructions depend on the processor architecture. The important idea is that machine code is fundamentally different from human-readable Python source code.

Think of translation.

Source code is written in a language designed to be practical for humans. Machine instructions are a much lower-level form that the processor can execute.

Think About It
  • Which would be easier for you to read: Python or binary?
  • Why don't programmers normally write entire applications directly as binary?
  • What must happen between human-readable source code and processor-executable instructions?
Important Idea
  • Source code is written by programmers.
  • Machine code consists of low-level instructions executed by a processor.
  • A system must process or translate source code so the computer can execute it.

4. Interpreters vs. Compilers

We now have an important question:

If programmers write human-readable source code, how does the computer actually execute it?

Two important approaches are associated with this process: interpreters and compilers.

You will be able to
  • Explain the basic idea of an interpreter.
  • Explain the basic idea of a compiler.
  • Describe the difference between the two approaches.
Interpreter — Basic Idea
Source Code
     ↓
Interpreter
     ↓
Execution

An interpreter is a program that processes source code and helps execute it.

Compiler — Basic Idea
Source Code
     ↓
Compiler
     ↓
Translated Program
     ↓
Execution

A compiler translates source code into another form before the resulting program is executed.

Important beginner idea:

The interpreter-versus-compiler distinction is useful for understanding the basic concept, but real programming-language implementations can be more complicated than this simple model.

For now, remember:

Interpretation emphasizes processing code for execution, while compilation emphasizes translating code before execution.

Think About It
  • Why might translating a program ahead of time be useful?
  • Why might processing code interactively be useful?
  • When might you want to discover a problem before running the final program?
Important Idea
  • An interpreter processes source code for execution.
  • A compiler translates source code into another form.
  • Real language implementations can combine multiple techniques.

5. What Happens When You Run Python?

Let's make the previous ideas concrete.

Suppose you write:

x = 10
y = 20
print(x + y)

What happens after you run it?

You will be able to
  • Describe the general stages involved when Python executes code.
  • Trace a simple Python program step by step.
  • Explain how Python processes source code to produce a result.
Step 1 — You Write Source Code
x = 10
y = 20
print(x + y)
Step 2 — Python Processes the Program

Python reads and analyzes the source code. It determines what the statements mean and prepares them for execution.

Step 3 — Python Executes the Instructions

The first instruction creates a name called x and associates it with the value 10.

The second instruction creates y and associates it with 20.

The third instruction calculates:

10 + 20

The result is:

30
Step 4 — The Result Is Displayed
30
Try It — Trace the Program
Loading Python...
Output
Python is loading...
Predict Before You Run

Change x to 100 and y to 50.

Before clicking Run, predict the output.

Then run the program and compare your prediction with the result.

Important Idea
  • You write Python source code.
  • Python processes the source code.
  • Python executes the instructions.
  • The computer produces the requested result.

6. Algorithms and Instructions

A program contains instructions, but before writing those instructions, programmers need to figure out what steps will solve the problem.

An algorithm is a sequence of steps used to solve a problem.

You will be able to
  • Explain what an algorithm is.
  • Create simple step-by-step instructions.
  • Apply an algorithm to a simple problem.
Example — Making Toast
  1. Get bread.
  2. Put bread in toaster.
  3. Start toaster.
  4. Wait.
  5. Remove toast.
Example — Finding the Largest Number

Suppose we have:

5, 12, 7

One possible algorithm is:

  1. Start with 5 as the largest.
  2. Compare 12 with 5.
  3. 12 is larger, so largest becomes 12.
  4. Compare 7 with 12.
  5. 7 is not larger.
  6. The answer is 12.
Try It — Algorithm in Python
Loading Python...
Output
Python is loading...
Think Like a Programmer
  • What happens if an algorithm leaves out an important step?
  • Can two different algorithms solve the same problem?
  • Which approach might be better if you had one million numbers?
Important Idea
  • An algorithm is a sequence of steps for solving a problem.
  • Algorithms exist before programming languages.
  • A good programmer thinks about the solution before worrying about syntax.

7. Syntax vs. Logic

Beginners often think that if Python accepts their code, the program must be correct.

That is not true.

We need to ask two different questions:

You will be able to
  • Distinguish syntax errors from logic errors.
  • Identify examples of each type of problem.
  • Explain why code can run and still be wrong.
Example — Syntax Error
print("Hello"

The closing parenthesis is missing. Python cannot correctly understand the statement.

Try the Syntax Error
Loading Python...
Output
Python is loading...
Example — Logic Error
price = 100
discount = 10

final_price = price + discount

print(final_price)

The program may run successfully. But if the intention is to subtract the discount, the logic is wrong.

The correct calculation is:

final_price = price - discount
Find the Logic Error
Loading Python...
Output
Python is loading...
Critical Thinking

If a program runs without producing an error message, does that prove the program is correct?

Explain your answer.

Important Idea
  • Syntax determines whether code is written correctly.
  • Logic determines whether the program does what we intended.
  • A program can have correct syntax but incorrect logic.

8. Bugs and Debugging

Programs rarely work perfectly the first time.

A bug is a problem in a program.

Debugging is the process of finding and fixing problems in a program.

You will be able to
  • Identify a bug.
  • Explain why a bug happened.
  • Modify code to fix a bug.
  • Test the program again after making a change.
Example — Debugging a Condition
number = 10

if number > 10:
    print("Number is 10 or greater")

The program runs, but nothing is displayed.

Why?

The condition asks whether 10 is greater than 10. It isn't.

If we want to include 10, we need:

if number >= 10:
    print("Number is 10 or greater")
Debug It
Loading Python...
Output
Python is loading...
Think Like a Debugger
  1. What did you expect the program to do?
  2. What actually happened?
  3. Where might the difference come from?
  4. What change could fix it?
  5. Does the program work after the change?
Important Idea
  • Bugs are normal parts of programming.
  • Debugging is a reasoning process, not just fixing typing mistakes.
  • Good programmers compare expected behavior with actual behavior.

Exit Ticket

Before moving on, answer these questions without looking back at the examples.

  1. In your own words, what is a computer program?
  2. What is the difference between source code and machine code?
  3. What is the basic difference between an interpreter and a compiler?
  4. Describe what generally happens when Python runs a program.
  5. What is an algorithm?
  6. What is the difference between a syntax error and a logic error?
  7. A program runs successfully but produces the wrong answer. Is that necessarily a syntax problem? Explain.
  8. Why is debugging more than simply fixing spelling mistakes?