Skip to main content

Simple statements

A simple statement is one statement on one logical line (possibly combined with others using ;). This page enumerates every form.

Source-of-record: CPython simple statements, Python/compile.c.

Expression statement​

Any expression used as a statement evaluates the expression and discards the result. In single-mode compilation (REPL), the result is also passed to sys.displayhook.

SourceBytecode
f(x)LOAD_NAME f; LOAD_NAME x; CALL 1; POP_TOP.
1 + 2LOAD_CONST 1; LOAD_CONST 2; BINARY_OP add; POP_TOP.

Assignment​

Simple assignment​

target_list = expression
target_list = target_list = ...

The right side is evaluated once; then each target is assigned, in left-to-right order.

Targets can be:

TargetAction
NAMEBind in the current scope.
attributesetattr(obj, name, value).
subscription__setitem__.
slicing__setitem__(slice(...), value).
*target (in a list/tuple)Collect remaining into a list.
(t1, t2, ...) / [t1, t2, ...]Iterable unpacking.

Iterable unpacking​

a, b, c = iterable
a, *b, c = iterable

The right side must be iterable. With a *target, exactly one starred target is allowed; non-starred targets are filled first from the iterable's ends, and the starred target receives the middle as a list.

Augmented assignment​

target op= value # +=, -=, *=, /=, //=, %=, **=, @=, &=, |=, ^=, <<=, >>=
StepAction
1Evaluate the target once (for attribute or subscript reads).
2Evaluate value.
3Try the in-place dunder (__iadd__ etc.). If it returns NotImplemented, fall back to the normal binary dunder.
4Assign the result back to the target.

Annotated assignment​

target: annotation = value # value optional
FormEffect
x: int = 0Bind x and record annotation lazily.
x: intRecord annotation; do not bind x at runtime.
self.x: int = 0Equivalent to self.x = 0; annotation ignored.

Annotations are evaluated lazily in 3.14 (PEP 649/749). Inspecting them goes through inspect.get_annotations(obj).

pass​

pass

Does nothing. Emitted bytecode is empty; the statement exists so that blocks can be syntactically non-empty.

del​

del target_list

For each target:

TargetAction
NAMEUnbind in the current scope.
attributedelattr(obj, name).
subscription__delitem__.
slicing__delitem__(slice(...)).

return​

return # equivalent to return None
return expression
return e1, e2, e3 # returns the tuple

A return outside a function body is a SyntaxError. A return inside a generator function ends iteration; the value (if any) is attached to the resulting StopIteration.value.

yield​

yield # yields None
yield expression
yield from iterable

A yield statement makes the enclosing function a generator function. See Expressions -> yield for the protocol; this entry covers yield used as a statement (the result is discarded).

raise​

raise # re-raise active exception
raise EXC # raise EXC()
raise EXC(args)
raise EXC from CAUSE # set __cause__
raise EXC from None # suppress implicit context
BehaviourSource
Implicit contextSet automatically by the interpreter.
Explicit causeSet by from.
TracebackSet when the exception is raised.

break​

break

Exits the innermost enclosing for or while loop. If the loop has an else clause, it is skipped.

A break outside a loop is a SyntaxError.

continue​

continue

Continues with the next iteration of the innermost enclosing loop. A continue outside a loop is a SyntaxError.

import​

See Import system for the protocol. The statement forms:

FormBytecode
import aIMPORT_NAME 'a'; STORE_NAME 'a'.
import a as xIMPORT_NAME 'a'; STORE_NAME 'x'.
import a.bIMPORT_NAME 'a.b'; STORE_NAME 'a'.
import a.b as xIMPORT_NAME 'a.b'; IMPORT_FROM 'b'; STORE_NAME 'x'; POP_TOP.
from a import bIMPORT_NAME 'a'; IMPORT_FROM 'b'; STORE_NAME 'b'; POP_TOP.
from a import *IMPORT_NAME 'a'; IMPORT_STAR.
from .a import bLevel encoded in IMPORT_NAME.

from x import * is only legal at module scope.

global​

global NAME [, NAME ...]

Declares that any binding of NAME in the current block writes to the module globals. Must precede any use of the name. Combining with a parameter default def f(x=1): global x is a SyntaxError.

nonlocal​

nonlocal NAME [, NAME ...]

Declares that any binding of NAME writes to the nearest enclosing function scope where NAME is bound. Must precede any use. Must find a binding; otherwise SyntaxError.

assert​

assert expression
assert expression, message

If __debug__ is true (the default), evaluates expression. If falsy, raises AssertionError(message). Running with -O strips asserts at compile time.

type (PEP 695)​

type Name = expression
type Name[T] = expression

Creates a TypeAliasType object bound to Name. The right-hand side is evaluated lazily, accessible as Name.__value__.

Multiple statements on one line​

x = 1; y = 2; z = 3

Semicolons join simple statements on one logical line. Compound statements cannot appear in such a sequence.

Gopy status​

Every simple statement listed is wired and lowered to the matching bytecode form. The bytecode emitted is byte-equivalent with CPython 3.14.

Reference​