COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-002 / Working with instructions◷ 14 min read + practice

Output and expressions

An expression produces a value. An instruction can do something with that value. Learning the difference helps you understand why code can run successfully while the output area remains empty.

By the end, you can: Use print(), separate output values and distinguish an expression from visible output.
Before this lesson: PY-001

Expressions make values

The expression 8 + 4 evaluates to 12. In the interactive Python prompt you often see that value displayed automatically. In a normal .py script, merely evaluating 8 + 4 does not print anything.

Use print(8 + 4) to send the calculated value to the output area. This is why the CF playground may display “No printed output” even when your code ran without an error.

Separate arguments

print() accepts more than one value separated by commas. By default, Python places a space between values and ends with a newline. These defaults are adjustable using the sep and end keyword arguments.

The output is not the program itself: you can change what the user sees without changing how you calculate the value.

Comments document intent

A # symbol starts a comment outside a string. Python ignores the rest of that line. Comments explain why a step exists or clarify a surprising decision; they are not a replacement for readable code.

Try removing a print() call while leaving the calculation. Observe that no visible result appears.

Try it yourself.

Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Expression versus output

Only the second expression is printed.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

Make both expressions visible, each on its own line.

Need a hint?

Wrap the first expression in print(...).

On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.

Check your understanding.

Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.

  1. Why might a line containing only 8 + 4 produce no output in a script?

    View answer

    In a normal .py script, writing 8 + 4 computes the expression but does not display it. print(8 + 4) sends 12 to standard output. An interactive Python prompt may automatically echo expression results; a script does not.

  2. What does the sep keyword change?

    View answer

    sep supplies the separator placed *between multiple arguments* passed to print(). By default it is a space: print("A", "B", sep="-") prints A-B. It does not alter the values themselves.

Where this leads.

Output becomes more interesting once we can name and reuse values.