Keywords and Control Flow

if — Conditional execution

Choose a branch using truth testing. Evaluate conditions from top to bottom and execute only the first matching suite; the final else handles the no-match case. A separate if starts a separate decision. Conditions may be any object with valid truth semantics, not just bool values. Brackets in the syntax denote optional clauses, not literal Python brackets.

Formal Syntax & Structure

python syntaxpython
if condition:    suite[elif condition:    suite] ...[else:    suite]

Working Example

Example Snippetpython
stock = 3if stock > 0:    print("available")else:    print("sold out")
Python Reference