Skip to content

Control flow

Indentation is important

Python uses indentation or the off-side rule to declare a block of code. A code block is considered open on indent and closed on dedent.

The if condition

The general syntax of an if statement in Python is

if condition:
  code to execute if True

In the example below, since a == 10 evaluates to True the indented block of code will be executed

a = 10

if a == 10:
  print('condition is True')

The else clause

If you want to execute a different block of code if the evaluated condition returns False, you would use an else clause

if a == 0:
  print('condition is True')
else:
  print('condition is False')

The elif clause

We can further complicate this if statement with an elif, short for else if

if a == 0:
  print('a is equal to 0')
elif a == 10:
  print('a is equal to 10')
else:
  print('a is not equal to 0 or 10')