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:
Get two eggs.
Crack the eggs.
Mix them.
Cook them.
A computer program follows the same basic idea:
Get some information.
Process the information.
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:
Python
Java
C++
JavaScript
C#
Go
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.
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
Get bread.
Put bread in toaster.
Start toaster.
Wait.
Remove toast.
Example — Finding the Largest Number
Suppose we have:
5, 12, 7
One possible algorithm is:
Start with 5 as the largest.
Compare 12 with 5.
12 is larger, so largest becomes 12.
Compare 7 with 12.
7 is not larger.
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:
Syntax: Did I write the code correctly?
Logic: Did I tell the computer to do the correct thing?
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.