Sequential Execution

count = 0                # Step 1
print(count)             # Step 2
count = count + 5        # Step 3
count = count + 2        # Step 4
print(count)             # Step 5

Programs execute line by line, one step at a time. Many times, the programs run super-fast and you won’t see this behavior. We have some tools that let us slow down and follow the execution. But even when we use these tools, programs still only run one line at a time.

Metaphor

A picture of a person reading a book, with an arrow pointing down the page.

Think about reading a book or acting out a script, how we move down a page. This is the same way a program is read: from top to bottom, line by line.

Time

A stopwatch is next to some lines of code. Below the lines of code, annotations indicate how long the code took to run for each line.

The result of this step-by-step instruction is that programs are executed over time. Although execution happens very, very fast, it does not happen all at once. Time is a crucial component in running programs.

Statements

# Assignment statement
count = 5

# Print statement
print(count)

A single line of code is known as a statement. A program is really a collection of statements. We call this collection the “body” of a program. There are many kinds of statements, but we only know about a few so far.

Expressions

An expression is any kind of combination of literal values, math operators, comparison operators, Boolean operators, and variables. Any value is an expression. Any variable is an expression. Expressions can be made up of expressions.

Connecting Statements and Expressions

Statements and expressions fit together. Statements are the steps of a program: they are stacked one after another, from top to bottom. An expression usually isn’t a step on its own. Instead, it sits inside a statement and provides the value that statement needs. In count = count + 5, the whole line is an assignment statement, and count + 5 is the expression that calculates the new value. In print(count), the expression count provides the value to print. Statements can have expressions inside, and expressions can have other expressions inside. However, expressions cannot have statements inside of them.

Order is Important

A picture of a confused person looking at a series of book chapters out of order: "Chapter 15", "Chapter 1", "Prologue", "Chapter 5".

Since programs run from top to bottom, the order of statements is important. If you rearranged the sentences of a book at random, they wouldn’t make much sense - the same is true for programs. Think very carefully about the order of the statements in your program!

Summary