Language Syntax

While — Python syntax

Repeat while the condition is truthy. The condition is evaluated before each iteration; else runs when it becomes false, including zero iterations, but not on break. The else suite is optional.

Formal Syntax & Structure

python syntaxpython
while condition:    suiteelse:    completion_suite

Parameters & Attributes

NameTypeDescription
conditionexpressionTest before each iteration.
suitestatement suiteLoop body.
completion_suitestatement suiteOptional normal-completion body.

Working Example

Example Snippetpython
n=3seen=[]while n:    seen.append(n)    n-=1else:    seen.append(0)print(seen)
Python Reference