A statement (also called a proposition) is a declarative sentence with a definite truth value: it is either True or False.
Three examples of statements:
Three examples of non-statements:
x is assigned.The distinction matters. Logic, and therefore programming, operates only on things that can be evaluated. Questions and commands cannot be evaluated as true or false. Open statements defer evaluation until a value is substituted.
Imagine a robot that can only respond to sentences by evaluating them
as True or False. Hand it a command and it
stalls. Hand it a question and it stalls. Hand it a statement and it
evaluates immediately.
A programming language interpreter is that robot. The expression
len(items) > 0 is a statement. The interpreter evaluates
it. It produces True or False. It does not ask
“what do you mean by items?” — that would be an error, not a hesitation.
Either items is in scope or the program crashes.
This robot model exposes something important: statements are
contracts. When you write if user.is_active:, you
are telling the interpreter that user.is_active will
resolve to a truth value. If it resolves to None in a
language without implicit boolean coercion, the contract is violated.
The robot stalls.
An open statement contains one or more free variables. It is not a statement until those variables are bound to values.
x > 10
This is not True or False. It is a predicate — a
function from a domain to {True, False}. When you substitute
x = 15, the predicate becomes the statement “15 > 10”,
which is True. When you substitute x = 3, it becomes “3
> 10”, which is False.
Open statements are covered in depth in Chapter 5. For now, the key distinction is: a statement has a fixed truth value; a predicate produces a truth value when given inputs.