Concept Flow - Assert statement usage
Start Execution
Evaluate Assert Condition
Continue
End or Next Statement
The program checks the assert condition; if true, it continues, if false, it stops with an error.
x = 5 assert x > 0 print('x is positive')
| Step | Action | Condition Evaluated | Result | Next Step |
|---|---|---|---|---|
| 1 | Assign x = 5 | N/A | x=5 | Evaluate assert |
| 2 | Evaluate assert x > 0 | 5 > 0 | True | Print message |
| 3 | Print message | N/A | 'x is positive' | End |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| x | undefined | 5 | 5 | 5 |
assert condition Checks if condition is True. If False, raises AssertionError and stops. Used for debugging and validating assumptions. Can include optional error message. Skipped if Python runs with -O option.