A statement in Python: the basic unit of a program

A statement is the basic unit of a Python program: an action to carry out, written on one line.
5 min read
Believemy logo

Definition

Open any Python file and you will see a string of lines, each one carrying out a precise action. To run that program in the right order, Python needs to know where one action starts and where it stops. That is exactly what a statement does: it splits the code into separate actions, which the interpreter carries out one after another.

An assignment, a function call, a return, an import are all different kinds of statements, yet they share the same writing rule: one per line, with no closing punctuation. Here are four of them, each doing something different:

PYTHON
total = 0                 # assignment: creates the variable
total += price            # compound assignment: updates it
print(total)              # call: displays its value
del total                 # deletion: erases the variable

This layout is not a cosmetic choice. The semicolon exists in Python and would technically let several statements sit on the same line, but no serious project uses it: one statement per line is part of the language identity, exactly like indentation. The rest of this entry explains how to recognise a statement and how several of them assemble into blocks.


Statement or expression

A beginner who writes if x = 5: gets an error that does not always speak for itself: Python refuses a single equals sign at that exact spot. The reason lies in a distinction that explains most early SyntaxError messages: an expression produces a value, a statement does something. 2 + 2 is an expression, it is worth 4 and can be used anywhere a value is expected. x = 2 + 2 is a statement, it assigns that value to a name and is not worth anything by itself.

The table below places similar-looking writings side by side, to locate the boundary between the two:

WrittenNature
price * 1.2Expression, produces a value
total = price * 1.2Statement, assigns that value
if price > 10:Compound statement, opens a block
len(items)Expression, a call handing back a result

This boundary has a direct consequence: a statement can never slip in where an expression is expected. That is what forbids writing x = (return 5) or placing an assignment inside a condition, and it is also what, by pushing away the single equals sign, protects against the great classic bug carried over from other languages.

Good to know

A call such as print(total) looks like a statement but stays an expression: it always hands back a value, here None. Writing x = print(total) does display the number on screen, but it stores None in x, never the value that was displayed.


Compound statements

Some statements do not fit on a single line: they open a space where other statements come to sit. An if that tests a condition still needs to carry out something when it is true, and that something is itself made of statements. These compound statements are recognisable by their header line ending in a colon, followed by an indented block: if, for, while, try, def, class, with.

Warning

Forgetting the colon at the end of a header line is the most common syntax error at the start of learning. As long as that sign is missing, Python refuses to run the whole file, not just the line in question.

One question remains: what happens when the block has nothing to hold yet, for instance while sketching out the shape of a program before filling it in? A block can never stay empty, and leaving the colon without a follow-up raises an error. That is exactly the situation the pass statement was built for: it does nothing, but it fills the place and keeps the file valid while the rest gets written.


The most common simple statements

Unlike the previous ones, simple statements fit on one line and open no block. A Python program contains far more of them than compound statements, and knowing them is enough to read most of the code already out there:

StatementWhat it does
x = 5Creates or reassigns a variable
returnEnds a function and hands a value back
importMakes a module available
raiseRaises an exception
delRemoves a name or an item
assertChecks an assumption while developing
break, continueChange the flow of a loop
passDoes nothing, but fills a block


Frequently asked questions

Question

Is a semicolon needed at the end of a line?

No, never in Python. The line break is enough to end a statement. Adding a semicolon raises no error, but it immediately signals a habit brought over from another language, and reviewers tend to notice right away.

Question

How is a statement too long for one line written?

By leaning on the parentheses, brackets or braces already present in the statement: as long as they stay unclosed, Python keeps reading the next line without complaint. That is the recommended method, well ahead of a backslash at the end of the line, which still works but breaks easily at the smallest edit.

Question

Can several statements go on one line?

Technically yes, with a semicolon, and also for a single-line block written right after a colon. Both writings work, but they hurt readability, and automatic formatters undo them without even asking whoever wrote them.

Related terms

Discover our python glossary

Browse the terms and definitions most commonly used in development with Python.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.