Concepts / Using the print() Function

Using the print() Function

A print() statement displays a result.

  • 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.

Statement 1runs firstStatement 2runs nextStatement 3runs after statement 2finishes beforefinishes before
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.

score = 5stores a valuescorevalue 5print(score)displays a result5visible outputstoresdisplays
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.

Code walkthrough
message = "Ready"
print(message)
print("Go")
  1. 1
    The first statement stores the value Ready in message. It does not display output.
  2. 2
    The second statement displays the value stored in message, so the first visible line is Ready.
  3. 3
    The third statement displays Go, so the second visible line is Go.
Output
Ready
Go
message = Readystores; no outputprint(message)outputs Readyprint(Go)outputs GoReady, then Govisible outputnextnextproduces
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

  • Common mistake

    Treating every statement as visible output.

    What it looks like
    name = "Ava"
    Why it fails

    An assignment stores a value but does not produce output.

    Do this instead

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

  • Common mistake

    Reading the script as if all statements run at once.

    What it looks like
    value = 1
    print(value)
    value = 2
    print(value)
    Why it fails

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

    Do this instead

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

  • Common mistake

    Ignoring a later assignment when predicting a later display.

    What it looks like
    level = 1
    level = 2
    print(level)
    Why it fails

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

    Do this instead

    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.
Worked example

A complete trace

Determine the visible output of the practice script.

  1. 1
    Statement 1

    first stores Start. There is no visible output.

  2. 2
    Statement 2

    print(first) displays Start.

  3. 3
    Statement 3

    second stores Finish. There is no visible output.

  4. 4
    Statement 4

    print(second) displays Finish.

Result

The visible output is Start, followed by Finish.

Key Takeaways

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

At a glance

  • 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.

More concepts starting with U