Programming Search⌘K

Featureintro

Expression statements

Also called Python expression statement, procedure call statement, bare expression, statement context.

An expression statement is an expression on a line of its own, such as a function call. Python evaluates it and discards the value; in interactive mode a value other than None is printed.

Language
Python

Synopsis

def greet(name):
    print("hi", name)

greet("ana")
len("ignored")
[1, 2, 3].append(4)
"just a string"
...
Output
hi ana

Five expression statements; only the first one prints, because it calls print.

Semantics

r1. Expression statements are used "(mostly interactively) to compute and write a value, or (usually) to call a procedure", a function that returns no meaningful result; Python procedures return None. Other uses are allowed.1 Example: expr-stmts.

r2. An expression statement evaluates the expression list, which may be a single expression, and discards the result.1 Any expression is allowed, so a bare name or comparison is legal and does nothing visible. Example: compare-not-assign.

r3. In interactive mode, a value that is not None is converted to a string with repr() and written to standard output on a line of its own; None is not written, so procedure calls print nothing extra.1 The 'single' mode of compile() gives the same behavior: there, expression statements whose value is not None are printed.2 Example: single-mode.

r4. A string literal that is the first expression in a class, function or module is a docstring. It is ignored when the suite runs, but the compiler puts it in the __doc__ attribute.3 Example: docstring-expr.

Examples

The same expression statement prints its value in 'single' mode and nothing in 'exec' mode (rule r3).

for mode in ["single", "exec"]:
    print("mode", mode)
    exec(compile("40 + 2\n", "<demo>", mode))
    exec(compile("None\n", "<demo>", mode))
Output
mode single
42
mode exec

A docstring is an expression statement too (rule r4).

def area(r):
    "Return the area of a circle of radius r."
    return 3.14159 * r * r

print(area.__doc__)
Output
Return the area of a circle of radius r.

Pitfalls

Forgetting the parentheses

A function name on its own is a valid expression statement (rule r2): it looks the function up and does not call it.

This code runs but gives a wrong answer on purpose.

def save():
    print("saved")

save
print("done")
Output
done

== where = was meant

count == 1 compares and throws the result away (rule r2), so count keeps its old value.

This code runs but gives a wrong answer on purpose.

count = 0
count == 1
print(count)
Output
0

Comparison

Go allows only function and method calls and receive operations "in statement context", and even some built-in functions such as len are not permitted there.4 So the Go compiler rejects the two pitfalls above, while Python runs them (rule r2).

References

  1. The Python Language Reference, Python 3.15, 7.1 Expression statements
  2. Built-in Functions, Python 3.15 documentation, compile()
  3. Glossary, Python 3.15 documentation, docstring
  4. The Go Programming Language Specification, Expression statements

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source