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_suiteParameters & Attributes
| Name | Type | Description |
|---|---|---|
| condition | expression | Test before each iteration. |
| suite | statement suite | Loop body. |
| completion_suite | statement suite | Optional normal-completion body. |
Working Example
Example Snippetpython
n=3seen=[]while n: seen.append(n) n-=1else: seen.append(0)print(seen)