Concepts / Using the print() Function

Using the print() Function

A script executes statements sequentially, from top to bottom, with each statement completing before the next begins.

  • Programming

A Script Has an Order

A script is not treated as one undifferentiated action. Python runs its statements sequentially, from top to bottom. Each statement completes before the next statement begins. This order matters because an earlier statement can store a value that a later statement uses or displays.

finishes beforefinishes beforeStatement 1runs firstStatement 2runs nextStatement 3runs after statement 2
What happens next after each statement finishes?

Storing Versus Displaying

Two statements can have very different visible effects. An assignment statement stores a value in memory, but it produces no output on the screen. A print() statement displays a result. Therefore, a script may perform an action without showing anything, and then display a value later when a print() statement runs.

storesdisplaysscore = 5stores a valuescorevalue 5print(score)displays a result5visible output
Which statements show something on the screen, and which statements only store a value for later use?
Statement roleWhat happensVisible output
AssignmentA value is stored for later use.No
print()A result is displayed.Yes

The key distinction when tracing a script

Tracing a Short Script

To predict a script's output, inspect one statement at a time. For each line, ask two questions: What does this statement do, and does it produce output? Also note whether a variable receives a value that can be used later.

message = "Ready" print(message) print("Go")

nextnextproducesmessage = Readystores; no outputprint(message)outputs Readyprint(Go)outputs GoReady, then Govisible output
What output appears after each statement runs from top to bottom?

Predict Before Reading the Output

What do you think happens?

What visible output is produced by this script? count = 2 print(count) count = 3 print(count)

  • 2, then 3
  • 3, then 3
  • No output
Reveal answer

Answer: 2, then 3

The first assignment stores 2 without displaying it. The first print() displays 2. The next assignment stores 3, and the final print() displays the updated value 3. The statements are traced in their top-to-bottom order.

This trace shows why execution order and stored values must be considered together. A value assigned earlier remains available for later use, but a later assignment can change what a subsequent print() statement displays. The assignment itself remains invisible; only the print() statements create visible output.

Mistakes in Output Tracing

  • Treating every statement as visible output.

    An assignment stores a value but does not produce output.

    Fix: Count a line as visible output only when it is a print() statement.

  • Reading the script as if all statements run at once.

    Statements execute from top to bottom, with each statement completing before the next begins.

    Fix: Trace each line in order and record the value available at that point.

  • Ignoring a later assignment when predicting a later display.

    The value available to the final print() is the value stored by the later assignment.

    Fix: When tracing, update your notes whenever an assignment stores a value.

Practice the Line-by-Line Method

EASY

Trace this script from top to bottom. Write down which statements produce output and what appears on the screen, in order. first = "Start" print(first) second = "Finish" print(second)

Hints
  • The first and third statements are assignments.
  • The second and fourth statements are print() statements.
  • The value assigned to first is displayed before the value assigned to second.

A complete trace

Determine the visible output of the practice script.

Statement 1: first stores Start. There is no visible output.

Statement 2: print(first) displays Start.

Statement 3: second stores Finish. There is no visible output.

Statement 4: print(second) displays Finish.

The visible output is Start, followed by Finish.

Key Takeaways

  1. Python executes statements sequentially from top to bottom.
  2. Each statement completes before the next statement begins.
  3. Assignment statements store values but produce no visible output.
  4. print() statements display results.
  5. To predict output, trace every statement in order and track which values are available at each point.

Key Takeaways

  • A script executes one statement after another, from top to bottom.
  • Assignments store values without displaying them.
  • print() displays a result when its statement runs.
  • Tracing means recording each statement's action, visible output, and effect on available values.